我正在使用以下命令尝试将记录插入到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"
结果我最后需要
conn.commit()
psycopg2
与Python DB API兼容,因此自动提交功能在默认情况下处于关闭状态。您需要调用^{with
语句在离开上下文时自动提交/回滚事务:从docs:
如果不想将每个条目提交到数据库,可以添加以下行:
所以你得到的代码是:
相关问题 更多 >
编程相关推荐