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

2
  • 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!! Commented Mar 17, 2011 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 Commented Mar 17, 2011 at 13:05

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.