PostgreSql与Python
我正在使用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值放入新表格呢?
请帮帮我。
1 个回答
1
p 是一个元组,所以你可以用这个元组创建一个新的 execute,并且使用 INSERT 语句来插入数据:
cursor.execute("INSERT INTO new_table (x, ...) VALUES (%s, ...)", p)
这里:
- (x, ....) 里面包含了列的名称
- (%s, ...) %s 会为每一列重复出现