开发者

Unable to insert data insert data in postgres table using pgdb

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 开发者_如何学Goinserted and then deleted when the program finishes execution. I have ran out of ideas on this one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜