Postgresql 表更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5339139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 22:54:41  来源:igfitidea点击:

Postgresql table update

pythondatabasepostgresqlpsycopg

提问by Ria

I updated a database table using postgresql from python My code was

我使用 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   

I got the updated table displayed

我显示了更新的表格

But when i display updated table by psql as homedb=# SELECT * FROM im_entry.pr_table; i got an empty table displayed..what is wrong?? please help me

但是当我通过 psql 将更新的表显示为 homedb=# SELECT * FROM im_entry.pr_table; 我显示了一张空桌子……怎么了??请帮我

回答by seb

You're probably not committing the transaction, i.e. you need a connection.commit()after all your updates.

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

There are various different settings you can make to the isolation level, e.g. autocommit, so you don't need to issue commits yourself. See, for example, How do I do database transactions with psycopg2/python db api?

您可以对隔离级别进行各种不同的设置,例如自动提交,因此您无需自己发出提交。例如,请参阅如何使用 psycopg2/python db api 进行数据库事务?