{"id":1118,"date":"2022-04-04T19:54:26","date_gmt":"2022-04-04T11:54:26","guid":{"rendered":"https:\/\/fireinsect.top\/?p=1118"},"modified":"2022-05-29T21:19:11","modified_gmt":"2022-05-29T13:19:11","slug":"python-mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89","status":"publish","type":"post","link":"https:\/\/fireinsect.top\/index.php\/2022\/04\/04\/python-mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89\/","title":{"rendered":"Python Mysql\u6570\u636e\u5e93\u57fa\u7840"},"content":{"rendered":"\n<p>\u5728python\u7684\u4f7f\u7528\u4e2d\uff0c\u6211\u4eec\u4e0d\u53ef\u907f\u514d\u9700\u8981\u4f7f\u7528\u6570\u636e\u5e93\u6765\u5b8c\u6210\u6570\u636e\u7684\u5b58\u50a8\u64cd\u4f5c\u3002python\u57fa\u4e8e\u5e9e\u5927\u7684\u5e93\uff0c\u80fd\u8fc7\u8f7b\u677e\u8fdb\u884c\u6570\u636e\u5e93\u7684\u4f7f\u7528\u4ee5\u53ca\u8868\u7684\u5404\u7c7b\u64cd\u4f5c\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">\u6570\u636e\u5e93\u7684\u8fde\u63a5<\/p>\n\n\n\n<p>\u8fdb\u884cmysql\u6570\u636e\u5e93\u7684\u8fde\u63a5\uff0c\u6211\u4eec\u9700\u8981python\u5e93\u7684\u652f\u6301\uff0c\u8f93\u5165\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">pip install mysql.connector<\/code><\/pre>\n\n\n\n<p>\u6765\u5b89\u88c5python\u7684mysql\u8fde\u63a5\u5e93<\/p>\n\n\n\n<p>\u4e4b\u540e\uff0c\u901a\u8fc7\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">import mysql.connector\n\nmydb = mysql.connector.connect(\nhost=\"localhost\", #\u9ed8\u8ba4\u7528\u4e3b\u673a\u540d\nuser=\"root\", #\u9ed8\u8ba4\u7528\u6237\u540d\npassword=\"123456\" #mysql\u5bc6\u7801\n,charset='utf8' #\u7f16\u7801\u65b9\u5f0f\n)<\/code><\/pre>\n\n\n\n<p>\u6765\u5b8c\u6210\u6570\u636e\u5e93\u7684\u8fde\u63a5<\/p>\n\n\n\n<div style=\"height:21px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">\u8868\u7684\u521b\u5efa<\/p>\n\n\n\n<p>\u5f53\u6211\u4eec\u5b8c\u6210\u6570\u636e\u5e93\u7684\u8fde\u63a5\u540e\u5c31\u53ef\u4ee5\u5b9e\u73b0\u7b80\u5355\u7684\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u6bd4\u5982\u521b\u5efa\u8868<\/p>\n\n\n\n<p>\u5728\u8fd9\u4e4b\u524d\uff0c\u6211\u4eec\u4f7f\u7528connect\u65b9\u6cd5\u8fde\u63a5\u4e86\u6570\u636e\u5e93\uff0c\u521b\u5efa\u7684\u8fde\u63a5\u5bf9\u8c61mydb\uff0c\u4e4b\u540e\u6211\u4eec\u9700\u8981\u4f7f\u7528cursor()\u65b9\u6cd5\u6765\u521b\u5efa\u64cd\u4f5c\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528execute()\u65b9\u6cd5\u6765\u6267\u884c\u64cd\u4f5c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">\nimport mysql.connector\n\nmydb = mysql.connector.connect(\n    host=\"localhost\",  # \u9ed8\u8ba4\u7528\u4e3b\u673a\u540d\n    user=\"root\",  # \u9ed8\u8ba4\u7528\u6237\u540d\n    password=\"123456\",  # mysql\u5bc6\u7801\n    charset='utf8',  # \u7f16\u7801\u65b9\u5f0f\n    database=\"chuan\"  # \u6570\u636e\u5e93\u540d\u79f0\n)\n\nmycursor = mydb.cursor()\n\nmycursor.execute(\"CREATE TABLE user (name VARCHAR(255), address VARCHAR(255))\")\n<\/code><\/pre>\n\n\n\n<p>\u4e4b\u540e\u4f7f\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">mycursor.execute(\"SHOW TABLES\")\nfor x in mycursor:\n    print(x)<\/code><\/pre>\n\n\n\n<p>\u6765\u67e5\u770b\u8868\u662f\u5426\u5b58\u5728<\/p>\n\n\n\n<div style=\"height:33px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">\u8868\u7684\u63d2\u5165<\/p>\n\n\n\n<p>\u5982\u540c\u5e73\u65f6\u7684sql\u8bed\u53e5\uff0cpython\u4e5f\u4f7f\u7528insert into \u8bed\u53e5\u6765\u5b8c\u6210\u6570\u636e\u63d2\u5165<\/p>\n\n\n\n<p class=\"has-medium-font-size\">\u5355\u6570\u636e\u63d2\u5165<\/p>\n\n\n\n<p>\u5728\u521b\u5efa\u597d\u6570\u636e\u5e93\u64cd\u4f5c\u5bf9\u8c61\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49sql\u8bed\u53e5\u5bf9\u8c61\u548c\u5185\u5bb9\u586b\u5145\u5143\u7ec4val\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">import mysql.connector\n\nmydb = mysql.connector.connect(\n    host=\"localhost\",  # \u9ed8\u8ba4\u7528\u4e3b\u673a\u540d\n    user=\"root\",  # \u9ed8\u8ba4\u7528\u6237\u540d\n    password=\"123456\",  # mysql\u5bc6\u7801\n    charset='utf8',  # \u7f16\u7801\u65b9\u5f0f\n    database=\"chuan\"  # \u6570\u636e\u5e93\u540d\u79f0\n)\n\nmycursor = mydb.cursor()\n\nsql = \"INSERT INTO user (name, address) VALUES (%s, %s)\" #sql\u8bed\u53e5\nval = (\"\u5ddd\u5ddd\", \"\u4e0a\u6d77\u4ea4\u5927\") #\u63d2\u5165\u7684\u5143\u7ec4\nmycursor.execute(sql, val) #\u6267\u884c\u8bed\u53e5\n\nmydb.commit() #\u6570\u636e\u8868\u5185\u5bb9\u6709\u66f4\u65b0\uff0c\u5fc5\u987b\u4f7f\u7528\u5230\u8be5\u8bed\u53e5\n\nprint(mycursor.rowcount, \"\u6dfb\u52a0\u8868\u683c\u6210\u529f.\") #\u6253\u5370\u66f4\u65b0\u6570\u636e\u6570\u91cf\u548c\u4fe1\u606f\n\n<\/code><\/pre>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u6570\u636e\u5e93\u5185\u5bb9\u5b58\u5728\u53d8\u5316\uff0c\u9700\u8981\u6267\u884c\u6570\u636e\u5e93\u8fde\u63a5\u5bf9\u8c61\u7684commit()\u65b9\u6cd5\u6765\u66f4\u65b0\u8fde\u63a5\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-medium-font-size\">\u5f53\u9700\u8981\u63d2\u5165\u591a\u4e2a\u6570\u636e\u65f6<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u6765\u5305\u542b\u5143\u7ec4\u4f7f\u7528executemany()\u65b9\u6cd5\u6765\u6267\u884c\u8bed\u53e5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">\nimport mysql.connector\n\nmydb = mysql.connector.connect(\n    host=\"localhost\",  # \u9ed8\u8ba4\u7528\u4e3b\u673a\u540d\n    user=\"root\",  # \u9ed8\u8ba4\u7528\u6237\u540d\n    password=\"123456\",  # mysql\u5bc6\u7801\n    charset='utf8',  # \u7f16\u7801\u65b9\u5f0f\n    database=\"chuan\"  # \u6570\u636e\u5e93\u540d\u79f0\n)\n\nmycursor = mydb.cursor()\n\nsql = \"INSERT INTO user (name, address) VALUES (%s, %s)\"\nval = [\n  ('Peter', 'Lowstreet 4'),\n  ('Amy', 'Apple st 652'),\n  ('Hannah', 'Mountain 21'),\n  ('Michael', 'Valley 345'),\n  ('Sandy', 'Ocean blvd 2'),\n  ('Betty', 'Green Grass 1'),\n  ('Richard', 'Sky st 331'),\n  ('Susan', 'One way 98'),\n  ('Vicky', 'Yellow Garden 2'),\n  ('Ben', 'Park Lane 38'),\n  ('William', 'Central st 954'),\n  ('Chuck', 'Main Road 989'),\n  ('Viola', 'Sideway 1633')\n]\n\nmycursor.executemany(sql, val)\n\nmydb.commit()\n\nprint(mycursor.rowcount, \"\u5168\u90e8\u6dfb\u52a0\u6210\u529f.\")\n<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u4e0d\u5b58\u5728\u8fd9\u4e2a\u8868\uff0c\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size\">\u83b7\u53d6\u63d2\u5165\u7684id<\/p>\n\n\n\n<p>\u4f7f\u7528 \u83b7\u53d6\u64cd\u4f5c\u5bf9\u8c61\u7684lastrowid\u6765\u83b7\u53d6id<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">print(\"\u8bb0\u5f55\u4e00\u4e2a\u63d2\u5165\u7684id:\", mycursor.lastrowid)\n<\/code><\/pre>\n\n\n\n<p>\u5f53\u7136\u88ab\u63d2\u5165\u7684\u8868\u7684id\u5fc5\u987b\u662fAUTO_INCREMENT\uff0ccursor.lastrowid\u624d\u80fd\u83b7\u53d6\u5230id\u503c\uff0c\u56e0\u4e3a\u53ea\u6709\u81ea\u52a8\u521b\u5efa\u7684id\u624d\u80fd\u83b7\u53d6id\u503c\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u63d2\u5165\u591a\u884c\u6570\u636e\uff0c\u5219\u8fd4\u56de\u6700\u540e\u63d2\u5165\u884c\u7684id<\/p>\n\n\n\n<div style=\"height:43px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">\u6570\u636e\u9009\u62e9<\/p>\n\n\n\n<p>\u4f7f\u7528select\u8bed\u53e5\u6765\u5bf9\u6570\u636e\u8fdb\u884c\u9009\u62e9\uff08\u8be6\u7ec6\u89c1sql\u8bed\u53e5\u8bed\u6cd5\uff09<\/p>\n\n\n\n<p>\u6211\u4eec\u4f7f\u7528\u64cd\u4f5c\u5bf9\u8c61\u7684 fetchall()\u65b9\u6cd5\u6765\u83b7\u53d6\u4e00\u4e2a\u7ed3\u679c\u5bf9\u8c61\uff08fetchall\u8868\u793a\u83b7\u53d6\u9009\u62e9\u7684\u6240\u6709\u8bb0\u5f55\uff09\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">import mysql.connector\n\nmydb = mysql.connector.connect(\n    host=\"localhost\",  # \u9ed8\u8ba4\u7528\u4e3b\u673a\u540d\n    user=\"root\",  # \u9ed8\u8ba4\u7528\u6237\u540d\n    password=\"123456\",  # mysql\u5bc6\u7801\n    charset='utf8',  # \u7f16\u7801\u65b9\u5f0f\n    database=\"chuan\"  # \u6570\u636e\u5e93\u540d\u79f0\n)\n\nmycursor = mydb.cursor()\n\nmycursor.execute(\"SELECT name FROM user\")\n\nmyresult = mycursor.fetchall() #fetchall()\u83b7\u53d6\u6240\u6709\u8bb0\u5f55\n\nfor x in myresult:\n  print(x)\n<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u53ea\u5bf9\u4e00\u884c\u611f\u5174\u8da3\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528fetchone()\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u53ea\u8fd4\u56de\u7ed3\u679c\u7684\u7b2c\u4e00\u884c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">import mysql.connector\n\nmydb = mysql.connector.connect(\n    host=\"localhost\",  # \u9ed8\u8ba4\u7528\u4e3b\u673a\u540d\n    user=\"root\",  # \u9ed8\u8ba4\u7528\u6237\u540d\n    password=\"123456\",  # mysql\u5bc6\u7801\n    charset='utf8',  # \u7f16\u7801\u65b9\u5f0f\n    database=\"chuan\"  # \u6570\u636e\u5e93\u540d\u79f0\n)\n\nmycursor = mydb.cursor()\n\nmycursor.execute(\"SELECT * FROM user\")\n\nmyresult = mycursor.fetchone()\n\nprint(myresult)\n<\/code><\/pre>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">\u9632\u6b62sql\u6ce8\u5165<\/p>\n\n\n\n<p>\u5f53\u901a\u8fc7\u67e5\u8be2\u503c\u662f\uff0c\u5e94\u8be5\u5bf9\u8fd9\u4e9b\u503c\u8fdb\u884c\u8f6c\u4e49\uff0c\u4f7f\u7528 %s\u5360\u4f4d\u7b26\u6765\u8f6c\u4e49\u67e5\u8be2\u503c:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">import mysql.connector\n\nmydb = mysql.connector.connect(\n  host=\"localhost\",  # \u9ed8\u8ba4\u7528\u4e3b\u673a\u540d\n    user=\"root\",  # \u9ed8\u8ba4\u7528\u6237\u540d\n    password=\"123456\",  # mysql\u5bc6\u7801\n    charset='utf8',  # \u7f16\u7801\u65b9\u5f0f\n    database=\"chuan\"  # \u6570\u636e\u5e93\u540d\u79f0\n)\n\nmycursor = mydb.cursor()\n\nsql = \"SELECT * FROM user WHERE address = %s\" #\u8fd9\u91cc\u8fdb\u884c\u4e86\u8f6c\u4e49\nadr = (\"Yellow Garden 2\", )\n\nmycursor.execute(sql, adr)\n\nmyresult = mycursor.fetchall()\n\nfor x in myresult:\n  print(x)<\/code><\/pre>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">\u6570\u636e\u66f4\u65b0<\/p>\n\n\n\n<p>\u6570\u636e\u66f4\u65b0\u65b9\u6cd5\u540c\u7406\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">import mysql.connector\n\nmydb = mysql.connector.connect(\n    host=\"localhost\",  # \u9ed8\u8ba4\u7528\u4e3b\u673a\u540d\n    user=\"root\",  # \u9ed8\u8ba4\u7528\u6237\u540d\n    password=\"123456\",  # mysql\u5bc6\u7801\n    charset='utf8',  # \u7f16\u7801\u65b9\u5f0f\n    database=\"chuan\"  # \u6570\u636e\u5e93\u540d\u79f0\n)\n\nmycursor = mydb.cursor()\n\nsql = \"UPDATE user SET address = %s WHERE address = %s\"\nval = (\"Valley 345\", \"Canyon 123\")\n\nmycursor.execute(sql, val)\n\nmydb.commit()\n\nprint(mycursor.rowcount, \"\u66f4\u65b0\u6210\u529f\")\n<\/code><\/pre>\n\n\n\n<p>\u4f9d\u7136\u9700\u8981commit\u65b9\u6cd5\u6765\u66f4\u65b0\u8fde\u63a5\u5bf9\u8c61\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728python\u7684\u4f7f\u7528\u4e2d\uff0c\u6211\u4eec\u4e0d\u53ef\u907f\u514d\u9700\u8981\u4f7f\u7528\u6570\u636e\u5e93\u6765\u5b8c\u6210\u6570\u636e\u7684\u5b58\u50a8\u64cd\u4f5c\u3002python\u57fa\u4e8e\u5e9e\u5927\u7684\u5e93\uff0c\u80fd\u8fc7\u8f7b\u677e\u8fdb\u884c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-1118","post","type-post","status-publish","format-standard","hentry","category-6","category-wcd"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1118"}],"collection":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/comments?post=1118"}],"version-history":[{"count":5,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1118\/revisions"}],"predecessor-version":[{"id":1212,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1118\/revisions\/1212"}],"wp:attachment":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/media?parent=1118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/categories?post=1118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/tags?post=1118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}