psycopg2 not actually inserting data

前端 未结 2 1959
我寻月下人不归
我寻月下人不归 2020-12-14 05:18

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=\'localho         


        
相关标签:
2条回答
  • 2020-12-14 06:02

    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.

    0 讨论(0)
  • 2020-12-14 06:02

    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.

    0 讨论(0)
提交回复
热议问题