python - Postgresql 表更新

标签 python database postgresql psycopg

我使用来自 python 的 postgresql 更新了一个数据库表 我的代码是

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()

for row in rows:
   i=i+1
   p = findmax(row)
   #print p
   idn="id"
   idn=idn+str(i)


   cursor.execute("UPDATE im_entry.pr_table SET (selected_entry) = ('"+p+"') WHERE  image_1d ='"+idn+"'")
   print 'DATABASE TO PRINT'
cursor.execute("SELECT * FROM im_entry.pr_table")
rows=cursor.fetchall()
for row in rows:
    print row   

我显示了更新后的表格

但是当我通过 psql 显示更新的表时 homedb=# SELECT * FROM im_entry.pr_table; 我得到一个空表显示..怎么了??请帮助我

最佳答案

您可能没有提交事务,即您需要在所有更新后执行 connection.commit()

您可以对隔离级别进行各种不同的设置,例如自动提交,所以你不需要自己提交。参见,例如,How do I do database transactions with psycopg2/python db api?

关于python - Postgresql 表更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5339139/

相关文章:

用于音频混合拆分的 Python 模块

database - 对 Apex 新手有好的资源/建议吗?

PostgreSQL : Cast Data Type from Text to Bigint when using WHERE IN

Postgresql 显式 VACUUM 与自动 VACUUM : Differences? 建议?

mysql - pgloader:堆耗尽,游戏结束。问题

python - 如何在 Django 中更改模型的 `object_name`

python - 如何使用 pandas 消除该图中的外部值?

mysql - 在非常大的 MySQL 分析表中 - 我应该索引时间戳吗?

php - 使用 MySQL 动态创建日期周期

Python bokeh - 链接时间序列