本文介紹了Python psycopg2 沒有插入到 postgresql 表中的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在使用以下方法嘗試將記錄插入到 postgresql 數(shù)據(jù)庫表中,但它不起作用.我沒有收到任何錯(cuò)誤,但表中沒有記錄.我需要提交還是什么?我正在使用通過 Bitnami djangostack 安裝的 postgresql 數(shù)據(jù)庫.
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"
推薦答案
如果不想每條記錄都提交到數(shù)據(jù)庫,可以添加以下行:
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"
這篇關(guān)于Python psycopg2 沒有插入到 postgresql 表中的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!