MANU
MANU

Reputation: 1446

Inserting rows into db from a list of tuples using cursor.mogrify gives error

I am trying to insert large number of rows into postgres using cursor.mogrify using this psycopg2: insert multiple rows with one query

data is a list of tuples where each tuple is a row that needs to be inserted.

 cursor = conn.cursor()

    args_str = ','.join(cursor.mogrify("(%s,%s,%s,%s,%s,%s,%s,%s)", x) for x in data)

    cursor.execute(
        "insert into table1 (n, p, r, c, date, p1, a, id) values " + args_str)`

but getting error :

TypeError: sequence item 0: expected str instance, bytes found

at line:

  args_str = ','.join(cursor.mogrify("(%s,%s,%s,%s,%s,%s,%s,%s)", x) for x in data)

If I try to change to b''.join(cursor.mogrify("(%s,%s,%s,%s,%s,%s,%s,%s)", x) for x in data) , then execute query gives error for inserting byte....

Am I doing something wrong ?

Upvotes: 2

Views: 4530

Answers (2)

Clodoaldo Neto
Clodoaldo Neto

Reputation: 125444

data = [(1,2),(3,4)]
args_str = ','.join(['%s'] * len(data))
sql = "insert into t (a, b) values {}".format(args_str)
print (cursor.mogrify(sql, data).decode('utf8'))
#cursor.execute(sql, data)

Output:

insert into t (a, b) values (1, 2),(3, 4)

Upvotes: 8

Maurice Meyer
Maurice Meyer

Reputation: 18136

You could do something like this, but verify dict values to prevent sql injection.

>>> from psycopg2.extensions import AsIs
>>> _insert_sql = 'INSERT INTO myTable (%s) VALUES %s RETURNING id'    
>>> data = {"col_1": "val1", "col_2": "val2"}
>>> values = (AsIs(','.join(data.keys())), tuple(data.values()))
>>> print(cur.mogrify(_insert_sql, values))
b"INSERT INTO myTable (col_1,col_2) VALUES ('val1', 'val2') RETURNING id"

Upvotes: 2

Related Questions