将Postgresql 9.5与psycopg2一起使用。
使用“返回”,我可以检索插入的单行的“ id”。
sql = "insert into %s (%s) values (%s) returning id" % (table_name, columns, values_template)
values = tuple(row_dict[key] for key in keys)
cur.execute(sql, values)
id_new_row = cur.fetchone()[0]
我希望也可以检索使用该executemany
语句插入的许多行中最后一行的ID 。
cur.executemany(insert_sql , data_dict)
#get id of the last row inserted
id_new_row = cur.fetchone()[0]
但这会引发DatabaseError,“没有要提取的结果”。
这是根本上不可能做的事情,还是我没有正确要求结果?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句