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+
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With