{"id":946121,"date":"2024-12-26T23:35:31","date_gmt":"2024-12-26T15:35:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/946121.html"},"modified":"2024-12-26T23:35:33","modified_gmt":"2024-12-26T15:35:33","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8mysql","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/946121.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528mysql"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082432\/b8205a03-8440-4173-ad9c-e4e5d5dde800.webp\" alt=\"python\u5982\u4f55\u8c03\u7528mysql\" \/><\/p>\n<p><p> Python\u8c03\u7528MySQL\u7684\u8fc7\u7a0b\u53ef\u4ee5\u901a\u8fc7<strong>\u5b89\u88c5MySQL\u9a71\u52a8\u3001\u8fde\u63a5MySQL\u6570\u636e\u5e93\u3001\u6267\u884cSQL\u8bed\u53e5\u3001\u5904\u7406\u7ed3\u679c\u96c6<\/strong>\u8fd9\u51e0\u4e2a\u6b65\u9aa4\u6765\u5b9e\u73b0\u3002\u5176\u4e2d\uff0c\u5b89\u88c5MySQL\u9a71\u52a8\u662f\u6700\u4e3a\u5173\u952e\u7684\u4e00\u6b65\uff0c\u56e0\u4e3aPython\u4e0eMySQL\u4e4b\u95f4\u7684\u4ea4\u4e92\u9700\u8981\u901a\u8fc7\u9a71\u52a8\u6765\u5b9e\u73b0\u3002\u901a\u5e38\u4f7f\u7528\u7684MySQL\u9a71\u52a8\u6709MySQL Connector\u3001PyMySQL\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\u6765\u8c03\u7528MySQL\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5MySQL\u9a71\u52a8<\/p>\n<\/p>\n<p><p>\u8981\u5728Python\u4e2d\u4e0eMySQL\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u4e00\u4e2aMySQL\u9a71\u52a8\u3002Python\u793e\u533a\u63d0\u4f9b\u4e86\u591a\u79cd\u9a71\u52a8\u7a0b\u5e8f\u4f9b\u9009\u62e9\uff0c\u4e0b\u9762\u4ecb\u7ecd\u51e0\u79cd\u5e38\u7528\u7684\u9a71\u52a8\u4ee5\u53ca\u5982\u4f55\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>MySQL Connector<\/strong><\/li>\n<\/ol>\n<p><p>MySQL Connector\u662fOracle\u5b98\u65b9\u63d0\u4f9b\u7684\u9a71\u52a8\uff0c\u5b89\u88c5\u7b80\u5355\u4e14\u517c\u5bb9\u6027\u597d\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install mysql-connector-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>PyMySQL<\/strong><\/li>\n<\/ol>\n<p><p>PyMySQL\u662f\u4e00\u4e2a\u7eafPython\u5b9e\u73b0\u7684MySQL\u9a71\u52a8\uff0c\u9002\u7528\u4e8e\u4e0d\u9700\u8981C\u6269\u5c55\u7684\u73af\u5883\u3002\u5b89\u88c5\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pymysql<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>MySQLdb<\/strong><\/li>\n<\/ol>\n<p><p>MySQLdb\u662f\u4e00\u4e2aC\u8bed\u8a00\u5b9e\u73b0\u7684\u9a71\u52a8\uff0c\u6027\u80fd\u8f83\u9ad8\uff0c\u4f46\u5728Windows\u5e73\u53f0\u4e0b\u9700\u8981\u989d\u5916\u7684\u7f16\u8bd1\u6b65\u9aa4\u3002\u5b89\u88c5\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install mysqlclient<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u8fde\u63a5MySQL\u6570\u636e\u5e93<\/p>\n<\/p>\n<p><p>\u5b89\u88c5\u597d\u9a71\u52a8\u540e\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7Python\u4ee3\u7801\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528MySQL Connector\u548cPyMySQL\u8fde\u63a5\u6570\u636e\u5e93\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528MySQL Connector\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<h2><strong>\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>connection = mysql.connector.connect(<\/p>\n<p>    host=&quot;localhost&quot;,<\/p>\n<p>    user=&quot;yourusername&quot;,<\/p>\n<p>    password=&quot;yourpassword&quot;,<\/p>\n<p>    database=&quot;yourdatabase&quot;<\/p>\n<p>)<\/p>\n<h2><strong>\u521b\u5efa\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = connection.cursor()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528PyMySQL\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<h2><strong>\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>connection = pymysql.connect(<\/p>\n<p>    host=&quot;localhost&quot;,<\/p>\n<p>    user=&quot;yourusername&quot;,<\/p>\n<p>    password=&quot;yourpassword&quot;,<\/p>\n<p>    database=&quot;yourdatabase&quot;<\/p>\n<p>)<\/p>\n<h2><strong>\u521b\u5efa\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = connection.cursor()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u6267\u884cSQL\u8bed\u53e5<\/p>\n<\/p>\n<p><p>\u8fde\u63a5\u6210\u529f\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6e38\u6807\u5bf9\u8c61\u6267\u884cSQL\u8bed\u53e5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684SQL\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u67e5\u8be2\u6570\u636e<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u6267\u884cSQL\u67e5\u8be2<\/p>\n<p>cursor.execute(&quot;SELECT * FROM yourtable&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c<\/strong><\/h2>\n<p>results = cursor.fetchall()<\/p>\n<h2><strong>\u5904\u7406\u7ed3\u679c\u96c6<\/strong><\/h2>\n<p>for row in results:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u63d2\u5165\u6570\u636e<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># SQL\u63d2\u5165\u8bed\u53e5<\/p>\n<p>sql = &quot;INSERT INTO yourtable (column1, column2) VALUES (%s, %s)&quot;<\/p>\n<p>values = (&quot;value1&quot;, &quot;value2&quot;)<\/p>\n<h2><strong>\u6267\u884c\u63d2\u5165\u64cd\u4f5c<\/strong><\/h2>\n<p>cursor.execute(sql, values)<\/p>\n<h2><strong>\u63d0\u4ea4\u5230\u6570\u636e\u5e93\u6267\u884c<\/strong><\/h2>\n<p>connection.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u66f4\u65b0\u6570\u636e<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># SQL\u66f4\u65b0\u8bed\u53e5<\/p>\n<p>sql = &quot;UPDATE yourtable SET column1 = %s WHERE column2 = %s&quot;<\/p>\n<p>values = (&quot;new_value&quot;, &quot;condition_value&quot;)<\/p>\n<h2><strong>\u6267\u884c\u66f4\u65b0\u64cd\u4f5c<\/strong><\/h2>\n<p>cursor.execute(sql, values)<\/p>\n<h2><strong>\u63d0\u4ea4\u5230\u6570\u636e\u5e93\u6267\u884c<\/strong><\/h2>\n<p>connection.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u5220\u9664\u6570\u636e<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># SQL\u5220\u9664\u8bed\u53e5<\/p>\n<p>sql = &quot;DELETE FROM yourtable WHERE column = %s&quot;<\/p>\n<p>value = (&quot;condition_value&quot;,)<\/p>\n<h2><strong>\u6267\u884c\u5220\u9664\u64cd\u4f5c<\/strong><\/h2>\n<p>cursor.execute(sql, value)<\/p>\n<h2><strong>\u63d0\u4ea4\u5230\u6570\u636e\u5e93\u6267\u884c<\/strong><\/h2>\n<p>connection.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u7ed3\u679c\u96c6<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u67e5\u8be2\u64cd\u4f5c\uff0c\u901a\u5e38\u9700\u8981\u5904\u7406\u7ed3\u679c\u96c6\u3002\u53ef\u4ee5\u901a\u8fc7<code>fetchall()<\/code>\u65b9\u6cd5\u83b7\u53d6\u6240\u6709\u7ed3\u679c\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>fetchone()<\/code>\u65b9\u6cd5\u9010\u884c\u83b7\u53d6\u7ed3\u679c\u3002\u5904\u7406\u7ed3\u679c\u96c6\u7684\u5173\u952e\u5728\u4e8e\u7406\u89e3\u7ed3\u679c\u96c6\u7684\u7ed3\u6784\uff0c\u4f8b\u5982\u884c\u6570\u3001\u5217\u6570\u53ca\u5176\u7c7b\u578b\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u9010\u884c\u5904\u7406\u7ed3\u679c<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u6267\u884cSQL\u67e5\u8be2<\/p>\n<p>cursor.execute(&quot;SELECT * FROM yourtable&quot;)<\/p>\n<h2><strong>\u9010\u884c\u83b7\u53d6\u7ed3\u679c<\/strong><\/h2>\n<p>row = cursor.fetchone()<\/p>\n<p>while row:<\/p>\n<p>    print(row)<\/p>\n<p>    row = cursor.fetchone()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u83b7\u53d6\u5217\u4fe1\u606f<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5904\u7406\u7ed3\u679c\u96c6\u65f6\uff0c\u6709\u65f6\u9700\u8981\u4e86\u89e3\u5217\u7684\u4fe1\u606f\u3002\u53ef\u4ee5\u901a\u8fc7\u6e38\u6807\u5bf9\u8c61\u7684<code>description<\/code>\u5c5e\u6027\u83b7\u53d6\u5217\u540d\u548c\u5217\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6267\u884cSQL\u67e5\u8be2<\/p>\n<p>cursor.execute(&quot;SELECT * FROM yourtable&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u5217\u4fe1\u606f<\/strong><\/h2>\n<p>columns = cursor.description<\/p>\n<p>for column in columns:<\/p>\n<p>    print(column[0])  # \u6253\u5370\u5217\u540d<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5173\u95ed\u8fde\u63a5<\/p>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u6570\u636e\u5e93\u64cd\u4f5c\u540e\uff0c\u8bb0\u5f97\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5\uff0c\u4ee5\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5173\u95ed\u6e38\u6807<\/p>\n<p>cursor.close()<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\uff0cPython\u8c03\u7528MySQL\u6570\u636e\u5e93\u7684\u8fc7\u7a0b\u662f\u901a\u8fc7\u5b89\u88c5\u5408\u9002\u7684MySQL\u9a71\u52a8\uff0c\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u6267\u884cSQL\u8bed\u53e5\uff0c\u5e76\u5904\u7406\u7ed3\u679c\u96c6\u6765\u5b9e\u73b0\u7684\u3002\u5728\u5904\u7406\u6570\u636e\u5e93\u8fde\u63a5\u65f6\uff0c\u6ce8\u610f\u7ba1\u7406\u8fde\u63a5\u6c60\u53ca\u5f02\u5e38\u5904\u7406\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u6027\u80fd\u3002\u901a\u8fc7\u719f\u7ec3\u638c\u63e1\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u9ad8\u6548\u5730\u5229\u7528Python\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fde\u63a5MySQL\u6570\u636e\u5e93\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u8fde\u63a5MySQL\u6570\u636e\u5e93\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>mysql-connector-python<\/code>\u5e93\u3002\u9996\u5148\uff0c\u901a\u8fc7<code>pip install mysql-connector-python<\/code>\u5b89\u88c5\u8be5\u5e93\u3002\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fdb\u884c\u8fde\u63a5\uff1a  <\/p>\n<pre><code class=\"language-python\">import mysql.connector\n\ndb = mysql.connector.connect(\n    host=&quot;localhost&quot;,\n    user=&quot;yourusername&quot;,\n    password=&quot;yourpassword&quot;,\n    database=&quot;yourdatabase&quot;\n)\n\ncursor = db.cursor()\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5c06<code>host<\/code>\u3001<code>user<\/code>\u3001<code>password<\/code>\u548c<code>database<\/code>\u66ff\u6362\u4e3a\u60a8\u7684MySQL\u6570\u636e\u5e93\u7684\u76f8\u5e94\u4fe1\u606f\u3002<\/p>\n<p><strong>Python\u4e0eMySQL\u4ea4\u4e92\u65f6\uff0c\u5982\u4f55\u6267\u884cSQL\u67e5\u8be2\uff1f<\/strong><br \/>\u5728\u8fde\u63a5\u5230MySQL\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6e38\u6807\u5bf9\u8c61<code>cursor<\/code>\u6765\u6267\u884cSQL\u67e5\u8be2\u3002\u4f7f\u7528<code>cursor.execute()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8fd0\u884cSQL\u8bed\u53e5\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">cursor.execute(&quot;SELECT * FROM your_table&quot;)\nresults = cursor.fetchall()\nfor row in results:\n    print(row)\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u5c06\u4ece\u6307\u5b9a\u7684\u8868\u4e2d\u68c0\u7d22\u6240\u6709\u6570\u636e\u5e76\u9010\u884c\u6253\u5370\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406MySQL\u67e5\u8be2\u7684\u5f02\u5e38\uff1f<\/strong><br \/>\u5904\u7406\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\u7684\u5f02\u5e38\u662f\u786e\u4fdd\u7a0b\u5e8f\u7a33\u5b9a\u8fd0\u884c\u7684\u91cd\u8981\u90e8\u5206\u3002\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u5757\u6765\u6355\u6349\u5e76\u5904\u7406\u5f02\u5e38\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">try:\n    cursor.execute(&quot;SELECT * FROM non_existent_table&quot;)\nexcept mysql.connector.Error as err:\n    print(f&quot;Error: {err}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u60a8\u8bc6\u522b\u548c\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u4f8b\u5982\u8868\u4e0d\u5b58\u5728\u6216\u8fde\u63a5\u5931\u8d25\u7b49\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8c03\u7528MySQL\u7684\u8fc7\u7a0b\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5MySQL\u9a71\u52a8\u3001\u8fde\u63a5MySQL\u6570\u636e\u5e93\u3001\u6267\u884cSQL\u8bed\u53e5\u3001\u5904\u7406\u7ed3\u679c\u96c6\u8fd9 [&hellip;]","protected":false},"author":3,"featured_media":946123,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/946121"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=946121"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/946121\/revisions"}],"predecessor-version":[{"id":946125,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/946121\/revisions\/946125"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/946123"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=946121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=946121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=946121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}