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"
3 个解决方案
#1
51
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"
#2
29
Turns out I needed conn.commit()
at the end
事实证明我最后需要conn.commit()
#3
2
psycopg2
is Python DB API-compliant, so the auto-commit feature is off by default. You need to call conn.commit
to commit any pending transaction to the database. As connections (and cursors) are context managers, you can simply use the with
statement to automatically commit/rollback a transaction on leaving the context:
psycopg2与Python 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
with
block, if no exception has been raised by the block, the transaction is committed. In case of exception the transaction is rolled back.当连接退出with块时,如果块没有引发异常,则提交事务。如果发生异常,则回滚事务。
When a cursor exits the
with
block it is closed, releasing any resource eventually associated with it. The state of the transaction is not affected.当光标退出with块时,它将关闭,释放最终与之关联的任何资源。交易状态不受影响。
#1
51
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"
#2
29
Turns out I needed conn.commit()
at the end
事实证明我最后需要conn.commit()
#3
2
psycopg2
is Python DB API-compliant, so the auto-commit feature is off by default. You need to call conn.commit
to commit any pending transaction to the database. As connections (and cursors) are context managers, you can simply use the with
statement to automatically commit/rollback a transaction on leaving the context:
psycopg2与Python 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
with
block, if no exception has been raised by the block, the transaction is committed. In case of exception the transaction is rolled back.当连接退出with块时,如果块没有引发异常,则提交事务。如果发生异常,则回滚事务。
When a cursor exits the
with
block it is closed, releasing any resource eventually associated with it. The state of the transaction is not affected.当光标退出with块时,它将关闭,释放最终与之关联的任何资源。交易状态不受影响。