Python psycopg2未插入到postgresql表中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18068901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:51:22  来源:igfitidea点击:

Python psycopg2 not inserting into postgresql table

pythonpostgresqlinsertpsycopg2

提问by Superdooperhero

I'm using the following to try and insert a record into a postgresql database table, but it's not working. I don't get any errors, but there are no records in the table. Do I need a commit or something? I'm using the postgresql database that was installed with the Bitnami djangostack install.

我正在使用以下方法尝试将记录插入到 postgresql 数据库表中,但它不起作用。我没有收到任何错误,但表中没有记录。我需要提交还是什么?我正在使用随 Bitnami djangostack 安装一起安装的 postgresql 数据库。

import psycopg2

try:
    conn = psycopg2.connect("dbname='djangostack' user='bitnami' host='localhost' password='password'")
except:
    print "Cannot connect to db"

cur = conn.cursor()

try:
    cur.execute("""insert into cnet values ('r', 's', 'e', 'c', 'w', 's', 'i', 'd', 't')""")
except:
    print "Cannot insert"

采纳答案by aright

If don't want to have to commit each entry to the database, you can add the following line:

如果不想将每个条目提交到数据库,您可以添加以下行:

conn.autocommit = True

So your resulting code would be:

所以你的结果代码将是:

import psycopg2

try:
    conn = psycopg2.connect("dbname='djangostack' user='bitnami' host='localhost' password='password'")
    conn.autocommit = True
except:
    print "Cannot connect to db"

cur = conn.cursor()

try:
    cur.execute("""insert into cnet values ('r', 's', 'e', 'c', 'w', 's', 'i', 'd', 't')""")
except:
    print "Cannot insert"

回答by Superdooperhero

Turns out I needed conn.commit()at the end

原来我需要conn.commit()在最后

回答by Eugene Yarmash

psycopg2is Python DB API-compliant, so the auto-commit feature is off by default. You need to call conn.committo commit any pending transaction to the database. As connections (and cursors) are context managers, you can simply use the withstatement to automatically commit/rollback a transaction on leaving the context:

psycopg2Python DB API兼容的,所以默认情况下自动提交功能是关闭的。您需要调用conn.commit将任何挂起的事务提交到数据库。由于连接(和游标)是上下文管理器,您可以简单地使用该with语句在离开上下文时自动提交/回滚事务:

with conn, conn.cursor() as cur:  # start a transaction and create a cursor
    cur.execute(sql)

From the docs:

文档

When a connection exits the withblock, if no exception has been raised by the block, the transaction is committed. In case of exception the transaction is rolled back.

When a cursor exits the withblock it is closed, releasing any resource eventually associated with it. The state of the transaction is not affected.

当连接退出with块时,如果块没有引发异常,则提交事务。在异常的情况下,事务被回滚。

当游标退出with块时,它被关闭,释放最终与其关联的任何资源。事务状态不受影响。

回答by heaven2sai

import psycopg2
conn = psycopg2.connect("dbname='djangostack' user='bitnami' 
host='localhost' password='password'")
con.set_session(autocommit=True)