pymysql执行mysql语句在 on duplicate key update这句报错不知如何解决
在学习python中练习数据入库时候这里一直提示报错:
# 插入抓取的数据到表中 cursor = cursor(connection) cursor.executemany('''insert into myfund(fcode,fname,nav,accnav,updatetime) values(%(fcode)s,%(fname)s,%(nav)s,%(accnav)s,%(updatetime)s) on duplicate key update `updatetime`= %(updatetime)s,nav= %(nav)s,accnav= %(accnav)s ''', result)登录后复制
报错内容:
traceback (most recent call last): file "f:pythoncodelearn9pymysqlmain.py", line 35, in登录后复制cursor.executemany('''insert into myfund(fcode,fname,nav,accnav,updatetime) file "f:pythoncodeenvlbqlibsite-packagespymysqlcursors.py", line 173, in executemany return self._do_execute_many( file "f:pythoncodeenvlbqlibsite-packagespymysqlcursors.py", line 211, in _do_execute_many rows += self.execute(sql + postfix) file "f:pythoncodeenvlbqlibsite-packagespymysqlcursors.py", line 148, in execute result = self._query(query) file "f:pythoncodeenvlbqlibsite-packagespymysqlcursors.py", line 310, in _query conn.query(q) file "f:pythoncodeenvlbqlibsite-packagespymysqlconnections.py", line 548, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) file "f:pythoncodeenvlbqlibsite-packagespymysqlconnections.py", line 775, in _read_query_result result.read() file "f:pythoncodeenvlbqlibsite-packagespymysqlconnections.py", line 1156, in read first_packet = self._read_packet() file "f:pythoncodeenvlbqlibsite-packagespymysqlconnections.py", line 725, in _read_packet packet.raise_for_error() file "f:pythoncodeenvlbqlibsite-packagespymysqlprotocol.py", line 221, in raise_for_error err.raise_mysql_exception(self._data) file "f:pythoncodeenvlbqlibsite-packagespymysqlerr.py", line 143, in raise_mysql_exception raise errorclass(errno, errval) pymysql.err.programmingerror: (1064, "you have an error in your sql syntax; check the manual that corresponds to your mysql server version for the right syntax to use near '%(updatetime)s,nav= %(nav)s,accnav= %(accnav)s' at line 3")
如果不写语句中的 on duplicate key update...则执行没问题
# 插入抓取的数据到表中 cursor = cursor(connection) cursor.executemany('''insert into myfund(fcode,fname,nav,accnav,updatetime) values(%(fcode)s,%(fname)s,%(nav)s,%(accnav)s,%(updatetime)s) ''', result)登录后复制
但是这个语句放到mysql中执行则正常
mysql> insert into myfund(fcode,fname,NAV,ACCNAV,updatetime) -> values('002649','华银能源革新灵活配比混合','1.3110','1.2840','2022-12-25 22:22:33') -> on duplicate key update `updatetime`= '2022-12-25 22:22:33',NAV= '1.3110',ACCNAV= '1.2840'; Query OK, 1 row affected (0.07 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from myfund where fcode = '002649'; +---------+-----------------------------+------+-------+---------------------+ | fcode | fname | NAV | ACCNAV | updatetime | +---------+-----------------------------+------+-------+---------------------+ | 002649 | 华银能源革新灵活配比混合 | 1.311 | 1.284 | 2022-12-25 22:22:33 | +---------+-----------------------------+------+-------+---------------------+ 1 row in set (0.00 sec)登录后复制
参数是一个dump
以上就是pymysql执行MySQL语句`on duplicate key update`报错,如何解决?的详细内容,更多请关注慧达安全导航其它相关文章!
免责 声明
1、本网站名称:慧达安全导航
2、本站永久网址:https//www.huida178.com/
3、本站所有资源来源于网友投稿和高价购买,所有资源仅对编程人员及源代码爱好者开放下载做参考和研究及学习,本站不提供任何技术服务!
4、本站所有资源的属示图片和信息不代表本站的立场!本站只是储蓄平台及搬运
5、下载者禁止在服务器和虚拟机下进行搭建运营,本站所有资源不支持联网运行!只允许调试,参考和研究!!!!
6、未经原版权作者许可禁止用于任何商业环境,任何人不得擅作它用,下载者不得用于违反国家法律,否则发生的一切法律后果自行承担!
7、为尊重作者版权,请在下载24小时内删除!请购买原版授权作品,支持你喜欢的作者,谢谢!
8.若资源侵犯了您的合法权益,请持 您的版权证书和相关原作品信息来信通知我们!QQ:1247526623我们会及时删除,给您带来的不便,我们深表歉意!
9、如下载链接失效、广告或者压缩包问题请联系站长处理
10、如果你也有好源码或者教程,可以发布到网站,分享有金币奖励和额外收入!
11、本站资源售价只是赞助,收取费用仅维持本站的日常运营所需
12、因源码具有可复制性,一经赞助,不得以任何形式退款。
13、本文内容由网友自发贡献和站长收集,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系1247526623@qq.com
转载请注明出处: 慧达安全导航 » pymysql执行MySQL语句`on duplicate key update`报错,如何解决?
发表评论 取消回复