python2.7 mysql execute()問題
問題描述
插入數(shù)據(jù)的部分代碼:
try:sql = 'INSERT INTO {}({}) VALUES(%s)'.format(table,cols) % q[0]print sql result = self.cur.executemany(sql,q)#result = self.cur.execute(sql)insert_id = self.db.insert_id()self.db.commit()#判斷是否執(zhí)行成功if result: return insert_idelse: return 0 except MySQLdb.Error,e:#發(fā)生錯(cuò)誤時(shí)回滾print '數(shù)據(jù)庫(kù)錯(cuò)誤,原因%d: %s' % (e.args[0], e.args[1]) self.db.rollback()
在執(zhí)行完程序后得到
[(u’'',' http://sz.centanet.com/ershoufang/szlg13772053.html',' 345u4e07|3u5ba42u5385|74.89u5e73',' u5357|2007u5e74|u4f4eu5c42(u517134u5c42)|u8c6au88c5|u5eb7u8fbeu5c14u82b1u56edu4e94u671f(u8774u8776u5821)|'’,)]INSERT INTO zydc(follow, house_url, price_area, houseinfo) VALUES(%s)數(shù)據(jù)庫(kù)錯(cuò)誤,原因1136: Column count doesn’t match value count at row 1None
那條insert語句我在數(shù)據(jù)庫(kù)中手動(dòng)輸入時(shí)并沒有問題,但是不知道為什么execute出錯(cuò)。當(dāng)我向一個(gè)只存鏈接的表插入數(shù)據(jù)時(shí),是完全沒問題的。剛剛我想輸出錯(cuò)誤原因時(shí),發(fā)現(xiàn)數(shù)據(jù)竟然迷之插入成功了,然后我試圖用executemany()后就又出現(xiàn)錯(cuò)誤了。
問題解答
回答1:最后我放棄使用executemany()了。
INSERT INTO employees (first_name, hire_date)VALUES (’Jane’, ’2005-02-12’), (’Joe’, ’2006-05-23’), (’John’, ’2010-10-03’)
直接用上面這種形式,execute()就好。https://dev.mysql.com/doc/con...
相關(guān)文章:
1. docker api 開發(fā)的端口怎么獲取?2. docker-machine添加一個(gè)已有的docker主機(jī)問題3. vim - docker中新的ubuntu12.04鏡像,運(yùn)行vi提示,找不到命名.4. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?5. docker綁定了nginx端口 外部訪問不到6. webgl - android上類似汽車之家的3d全景照片怎么實(shí)現(xiàn)7. PHP類封裝的插入數(shù)據(jù),總是插入不成功,返回false;8. 關(guān)于純用css寫導(dǎo)航鼠標(biāo)移入事件的問題?9. 百度地圖api - Android百度地圖SDK,MapView上層按鈕可見卻不可觸,怎么解決?10. javascript - angular錨點(diǎn)問題

網(wǎng)公網(wǎng)安備