本文介紹了Python psycopg2 沒有插入到 postgresql 表中的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在使用以下方法嘗試將記錄插入到 postgresql 數據庫表中,但它不起作用.我沒有收到任何錯誤,但表中沒有記錄.我需要提交還是什么?我正在使用通過 Bitnami djangostack 安裝的 postgresql 數據庫.
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.
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"
推薦答案
如果不想每條記錄都提交到數據庫,可以添加以下行:
If don't want to have to commit each entry to the database, you can add the following line:
conn.autocommit = True
所以你得到的代碼是:
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"
這篇關于Python psycopg2 沒有插入到 postgresql 表中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!