import?sqlite3
coo?=?sqlite3.connect('mrsoft.db')
cursor?=?coo.cursor()
try:
????cursor.execute("insert?into?user(id,name)values?('4','Yu')")
except?Exception?as?e:
????print('錯(cuò)誤信息:',e)
finally:
????cursor.close()
????coo.close()
????print('操作數(shù)據(jù)庫完成')
?執(zhí)行這段代碼后,沒有保存,但是數(shù)據(jù)庫內(nèi)未增加數(shù)據(jù)。import?sqlite3
?with?sqlite3.connect('mrsoft.db')?as?cursor:
?????cursor.execute('insert?into?user(id,name)values?("3","Bob")')
執(zhí)行這段代碼后,成功將數(shù)據(jù)插入數(shù)據(jù)庫中了。。。
求教,為什么出現(xiàn)了這種情況呢?
1 回答
已采納

pardon110
TA貢獻(xiàn)1038條經(jīng)驗(yàn) 獲得超227個(gè)贊
游標(biāo)對(duì)象是用來執(zhí)行select查詢數(shù)據(jù)庫的,db連接對(duì)象才是用來做增刪改操作的。你在cursor對(duì)象上insert自然得不到結(jié)果,后者用with雖然句柄名叫cursor,但實(shí)際上是一個(gè)數(shù)據(jù)庫連接對(duì)象。python雖是動(dòng)態(tài)語言,但最基本的類型意識(shí)還要是有的。分清數(shù)據(jù)類型,類型決定操作的方法集。
添加回答
舉報(bào)
0/150
提交
取消