Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

psycopg2 not actually inserting data

I need to insert JSON data from tornado to postgres, so here's test like this:

from psycopg2 import connect  conn = connect("user='pguser' host='localhost' dbname='pgdb' password='pgpass'") cursor = conn.cursor()  data = '[{"id":"sdf","name":"wqe","author":"vb"}]'  for row in eval(data):   print row   cursor.execute("""INSERT INTO books(id,name,author) VALUES('%s','%s','%s')""" % \         (row['id'], row['name'], row['author'])   )  >>> cursor.execute("SELECT * FROM books") >>> cursor.fetchall() [('sdf', 'wqe', 'vb')] >>>  $> psql -d pgdb -U pguser -W Password for user pguser:  psql (9.1.6) Type "help" for help.  pgdb=> select * from books;  id | name | author  ----+------+-------- (0 rows) 

As you can see after doing select in python shell, there's some data, but in psql there's 0 rows! What may I be doing wrong?

Python 2.7.2+

like image 322
juk Avatar asked Dec 05 '12 03:12

juk


People also ask

How do I know if psycopg2 is alive?

In order to make sure a connection is still valid, read the property connection. isolation_level . This will raise an OperationalError with pgcode == "57P01" in case the connection is dead. This adds a bit of latency for a roundtrip to the database but should be preferable to a SELECT 1 or similar.

What is commit in psycopg2?

commit() Commit any pending transaction to the database. By default, Psycopg opens a transaction before executing the first command: if commit() is not called, the effect of any data manipulation will be lost.

Should I use psycopg2-binary?

The psycopg2-binary package is meant for beginners to start playing with Python and PostgreSQL without the need to meet the build requirements. If you are the maintainer of a published package depending on psycopg2 you shouldn't use psycopg2-binary as a module dependency.


2 Answers

You didn't commit the transaction.

Psycopg2 opens a transaction automatically, and you must tell it to commit in order to make the data visible to other sessions.

See the psycopg2 FAQ and the connection.commit() method.

like image 56
Craig Ringer Avatar answered Oct 27 '22 13:10

Craig Ringer


Just had the same perplexing issue. To put options together:

as @Craig Ringer writes after cursor.execute you can run connection.commit

cursor.execute('INSERT INTO table VALUES(DEFAULT, %s)', email) ... connection.commit() 

OR after connect set autocommit

connection = connect("user='pguser' host='localhost' dbname='pgdb' password='pgpass'") connection.autocommit = True 

OR use set_session to set autocommit

connection = connect("user='pguser' host='localhost' dbname='pgdb' password='pgpass'")  connection.set_session(autocommit=True) 

All worked for me.

like image 38
frmbelz Avatar answered Oct 27 '22 13:10

frmbelz