python2.7 mysql execute()問題
問題描述
插入數據的部分代碼:
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()#判斷是否執行成功if result: return insert_idelse: return 0 except MySQLdb.Error,e:#發生錯誤時回滾print '數據庫錯誤,原因%d: %s' % (e.args[0], e.args[1]) self.db.rollback()
在執行完程序后得到
[(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)數據庫錯誤,原因1136: Column count doesn’t match value count at row 1None
那條insert語句我在數據庫中手動輸入時并沒有問題,但是不知道為什么execute出錯。當我向一個只存鏈接的表插入數據時,是完全沒問題的。剛剛我想輸出錯誤原因時,發現數據竟然迷之插入成功了,然后我試圖用executemany()后就又出現錯誤了。
問題解答
回答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. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?2. android - 百度地圖加載完成監聽3. 關于docker下的nginx壓力測試4. nignx - docker內nginx 80端口被占用5. java - 阿里的開發手冊中為什么禁用map來作為查詢的接受類?6. macos - mac下docker如何設置代理7. python3.x - git bash如何運行.bat文件?8. dockerfile - [docker build image失敗- npm install]9. golang - 用IDE看docker源碼時的小問題10. docker api 開發的端口怎么獲取?
