3

我使用 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; 我有一个空表显示..怎么了?请帮我

4

1 回答 1

7

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

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

于 2011-03-17T13:03:55.530 回答