python - PostgreSQL 和 Python

标签 python database postgresql psycopg2

我正在使用 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/

相关文章:

python - 刷新Python tkinter TreeView

sql - 使用 JOIN 有条件地更新 SQL 表

python - 仅在选定区域中查找 2d-Numpy 数组中最大值的索引

php - codeigniter - 如何查询两个表中的两个变量并连接?

php - 我需要关于数据库问题的建议

python - Google App Engine - ndb 按字符串排序? (Python)

python - 使用带有 Mod_Python 和多个 Python 安装的 Apache 在 Virtualenv 中运行 Django

python - 在python中用反斜杠单引号替换单引号

python - 多热标签编码

sql - 在 JOIN 上具有重复列名的 WHERE 语句 - PostgreSQL