本文介紹了Python MySQLdb不會(huì)插入到數(shù)據(jù)庫(kù)中的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
您好,我在向數(shù)據(jù)庫(kù)中插入信息時(shí)遇到問(wèn)題。它不會(huì)發(fā)出錯(cuò)誤。代碼在此處。
import MySQLdb as m
def Room(room):
db = m.connect("localhost","root","password","rooms")
cur = db.cursor()
cur.execute('INSERT INTO rooms (name) VALUES("%s");'% (room))
def getRoomDb():
db = m.connect("localhost","root","password","rooms")
cur = db.cursor()
cur.execute("SELECT * FROM rooms;")
result = cur.fetchall()
return result
print getRoomDb()
在我運(yùn)行Room("roomname")之后,它的輸出應(yīng)該是正確的,但實(shí)際上沒(méi)有任何內(nèi)容被放入數(shù)據(jù)庫(kù)
推薦答案
您沒(méi)有為執(zhí)行插入的事務(wù)調(diào)用commit()。
在Python中,默認(rèn)操作是回滾工作,除非您顯式提交。
另見(jiàn):
Python MySQL – SELECTs work but not DELETEs?
Python MySQLdb update query fails
http://mysql-python.sourceforge.net/FAQ.html#my-data-disappeared-or-won-t-go-away
這篇關(guān)于Python MySQLdb不會(huì)插入到數(shù)據(jù)庫(kù)中的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,






