最佳答案
我需要将来自龙卷风的 JSON 数据插入 postgres,所以这里的测试是这样的:
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)
正如您在 pythonshell 中执行 select
之后所看到的,有一些数据,但是在 psql 中有
0行! 我可能做错了什么?
Python 2.7.2 +