我正在使用 python 和 postgresql。我有一个包含 6 列的表。一个 id 和 5 个条目。我想将 id 和 5 个条目中重复次数最多的条目复制到一个新表中。
我这样做了:
import psycopg2
connection=psycopg2.connect("dbname=homedb user=ria")
cursor=connection.cursor()
l_dict= {'licence_id':1}
cursor.execute("SELECT * FROM im_entry.usr_table")
rows=cursor.fetchall()
cursor.execute("INSERT INTO im_entry.pr_table (image_1d) SELECT image_1d FROM im_entry.usr_table")
for row in rows:
p = findmax(row) #to get most repeated entry from first table
.................
.................
那我怎么把这个p值输入到新表中呢?
请帮帮我
最佳答案
p 是一个元组,因此您可以使用传递元组(或部分)的 INSERT 语句创建一个新的 execute:
cursor.execute("INSERT INTO new_table (x, ...) VALUES (%s, ...)", p)
哪里:
- (x, ....) 包含列名
- (%s, ...) %s 对每一列重复
关于python - PostgreSQL 和 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5335938/