{"id":1018986,"date":"2024-12-27T12:49:11","date_gmt":"2024-12-27T04:49:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1018986.html"},"modified":"2024-12-27T12:49:20","modified_gmt":"2024-12-27T04:49:20","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96mysql%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1018986.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6mysql\u4fe1\u606f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25161550\/99d25210-1d06-4e4d-8913-3c2ad0496548.webp\" alt=\"python\u5982\u4f55\u83b7\u53d6mysql\u4fe1\u606f\" \/><\/p>\n<p><p> \u8981\u5728Python\u4e2d\u83b7\u53d6MySQL\u6570\u636e\u5e93\u7684\u4fe1\u606f\uff0c<strong>\u53ef\u4ee5\u4f7f\u7528MySQL\u8fde\u63a5\u5668\u5e93\u3001\u6267\u884cSQL\u67e5\u8be2\u3001\u89e3\u6790\u67e5\u8be2\u7ed3\u679c\u3001\u5904\u7406\u6570\u636e\u5e93\u8fde\u63a5\u5f02\u5e38<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528MySQL\u8fde\u63a5\u5668\u5e93\u662f\u83b7\u53d6MySQL\u4fe1\u606f\u7684\u57fa\u7840\u6b65\u9aa4\u3002MySQL\u8fde\u63a5\u5668\u5e93\u662f\u4e00\u4e2a\u7528\u4e8e\u8fde\u63a5Python\u5e94\u7528\u7a0b\u5e8f\u548cMySQL\u6570\u636e\u5e93\u7684\u5e93\uff0c\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u901a\u8fc7Python\u4ee3\u7801\u4e0eMySQL\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528MySQL\u8fde\u63a5\u5668\u5e93\u9700\u8981\u5b89\u88c5\u76f8\u5173\u5e93\uff0c\u5982<code>mysql-connector-python<\/code>\u6216<code>PyMySQL<\/code>\u3002\u4ee5<code>mysql-connector-python<\/code>\u4e3a\u4f8b\uff0c\u5b89\u88c5\u5e93\u540e\uff0c\u9700\u8981\u901a\u8fc7\u4ee3\u7801\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93\u3002\u8fde\u63a5\u6210\u529f\u540e\uff0c\u53ef\u4ee5\u6267\u884cSQL\u67e5\u8be2\u4ee5\u83b7\u53d6\u6240\u9700\u7684\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u8fde\u63a5\u548c\u67e5\u8be2\u7684\u57fa\u672c\u6b65\u9aa4\uff1a<\/p>\n<\/p>\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=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;your_username&#39;,<\/p>\n<p>    password=&#39;your_password&#39;,<\/p>\n<p>    database=&#39;your_database&#39;<\/p>\n<p>)<\/p>\n<h2><strong>\u521b\u5efa\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = connection.cursor()<\/p>\n<h2><strong>\u6267\u884cSQL\u67e5\u8be2<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT * FROM your_table&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c<\/strong><\/h2>\n<p>results = cursor.fetchall()<\/p>\n<h2><strong>\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/strong><\/h2>\n<p>cursor.close()<\/p>\n<p>connection.close()<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>for row in results:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e00\u3001MYSQL\u8fde\u63a5\u5668\u5e93\u7684\u9009\u62e9<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8981\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93\uff0c\u6709\u591a\u4e2a\u8fde\u63a5\u5668\u5e93\u53ef\u4f9b\u9009\u62e9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u5e93\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>mysql-connector-python<\/strong>\uff1a\u8fd9\u662f\u5b98\u65b9\u63d0\u4f9b\u7684MySQL\u8fde\u63a5\u5668\uff0c\u652f\u6301\u4e0eMySQL\u6570\u636e\u5e93\u7684\u53cc\u5411\u901a\u4fe1\uff0c\u5177\u6709\u826f\u597d\u7684\u517c\u5bb9\u6027\u548c\u7a33\u5b9a\u6027\u3002\u5b83\u652f\u6301Python 3.x\uff0c\u5e76\u4e14\u5b89\u88c5\u548c\u4f7f\u7528\u76f8\u5bf9\u7b80\u5355\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>PyMySQL<\/strong>\uff1a\u8fd9\u662f\u4e00\u4e2a\u7eafPython\u5b9e\u73b0\u7684MySQL\u8fde\u63a5\u5668\uff0c\u9002\u7528\u4e8e\u4e0d\u5e0c\u671b\u5728\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4efb\u4f55C\u8bed\u8a00\u5e93\u7684\u573a\u666f\u3002PyMySQL\u7684\u4e00\u4e2a\u4f18\u52bf\u662f\u5176\u7eafPython\u5b9e\u73b0\uff0c\u4fbf\u4e8e\u8de8\u5e73\u53f0\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>MySQLdb<\/strong>\uff1a\u8fd9\u662f\u4e00\u4e2a\u57fa\u4e8eC\u8bed\u8a00\u7684MySQL\u8fde\u63a5\u5668\uff0c\u6027\u80fd\u8f83\u9ad8\uff0c\u4f46\u5728\u5b89\u88c5\u65f6\u9700\u8981\u7f16\u8bd1C\u6269\u5c55\uff0c\u53ef\u80fd\u5bf9\u67d0\u4e9b\u73af\u5883\u914d\u7f6e\u8981\u6c42\u8f83\u9ad8\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u8fde\u63a5\u5668\u5e93\u65f6\uff0c\u5e94\u6839\u636e\u9879\u76ee\u9700\u6c42\u3001\u6027\u80fd\u8981\u6c42\u3001\u5f00\u53d1\u73af\u5883\u7b49\u56e0\u7d20\u7efc\u5408\u8003\u8651\u3002\u901a\u5e38\uff0c<code>mysql-connector-python<\/code>\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u662fMySQL\u5b98\u65b9\u63d0\u4f9b\u7684\uff0c\u5e76\u4e14\u5bb9\u6613\u5b89\u88c5\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5<\/p>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5408\u9002\u7684\u8fde\u63a5\u5668\u5e93\u540e\uff0c\u4e0b\u4e00\u6b65\u662f\u5efa\u7acb\u4e0eMySQL\u6570\u636e\u5e93\u7684\u8fde\u63a5\u3002\u4ee5<code>mysql-connector-python<\/code>\u4e3a\u4f8b\uff0c\u5efa\u7acb\u8fde\u63a5\u901a\u5e38\u5305\u62ec\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u5e93<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u547d\u4ee4\u5b89\u88c5<code>mysql-connector-python<\/code>\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install mysql-connector-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5bfc\u5165\u5e93\u5e76\u5efa\u7acb\u8fde\u63a5<\/strong>\uff1a\u5728Python\u4ee3\u7801\u4e2d\uff0c\u5bfc\u5165<code>mysql.connector<\/code>\u6a21\u5757\uff0c\u5e76\u4f7f\u7528<code>connect<\/code>\u65b9\u6cd5\u5efa\u7acb\u8fde\u63a5\u3002<\/p>\n<\/p>\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=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;your_username&#39;,<\/p>\n<p>    password=&#39;your_password&#39;,<\/p>\n<p>    database=&#39;your_database&#39;<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u8fde\u63a5\u5f02\u5e38<\/strong>\uff1a\u5728\u5efa\u7acb\u8fde\u63a5\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u8fde\u63a5\u5931\u8d25\u7684\u60c5\u51b5\uff0c\u5982\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef\u3001\u6570\u636e\u5e93\u4e0d\u5b58\u5728\u7b49\u3002\u56e0\u6b64\uff0c\u5e94\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    connection = mysql.connector.connect(<\/p>\n<p>        host=&#39;localhost&#39;,<\/p>\n<p>        user=&#39;your_username&#39;,<\/p>\n<p>        password=&#39;your_password&#39;,<\/p>\n<p>        database=&#39;your_database&#39;<\/p>\n<p>    )<\/p>\n<p>except mysql.connector.Error as err:<\/p>\n<p>    print(f&quot;Error: {err}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u6267\u884cSQL\u67e5\u8be2<\/p>\n<\/p>\n<p><p>\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6267\u884cSQL\u67e5\u8be2\u6765\u83b7\u53d6\u6240\u9700\u7684\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u6267\u884cSQL\u67e5\u8be2\u7684\u57fa\u672c\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u6e38\u6807\u5bf9\u8c61<\/strong>\uff1a\u4f7f\u7528\u8fde\u63a5\u5bf9\u8c61\u7684<code>cursor<\/code>\u65b9\u6cd5\u521b\u5efa\u6e38\u6807\u5bf9\u8c61\u3002\u6e38\u6807\u7528\u4e8e\u6267\u884cSQL\u67e5\u8be2\u5e76\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor = connection.cursor()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6267\u884cSQL\u67e5\u8be2<\/strong>\uff1a\u4f7f\u7528\u6e38\u6807\u5bf9\u8c61\u7684<code>execute<\/code>\u65b9\u6cd5\u6267\u884cSQL\u67e5\u8be2\u3002\u53ef\u4ee5\u6267\u884c\u5404\u79cd\u7c7b\u578b\u7684\u67e5\u8be2\uff0c\u5982<code>SELECT<\/code>\u3001<code>INSERT<\/code>\u3001<code>UPDATE<\/code>\u3001<code>DELETE<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.execute(&quot;SELECT * FROM your_table&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c<\/strong>\uff1a\u5bf9\u4e8e<code>SELECT<\/code>\u67e5\u8be2\uff0c\u53ef\u4ee5\u4f7f\u7528\u6e38\u6807\u5bf9\u8c61\u7684<code>fetchall<\/code>\u65b9\u6cd5\u83b7\u53d6\u6240\u6709\u7ed3\u679c\uff0c\u6216\u4f7f\u7528<code>fetchone<\/code>\u65b9\u6cd5\u83b7\u53d6\u5355\u884c\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">results = cursor.fetchall()<\/p>\n<p>for row in results:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/strong>\uff1a\u67e5\u8be2\u5b8c\u6210\u540e\uff0c\u5173\u95ed\u6e38\u6807\u548c\u6570\u636e\u5e93\u8fde\u63a5\u4ee5\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.close()<\/p>\n<p>connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u89e3\u6790\u67e5\u8be2\u7ed3\u679c<\/p>\n<\/p>\n<p><p>\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c\u540e\uff0c\u9700\u8981\u5bf9\u7ed3\u679c\u8fdb\u884c\u89e3\u6790\u548c\u5904\u7406\uff0c\u4ee5\u4fbf\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u3002\u89e3\u6790\u7ed3\u679c\u7684\u65b9\u5f0f\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u7ed3\u6784\u548c\u5e94\u7528\u7a0b\u5e8f\u7684\u9700\u6c42\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u904d\u5386\u7ed3\u679c\u96c6<\/strong>\uff1a\u901a\u5e38\uff0c\u67e5\u8be2\u7ed3\u679c\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5143\u7ec4\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7ec4\u4ee3\u8868\u4e00\u884c\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u904d\u5386\u7ed3\u679c\u96c6\uff0c\u5e76\u8bbf\u95ee\u5143\u7ec4\u4e2d\u7684\u5404\u4e2a\u5b57\u6bb5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for row in results:<\/p>\n<p>    id = row[0]<\/p>\n<p>    name = row[1]<\/p>\n<p>    print(f&quot;ID: {id}, Name: {name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u5b57\u5178\u6e38\u6807<\/strong>\uff1a\u4e3a\u4e86\u66f4\u65b9\u4fbf\u5730\u8bbf\u95ee\u5b57\u6bb5\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6e38\u6807\u3002\u5b57\u5178\u6e38\u6807\u4f1a\u5c06\u6bcf\u884c\u7ed3\u679c\u8868\u793a\u4e3a\u5b57\u5178\uff0c\u5176\u4e2d\u5217\u540d\u4f5c\u4e3a\u952e\uff0c\u5217\u503c\u4f5c\u4e3a\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor = connection.cursor(dictionary=True)<\/p>\n<p>cursor.execute(&quot;SELECT * FROM your_table&quot;)<\/p>\n<p>results = cursor.fetchall()<\/p>\n<p>for row in results:<\/p>\n<p>    print(f&quot;ID: {row[&#39;id&#39;]}, Name: {row[&#39;name&#39;]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5904\u7406\u6570\u636e\u5e93\u8fde\u63a5\u5f02\u5e38<\/p>\n<\/p>\n<p><p>\u5728\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u8fde\u63a5\u5931\u8d25\u3001\u67e5\u8be2\u9519\u8bef\u3001\u8fde\u63a5\u8d85\u65f6\u7b49\u3002\u4e3a\u4e86\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u9700\u8981\u5bf9\u8fd9\u4e9b\u5f02\u5e38\u8fdb\u884c\u5904\u7406\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6355\u83b7\u8fde\u63a5\u5f02\u5e38<\/strong>\uff1a\u5728\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\u65f6\uff0c\u53ef\u80fd\u4f1a\u56e0\u7f51\u7edc\u95ee\u9898\u3001\u8ba4\u8bc1\u5931\u8d25\u7b49\u539f\u56e0\u5bfc\u81f4\u8fde\u63a5\u5f02\u5e38\u3002\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    connection = mysql.connector.connect(...)<\/p>\n<p>except mysql.connector.Error as err:<\/p>\n<p>    print(f&quot;Error: {err}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6355\u83b7\u67e5\u8be2\u5f02\u5e38<\/strong>\uff1a\u5728\u6267\u884cSQL\u67e5\u8be2\u65f6\uff0c\u53ef\u80fd\u4f1a\u56e0\u8bed\u6cd5\u9519\u8bef\u3001\u7ea6\u675f\u51b2\u7a81\u7b49\u539f\u56e0\u5bfc\u81f4\u67e5\u8be2\u5931\u8d25\u3002\u5e94\u5728\u6267\u884c\u67e5\u8be2\u65f6\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    cursor.execute(&quot;SELECT * FROM your_table&quot;)<\/p>\n<p>except mysql.connector.Error as err:<\/p>\n<p>    print(f&quot;Query Error: {err}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u786e\u4fdd\u8d44\u6e90\u91ca\u653e<\/strong>\uff1a\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u90fd\u5e94\u786e\u4fdd\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5\u4ee5\u91ca\u653e\u8d44\u6e90\u3002\u53ef\u4ee5\u4f7f\u7528<code>finally<\/code>\u5757\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    cursor = connection.cursor()<\/p>\n<p>    cursor.execute(&quot;SELECT * FROM your_table&quot;)<\/p>\n<p>except mysql.connector.Error as err:<\/p>\n<p>    print(f&quot;Error: {err}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    cursor.close()<\/p>\n<p>    connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u83b7\u53d6MySQL\u4fe1\u606f\u65f6\uff0c\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u7ef4\u62a4\u6027\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6<\/strong>\uff1a\u5c06\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\uff08\u5982\u4e3b\u673a\u540d\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u3001\u6570\u636e\u5e93\u540d\u7b49\uff09\u5b58\u50a8\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u786c\u7f16\u7801\u5728\u4ee3\u7801\u4e2d\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u5b89\u5168\u6027\uff0c\u5e76\u65b9\u4fbf\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u53c2\u6570\u5316\u67e5\u8be2<\/strong>\uff1a\u5728\u6267\u884cSQL\u67e5\u8be2\u65f6\uff0c\u4f7f\u7528\u53c2\u6570\u5316\u67e5\u8be2\u4ee5\u9632\u6b62SQL\u6ce8\u5165\u653b\u51fb\u3002\u53c2\u6570\u5316\u67e5\u8be2\u53ef\u4ee5\u81ea\u52a8\u5904\u7406\u7528\u6237\u8f93\u5165\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\uff0c\u4ece\u800c\u9632\u6b62\u6076\u610f\u6ce8\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">query = &quot;SELECT * FROM your_table WHERE id = %s&quot;<\/p>\n<p>cursor.execute(query, (id,))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u8fde\u63a5\u6c60<\/strong>\uff1a\u5728\u9ad8\u5e76\u53d1\u5e94\u7528\u4e2d\uff0c\u9891\u7e41\u5efa\u7acb\u548c\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u4f1a\u5bfc\u81f4\u6027\u80fd\u74f6\u9888\u3002\u4f7f\u7528\u8fde\u63a5\u6c60\u53ef\u4ee5\u590d\u7528\u8fde\u63a5\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528<code>mysql.connector.pooling<\/code>\u6a21\u5757\u521b\u5efa\u8fde\u63a5\u6c60\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5f02\u5e38\u65e5\u5fd7\u8bb0\u5f55<\/strong>\uff1a\u5728\u6355\u83b7\u5f02\u5e38\u65f6\uff0c\u8bb0\u5f55\u8be6\u7ec6\u7684\u9519\u8bef\u65e5\u5fd7\u4ee5\u4fbf\u540e\u7eed\u5206\u6790\u548c\u8c03\u8bd5\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>logging<\/code>\u6a21\u5757\u8bb0\u5f55\u65e5\u5fd7\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6b65\u9aa4\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u9ad8\u6548\u5730\u83b7\u53d6MySQL\u6570\u636e\u5e93\u7684\u4fe1\u606f\uff0c\u5e76\u786e\u4fdd\u4ee3\u7801\u7684\u5b89\u5168\u6027\u548c\u53ef\u7ef4\u62a4\u6027\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\u9700\u8981\u5b89\u88c5<code>mysql-connector-python<\/code>\u6216<code>PyMySQL<\/code>\u7b49\u5e93\u3002\u5b89\u88c5\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93\uff1a  <\/p>\n<pre><code class=\"language-python\">import mysql.connector\n\nconnection = mysql.connector.connect(\n    host=&#39;\u4f60\u7684\u4e3b\u673a\u540d&#39;,\n    user=&#39;\u4f60\u7684\u7528\u6237\u540d&#39;,\n    password=&#39;\u4f60\u7684\u5bc6\u7801&#39;,\n    database=&#39;\u4f60\u7684\u6570\u636e\u5e93\u540d&#39;\n)\n\ncursor = connection.cursor()\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728\u4f7f\u7528\u5b8c\u6570\u636e\u5e93\u540e\u5173\u95ed\u8fde\u63a5\uff0c\u4ee5\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<p><strong>Python\u83b7\u53d6MySQL\u6570\u636e\u7684\u5e38\u7528\u65b9\u6cd5\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6e38\u6807\u5bf9\u8c61\u7684<code>execute()<\/code>\u65b9\u6cd5\u6765\u6267\u884cSQL\u67e5\u8be2\uff0c\u968f\u540e\u901a\u8fc7<code>fetchall()<\/code>\u6216<code>fetchone()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u83b7\u53d6\u8868\u4e2d\u7684\u6240\u6709\u6570\u636e\uff1a  <\/p>\n<pre><code class=\"language-python\">cursor.execute(&quot;SELECT * FROM \u8868\u540d&quot;)\n\u7ed3\u679c = cursor.fetchall()\n\nfor \u884c in \u7ed3\u679c:\n    print(\u884c)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u76f4\u89c2\uff0c\u9002\u5408\u83b7\u53d6\u5c11\u91cf\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u4e0eMySQL\u7684\u5f02\u5e38\u548c\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u8fde\u63a5MySQL\u65f6\uff0c\u5904\u7406\u5f02\u5e38\u975e\u5e38\u91cd\u8981\u3002\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u6765\u6355\u6349\u8fde\u63a5\u9519\u8bef\u6216\u67e5\u8be2\u9519\u8bef\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">try:\n    connection = mysql.connector.connect(...)\n    cursor = connection.cursor()\n    cursor.execute(&quot;SELECT * FROM \u8868\u540d&quot;)\nexcept mysql.connector.Error as err:\n    print(f&quot;\u9519\u8bef: {err}&quot;)\nfinally:\n    if connection.is_connected():\n        cursor.close()\n        connection.close()\n<\/code><\/pre>\n<p>\u8fd9\u6837\u7684\u9519\u8bef\u5904\u7406\u65b9\u5f0f\u6709\u52a9\u4e8e\u786e\u4fdd\u7a0b\u5e8f\u5728\u51fa\u73b0\u95ee\u9898\u65f6\u4e0d\u4f1a\u5d29\u6e83\uff0c\u5e76\u80fd\u8fdb\u884c\u76f8\u5e94\u7684\u8c03\u8bd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u83b7\u53d6MySQL\u6570\u636e\u5e93\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528MySQL\u8fde\u63a5\u5668\u5e93\u3001\u6267\u884cSQL\u67e5\u8be2\u3001\u89e3\u6790\u67e5\u8be2\u7ed3\u679c\u3001\u5904\u7406\u6570 [&hellip;]","protected":false},"author":3,"featured_media":1018999,"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\/1018986"}],"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=1018986"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1018986\/revisions"}],"predecessor-version":[{"id":1019000,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1018986\/revisions\/1019000"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1018999"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1018986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1018986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1018986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}