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. node.js - nodejs+express+vue2. python對8000行csv添加列3. javascript - 如何獲取未來元素的父元素在頁面中所有相同元素中是第幾個?4. javascript - onclick事件點擊不起作用5. python 字符串匹配問題6. DADB.class.php文件的代碼怎么寫7. 使用mysql命令行連接遠程數(shù)據(jù)庫host跳轉(zhuǎn)8. 數(shù)據(jù)庫 - Mysql的存儲過程真的是個坑!求助下面的存儲過程哪里錯啦,實在是找不到哪里的問題了。9. javascript - 我的站點貌似被別人克隆了, google 搜索特定文章,除了域名不一樣,其他的都一樣,如何解決?10. python - 如何把152753這個字符串轉(zhuǎn)變成時間格式15:27:53

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