1

I am trying to insert some values in a postgres table in a python script using pgdb.

At first I tried this:

cursor.execute('INSERT INTO sites (url) VALUES (%s)', (i,))

This did not work so I changed it to

print 'INSERT INTO sites (url) VALUES (\'%s\')' %(i)
cursor.execute('INSERT INTO sites (url) VALUES (\'%s\')' %(i))

The output from the print statement isINSERT INTO sites (url) VALUES ('72.240.34.19:80')

If I paste the above output in psql, it works(i.e. row is inserted). No errors are generated in the script but no rows get inserted. The user has all privileges and the cursor is active(I can perform selects etc.)

Since this is the first time I am using postgres I suspect I might be missing something obvious. I looked at all examples I could find but was unable to discover what's wrong with my code.

Edit: I added this line to the code: print 'Rowcount %s' %(cursor.rowcount)

This outputs Rowcount 1 But SELECT * FROM sites is empty

Second Edit: This is weird. I manually inserted a row in the table. It's auto generated id which is of type serial was 10. I ran the script 3 times, as earlier no rows were inserted. Now when I inserted a row manually it's id became 14. It looks like the rows get inserted and then deleted when the program finishes execution. I have ran out of ideas on this one.

Ravi
  • 727
  • 6
  • 19
  • 1
    Found out it was really something I had missed. I needed to perform a commit. i.e. `conn.commit` after making the changes. – Ravi Aug 23 '11 at 13:19

0 Answers0