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ā)生錯誤時回滾print '數(shù)據(jù)庫錯誤,原因%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ù)庫錯誤,原因1136: Column count doesn’t match value count at row 1None
那條insert語句我在數(shù)據(jù)庫中手動輸入時并沒有問題,但是不知道為什么execute出錯。當我向一個只存鏈接的表插入數(shù)據(jù)時,是完全沒問題的。剛剛我想輸出錯誤原因時,發(fā)現(xiàn)數(shù)據(jù)竟然迷之插入成功了,然后我試圖用executemany()后就又出現(xiàn)錯誤了。
問題解答
回答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...
相關文章:
1. django - 后臺返回的json數(shù)據(jù)經(jīng)過Base64加密,獲取時用python如何解密~!2. css3 - 請問一下在移動端CSS布局布局中通常需要用到哪些元素,屬性?3. 我在centos容器里安裝docker,也就是在容器里安裝容器,報錯了?4. 我的html頁面一提交,網(wǎng)頁便顯示出了我的php代碼,求問是什么原因?5. tp6表單令牌6. angular.js - 如何通俗易懂的解釋“依賴注入”?7. docker 17.03 怎么配置 registry mirror ?8. node.js - node 客戶端socket一直報錯Error: read ECONNRESET,用php的socket沒問題哈。。9. 老哥們求助啊10. 在MySQL中新增字段時,報錯??
