psycopg2: insert multiple rows with one query

后端 未结 15 2288
谎友^
谎友^ 2020-11-22 09:11

I need to insert multiple rows with one query (number of rows is not constant), so I need to execute query like this one:

INSERT INTO t (a, b) VALUES (1, 2),         


        
15条回答
  •  渐次进展
    2020-11-22 09:54

    Using aiopg - The snippet below works perfectly fine

        # items = [10, 11, 12, 13]
        # group = 1
        tup = [(gid, pid) for pid in items]
        args_str = ",".join([str(s) for s in tup])
        # insert into group values (1, 10), (1, 11), (1, 12), (1, 13)
        yield from cur.execute("INSERT INTO group VALUES " + args_str)
    

提交回复
热议问题