{"id":200662,"date":"2025-05-06T08:50:50","date_gmt":"2025-05-06T00:50:50","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200662\/"},"modified":"2025-05-06T08:50:50","modified_gmt":"2025-05-06T00:50:50","slug":"pymysql%e6%89%a7%e8%a1%8cmysql%e8%af%ad%e5%8f%a5on-duplicate-key-update%e6%8a%a5%e9%94%99%ef%bc%8c%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200662\/","title":{"rendered":"pymysql\u6267\u884cMySQL\u8bed\u53e5`on duplicate key update`\u62a5\u9519\uff0c\u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>pymysql\u6267\u884cMySQL\u8bed\u53e5`on duplicate key update`\u62a5\u9519\uff0c\u5982\u4f55\u89e3\u51b3\uff1f<\/h1>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u6570\u636e\u5e93\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u4e3b\u673a\u5b9d\u8d1d\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300apymysql\u6267\u884cMySQL\u8bed\u53e5`on duplicate key update`\u62a5\u9519\uff0c\u5982\u4f55\u89e3\u51b3\uff1f\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241111\/1731286474673155caa90ba.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>pymysql\u6267\u884cmysql\u8bed\u53e5\u5728 on duplicate key update\u8fd9\u53e5\u62a5\u9519\u4e0d\u77e5\u5982\u4f55\u89e3\u51b3<\/strong><\/p>\n<p>\u5728\u5b66\u4e60python\u4e2d\u7ec3\u4e60\u6570\u636e\u5165\u5e93\u65f6\u5019\u8fd9\u91cc\u4e00\u76f4\u63d0\u793a\u62a5\u9519\uff1a<\/p>\n<pre># \u63d2\u5165\u6293\u53d6\u7684\u6570\u636e\u5230\u8868\u4e2d\ncursor = cursor(connection)\ncursor.executemany('''insert into myfund(fcode,fname,nav,accnav,updatetime)\nvalues(%(fcode)s,%(fname)s,%(nav)s,%(accnav)s,%(updatetime)s) \non duplicate key update `updatetime`= %(updatetime)s,nav= %(nav)s,accnav= %(accnav)s ''',\nresult)<\/pre>\n<p>\u62a5\u9519\u5185\u5bb9\uff1a<\/p>\n<pre>traceback (most recent call last):\n  file &amp;quot;f:\\pythoncode\\learn\\9pymysql\\main.py&amp;quot;, line 35, in &amp;lt;module&amp;gt;\n    cursor.executemany('''insert into myfund(fcode,fname,nav,accnav,updatetime)\n  file &amp;quot;f:\\pythoncode\\venv\\lbq\\lib\\site-packages\\pymysql\\cursors.py&amp;quot;, line 173, in executemany\n    return self._do_execute_many(\n  file &amp;quot;f:\\pythoncode\\venv\\lbq\\lib\\site-packages\\pymysql\\cursors.py&amp;quot;, line 211, in _do_execute_many\n    rows += self.execute(sql + postfix)\n  file &amp;quot;f:\\pythoncode\\venv\\lbq\\lib\\site-packages\\pymysql\\cursors.py&amp;quot;, line 148, in execute\n    result = self._query(query)\n  file &amp;quot;f:\\pythoncode\\venv\\lbq\\lib\\site-packages\\pymysql\\cursors.py&amp;quot;, line 310, in _query\n    conn.query(q)\n  file &amp;quot;f:\\pythoncode\\venv\\lbq\\lib\\site-packages\\pymysql\\connections.py&amp;quot;, line 548, in query\n    self._affected_rows = self._read_query_result(unbuffered=unbuffered)\n  file &amp;quot;f:\\pythoncode\\venv\\lbq\\lib\\site-packages\\pymysql\\connections.py&amp;quot;, line 775, in _read_query_result\n    result.read()\n  file &amp;quot;f:\\pythoncode\\venv\\lbq\\lib\\site-packages\\pymysql\\connections.py&amp;quot;, line 1156, in read\n    first_packet = self._read_packet()\n  file &amp;quot;f:\\pythoncode\\venv\\lbq\\lib\\site-packages\\pymysql\\connections.py&amp;quot;, line 725, in _read_packet\n    packet.raise_for_error()\n  file &amp;quot;f:\\pythoncode\\venv\\lbq\\lib\\site-packages\\pymysql\\protocol.py&amp;quot;, line 221, in raise_for_error\n    err.raise_mysql_exception(self._data)\n  file &amp;quot;f:\\pythoncode\\venv\\lbq\\lib\\site-packages\\pymysql\\err.py&amp;quot;, line 143, in raise_mysql_exception\n    raise errorclass(errno, errval)\npymysql.err.programmingerror: (1064, &amp;quot;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&amp;quot;)<\/pre>\n<p>\u5982\u679c\u4e0d\u5199\u8bed\u53e5\u4e2d\u7684 on duplicate key update&#8230;\u5219\u6267\u884c\u6ca1\u95ee\u9898<\/p>\n<pre># \u63d2\u5165\u6293\u53d6\u7684\u6570\u636e\u5230\u8868\u4e2d\ncursor = cursor(connection)\ncursor.executemany('''insert into myfund(fcode,fname,nav,accnav,updatetime)\nvalues(%(fcode)s,%(fname)s,%(nav)s,%(accnav)s,%(updatetime)s) ''',\nresult)<\/pre>\n<p>\u4f46\u662f\u8fd9\u4e2a\u8bed\u53e5\u653e\u5230mysql\u4e2d\u6267\u884c\u5219\u6b63\u5e38<\/p>\n<pre>mysql&amp;gt; insert into myfund(fcode,fname,NAV,ACCNAV,updatetime)\n    -&amp;gt; values('002649','\u534e\u94f6\u80fd\u6e90\u9769\u65b0\u7075\u6d3b\u914d\u6bd4\u6df7\u5408','1.3110','1.2840','2022-12-25 22:22:33')\n    -&amp;gt; on duplicate key update `updatetime`= '2022-12-25 22:22:33',NAV= '1.3110',ACCNAV= '1.2840';\nQuery OK, 1 row affected (0.07 sec)\nRows matched: 1  Changed: 1  Warnings: 0\n\nmysql&amp;gt; select * from myfund where fcode = '002649';\n+---------+-----------------------------+------+-------+---------------------+\n| fcode    | fname                       | NAV   | ACCNAV | updatetime          |\n+---------+-----------------------------+------+-------+---------------------+\n| 002649   | \u534e\u94f6\u80fd\u6e90\u9769\u65b0\u7075\u6d3b\u914d\u6bd4\u6df7\u5408 | 1.311 | 1.284 | 2022-12-25 22:22:33 |\n+---------+-----------------------------+------+-------+---------------------+\n1 row in set (0.00 sec)<\/pre>\n<p>\u53c2\u6570\u662f\u4e00\u4e2adump<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300apymysql\u6267\u884cMySQL\u8bed\u53e5`on duplicate key update`\u62a5\u9519\uff0c\u5982\u4f55\u89e3\u51b3\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>pymysql\u6267\u884cMySQL\u8bed\u53e5&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200662","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200662","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200662"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200662\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}