Python使用psycopg2的copy_from方法高效的大量数据批量导入

    def copy_markettrend_resume_db(self):
        try:
            conn = psycopg2.connect(
                dbname="your_db",
                user="your_user",
                password="your_password",
                host="localhost",
                port="5432"
            )
            with conn.cursor() as cursor:
                with open(self.bbo_csv, "r", encoding="utf-8") as csv_file:
                    next(csv_file)  # 跳过header
                    cursor.copy_from(csv_file, "t_market_trend_hist", sep=",")
                conn.commit()
        except Exception as e:
            self.logger.error(e)
            if conn:
                conn.rollback()
posted @ 2026-02-09 17:44  狗狗听话  阅读(9)  评论(0)    收藏  举报