{"id":870,"date":"2024-11-07T14:25:38","date_gmt":"2024-11-07T06:25:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/870\/"},"modified":"2024-11-07T14:25:38","modified_gmt":"2024-11-07T06:25:38","slug":"%e5%be%ae%e4%bf%a1%e4%b8%ad%e5%90%91-mysql-%e6%8f%92%e5%85%a5%e6%96%87%e6%9c%ac%e5%87%ba%e7%8e%b0%e4%b9%b1%e7%a0%81%ef%bc%8c%e8%af%a5%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/870\/","title":{"rendered":"\u5fae\u4fe1\u4e2d\u5411 MySQL \u63d2\u5165\u6587\u672c\u51fa\u73b0\u4e71\u7801\uff0c\u8be5\u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1035\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173026370396746.jpg\" width=\"720\" height=\"288\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173026370396746.jpg 720w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173026370396746-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173026370396746-670x268.jpg 670w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" title=\"\u5fae\u4fe1\u4e2d\u5411 MySQL \u63d2\u5165\u6587\u672c\u51fa\u73b0\u4e71\u7801\uff0c\u8be5\u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" alt=\"\u5fae\u4fe1\u4e2d\u5411 MySQL \u63d2\u5165\u6587\u672c\u51fa\u73b0\u4e71\u7801\uff0c\u8be5\u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5fae\u4fe1\u4e2d\u5411 mysql \u63d2\u5165\u6587\u672c\u51fa\u73b0\u4e71\u7801\u95ee\u9898\u63a2\u8ba8<\/strong><\/p>\n<p>\u5728\u5fae\u4fe1\u5f00\u53d1\u4e2d\uff0c\u5411\u670d\u52a1\u5668\u7684 mysql \u6570\u636e\u5e93\u63d2\u5165\u6587\u672c\u65f6\uff0c\u6c49\u5b57\u5374\u53d8\u6210\u4e86\u4e71\u7801\uff0c\u8fd9\u53ef\u80fd\u662f\u4e00\u4e2a\u4ee4\u4eba\u5934\u75bc\u7684\u95ee\u9898\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u8fd9\u4e2a\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p><strong>\u95ee\u9898\u63cf\u8ff0<\/strong><\/p>\n<p>\u6b63\u5982\u63d0\u95ee\u8005\u6240\u8ff0\uff0c\u5728\u5fae\u4fe1\u7684\u81ea\u52a8\u56de\u590d\u5185\u5bb9\u4e2d\u663e\u793a\u7684\u6c49\u5b57\u90fd\u662f\u6b63\u5e38\u7684\uff0c\u4f46\u662f\u63d2\u5165\u5230\u6570\u636e\u5e93\u4e2d\u7684\u6c49\u5b57\u5374\u53d8\u6210\u4e86\u4e71\u7801\u3002\u800c\u8be5\u6570\u636e\u5e93\u4e2d\u7684 message \u5b57\u6bb5\u662f\u4e00\u4e2a text \u7c7b\u578b\u7684\u5b57\u6bb5\u3002<\/p>\n<p><strong>\u95ee\u9898\u89e3\u51b3<\/strong><\/p>\n<p>\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u5173\u952e\u662f\u4e86\u89e3 mysql \u4e2d text \u5b57\u6bb5\u7684\u7f16\u7801\u95ee\u9898\u3002text \u5b57\u6bb5\u5b58\u50a8\u6587\u672c\u6570\u636e\uff0c\u5176\u7f16\u7801\u9ed8\u8ba4\u4e3a latin1\uff0c\u8be5\u7f16\u7801\u4e0d\u5305\u542b\u4e2d\u6587\u6c49\u5b57\u3002\u56e0\u6b64\uff0c\u5728\u5c06\u4e2d\u6587\u6c49\u5b57\u63d2\u5165\u5230 text \u5b57\u6bb5\u4e2d\u65f6\uff0c\u5982\u679c\u672a\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\uff0c\u5c31\u4f1a\u51fa\u73b0\u4e71\u7801\u3002<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u5728\u63d2\u5165\u64cd\u4f5c\u4e2d\u6307\u5b9a\u6b63\u786e\u7684\u7f16\u7801\u3002\u5728 python \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>import mysql.connector\n\n# \u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\nconn = mysql.connector.connect(\n    host=\"localhost\",\n    user=\"username\",\n    password=\"password\",\n    database=\"database_name\"\n)\n\n# \u521b\u5efa\u6e38\u6807\ncursor = conn.cursor()\n\n# \u63d2\u5165\u6570\u636e\uff0c\u5e76\u6307\u5b9a\u7f16\u7801\u4e3a utf8\nquery = \"INSERT INTO table_name (message) VALUES (%s)\"\ndata = (text, )\ncursor.execute(query, data)\n\n# \u63d0\u4ea4\u4fee\u6539\nconn.commit()\n\n# \u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5\ncursor.close()\nconn.close()<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0ctext \u4e3a\u8981\u63d2\u5165\u7684\u4e2d\u6587\u6587\u672c\uff0cutf8 \u4e3a\u6307\u5b9a\u7684\u7f16\u7801\u3002\u8fd9\u6837\uff0c\u4e2d\u6587\u6587\u672c\u5c31\u80fd\u6b63\u786e\u5730\u63d2\u5165\u5230\u6570\u636e\u5e93\u4e2d\uff0c\u800c\u4e0d\u4f1a\u4e71\u7801\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5fae\u4fe1\u4e2d\u5411 MySQL \u63d2\u5165\u6587\u672c\u51fa\u73b0\u4e71\u7801\uff0c\u8be5\u5982\u4f55\u89e3\u51b3\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5fae\u4fe1\u4e2d\u5411 mysql \u63d2\u5165\u6587\u672c\u51fa\u73b0\u4e71\u7801\u95ee\u9898\u63a2\u8ba8 \u5728\u5fae\u4fe1\u5f00\u53d1\u4e2d\uff0c\u5411\u670d\u52a1\u5668\u7684 mysql \u6570\u636e\u5e93\u63d2\u5165\u6587\u672c\u65f6\uff0c\u6c49\u5b57\u5374\u53d8\u6210\u4e86\u4e71\u7801\uff0c\u8fd9\u53ef\u80fd\u662f\u4e00\u4e2a\u4ee4\u4eba\u5934\u75bc\u7684\u95ee\u9898\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u8fd9\u4e2a\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u3002 \u95ee\u9898\u63cf\u8ff0 \u6b63\u5982\u63d0\u95ee\u8005\u6240\u8ff0\uff0c\u5728\u5fae\u4fe1\u7684\u81ea\u52a8\u56de\u590d\u5185\u5bb9\u4e2d\u663e\u793a\u7684\u6c49\u5b57\u90fd\u662f\u6b63\u5e38\u7684\uff0c\u4f46\u662f\u63d2\u5165\u5230\u6570\u636e\u5e93\u4e2d\u7684\u6c49\u5b57\u5374\u53d8\u6210\u4e86\u4e71\u7801\u3002\u800c\u8be5\u6570\u636e\u5e93\u4e2d\u7684 message \u5b57\u6bb5\u662f\u4e00\u4e2a text \u7c7b\u578b\u7684\u5b57\u6bb5\u3002 \u95ee\u9898\u89e3\u51b3 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u5173\u952e\u662f\u4e86\u89e3 mysql \u4e2d text \u5b57\u6bb5\u7684\u7f16\u7801\u95ee\u9898\u3002text \u5b57\u6bb5\u5b58\u50a8\u6587\u672c\u6570\u636e\uff0c\u5176\u7f16\u7801\u9ed8\u8ba4\u4e3a latin1\uff0c\u8be5\u7f16\u7801\u4e0d\u5305\u542b\u4e2d\u6587\u6c49\u5b57\u3002\u56e0\u6b64\uff0c\u5728\u5c06\u4e2d\u6587\u6c49\u5b57\u63d2\u5165\u5230 text \u5b57\u6bb5\u4e2d\u65f6\uff0c\u5982\u679c\u672a\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\uff0c\u5c31\u4f1a\u51fa\u73b0\u4e71\u7801\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u5728\u63d2\u5165\u64cd\u4f5c\u4e2d\u6307\u5b9a\u6b63\u786e\u7684\u7f16\u7801\u3002\u5728 python \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a import mysql.connector # \u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5 conn = mysql.connector.connect( host=&#8221;localhost&#8221;, user=&#8221;username&#8221;, password=&#8221;password&#8221;, database=&#8221;database_name&#8221; ) # \u521b\u5efa\u6e38\u6807 cursor = conn.cursor() # \u63d2\u5165\u6570\u636e\uff0c\u5e76\u6307\u5b9a\u7f16\u7801\u4e3a utf8 query = &#8220;INSERT INTO table_name (message) VALUES (%s)&#8221; data = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-870","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=870"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/870\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}