色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

python mysql使用executemany()出現TypeError

瀏覽:123日期:2022-08-01 09:27:08

問題描述

def insertData(self,table,param):try: self.db.set_character_set(’utf8’) q= [] for x in param:cols = ’, ’.join(x.keys())values = ’',' ’.join(x.values())q.append((table, cols, ’'’+values+’'’)) sql = 'INSERT INTO %s(%s) VALUES(%s)' try:result = self.cur.executemany(sql,q)insert_id = self.db.insert_id()self.db.commit() except MySQLdb.Error,e:#發生錯誤時回滾self.db.rollback()except MySQLdb.Error,e: print self.getCurrentTime(),'數據庫錯誤,原因%d: %s' % (e.args[0], e.args[1])

其中q的部分內容為[(’houseurl’, ’url’, u’'/ershoufang/szlh11469938.html'’), (’houseurl’, ’url’, u’'/ershoufang/szlh11470634.html'’)]

執行以上代碼后,出現以下問題:

29 sql = 'INSERT INTO %s(%s) VALUES(%s)' 30 try:---> 31 result = self.cur.executemany(sql,q) 32 insert_id = self.db.insert_id() 33 self.db.commit()/usr/lib/python2.7/dist-packages/MySQLdb/cursors.pyc in executemany(self, query, args) 274 self.errorhandler(self, ProgrammingError, msg.args[0]) 275 else:--> 276 self.errorhandler(self, TypeError, msg) 277 except (SystemExit, KeyboardInterrupt): 278 raise/usr/lib/python2.7/dist-packages/MySQLdb/connections.pyc in defaulterrorhandler(***failed resolving arguments***) 34 del connection 35 if isinstance(errorvalue, BaseException):---> 36 raise errorvalue 37 if errorclass is not None: 38 raise errorclass(errorvalue)TypeError: not all arguments converted during string formatting

但是我一條條插入使用execute()就沒問題。

問題解答

回答1:

'INSERT INTO %s(%s) VALUES(%s)'

這種寫法是錯誤的。占位符 %s 只能出現在值的地方,不能作為表名、字段名出現。.execute* 不會幫你處理這些東西。

你可以預先構造好合適的 SQL 模板,再傳給 .execute*。前提是,你的表名、字段名是確定不會有特殊字符的:

fields = ...data = ...sql = ’INSERT INTO {}({}) VALUES(%s)’.format(table, fields)cur.executemany(sql, data)

標簽: Python 編程
相關文章:
主站蜘蛛池模板: xx毛片| 波多野结衣手机视频一区 | 97视频在线免费观看 | 国产真实乱子伦精品视手机观看 | 久草com| 在线精品亚洲 | 在线观看亚洲免费 | 三级全黄视频 | 黄色在线视频网 | 国产一级a毛片高清 | 国产精品视频免费观看调教网 | 夜色邦合成福利网站 | 中文字幕日本不卡 | 国产一区精品 | 色综久久 | 亚洲www视频| 国产一级一级一级成人毛片 | 香港毛片免费观看 | 国产精品久久久久久久毛片 | 日韩中文精品亚洲第三区 | 欧美一区二区在线观看 | 久久2017| 怡红院免费全部视频在线视频 | 久久久久久综合一区中文字幕 | 久久精品国产欧美日韩99热 | 久久久9视频在线观看 | 国产成人经典三级在线观看 | 日本三级香港三级人妇99视 | 亚洲 欧美 中文字幕 | 日本黄色美女网站 | 欧美激情特级黄aa毛片 | 99精品欧美一区二区三区美图 | 欧美日韩在线视频播放 | 国产成人精品视频在放 | 亚洲高清综合 | 久久国产精品免费视频 | 久久青草免费免费91线频观看 | 最新国产精品亚洲二区 | 成人午夜久久 | 国产成人免费在线 | 91香蕉国产线在线观看免费 |