3

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

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

winwaed
  • 7,645
  • 6
  • 36
  • 81
Ria
  • 167
  • 2
  • 4
  • 12
  • I solved it myself..I have forgotten to use COMMIT with in the procedure after INSERT and UPDATE statement in order to COMMIT the changes that were made!! – Ria Mar 17 '11 at 13:01
  • What does the `findmax()` function do exactly? I don't think the loop is necessary at all but that depends on what `findmax()` is doing –  Mar 17 '11 at 13:05

1 Answers1

7

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

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?

Community
  • 1
  • 1
seb
  • 3,646
  • 26
  • 21