Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中psycopg2的批提交_Python_Python 3.x_Psycopg2 - Fatal编程技术网

python中psycopg2的批提交

python中psycopg2的批提交,python,python-3.x,psycopg2,Python,Python 3.x,Psycopg2,我是python新手。我有一个用例,在这个用例中,我必须解析一个CSV文件,然后将行插入数据库 这是我的密码 with open(targetFileName, 'r') as csvfile: # creating a csv reader object csvreader = csv.reader(csvfile) for row in csvreader: print(row) with c

我是python新手。我有一个用例,在这个用例中,我必须解析一个CSV文件,然后将行插入数据库

这是我的密码

with open(targetFileName, 'r') as csvfile:
        # creating a csv reader object 
        csvreader = csv.reader(csvfile) 
        for row in csvreader:
            print(row)
            with conn.cursor() as cur:
                cur.execute("insert into test (first, second) values(%s,%s)",row)
                conn.commit()

这里我逐行执行和提交查询。我想批量提交,而不是提交每一行。有什么方法可以做到这一点吗?

还没有测试它,但是您不能将for循环放在第二个with语句中,并将commit放在for循环之后吗

with open(targetFileName, 'r') as csvfile:
    # creating a csv reader object 
    csvreader = csv.reader(csvfile)
    with conn.cursor() as cur:
        for row in csvreader:
            print(row)
            cur.execute("insert into test (first, second) values(%s,%s)",row)
        conn.commit()

您还可以考虑在PycPcG2:

中使用ExcUTETHORD命令