{"id":977533,"date":"2024-12-27T06:30:23","date_gmt":"2024-12-26T22:30:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/977533.html"},"modified":"2024-12-27T06:30:25","modified_gmt":"2024-12-26T22:30:25","slug":"python%e5%a6%82%e4%bd%95%e4%b8%8emysql%e8%bf%9e%e6%8e%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/977533.html","title":{"rendered":"python\u5982\u4f55\u4e0emysql\u8fde\u63a5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24202805\/467136b2-203d-427f-a899-f10bc871d3f0.webp\" alt=\"python\u5982\u4f55\u4e0emysql\u8fde\u63a5\" \/><\/p>\n<p><p> <strong>Python\u4e0eMySQL\u7684\u8fde\u63a5\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528MySQL Connector\u3001PyMySQL\u3001SQLAlchemy\u7b49\u5e93\u3002<\/strong>\u5176\u4e2d\uff0c<strong>MySQL Connector<\/strong>\u662f\u5b98\u65b9\u63d0\u4f9b\u7684\u5e93\uff0c\u5177\u6709\u826f\u597d\u7684\u517c\u5bb9\u6027\u548c\u6027\u80fd\uff1b<strong>PyMySQL<\/strong>\u662f\u4e00\u4e2a\u7eafPython\u5b9e\u73b0\u7684MySQL\u5ba2\u6237\u7aef\uff0c\u9002\u5408\u5728\u4e0d\u652f\u6301C\u6269\u5c55\u7684\u73af\u5883\u4e2d\u4f7f\u7528\uff1b<strong>SQLAlchemy<\/strong>\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684ORM\u6846\u67b6\uff0c\u9002\u7528\u4e8e\u590d\u6742\u9879\u76ee\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u7ba1\u7406\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528MySQL Connector\u6765\u5b9e\u73b0Python\u4e0eMySQL\u7684\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5MySQL Connector<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528MySQL Connector\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u76f8\u5173\u7684\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\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<p><p>\u8be5\u547d\u4ee4\u4f1a\u81ea\u52a8\u4e0b\u8f7d\u5e76\u5b89\u88c5MySQL Connector\u5e93\u3002\u5982\u679c\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u9047\u5230\u95ee\u9898\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5347\u7ea7pip\u6216\u8005\u68c0\u67e5\u7f51\u7edc\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u8fde\u63a5MySQL\u6570\u636e\u5e93\u7684\u7b2c\u4e00\u6b65\u662f\u521b\u5efa\u4e00\u4e2a\u8fde\u63a5\u5bf9\u8c61\u3002\u53ef\u4ee5\u4f7f\u7528<code>mysql.connector.connect()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u6b64\u51fd\u6570\u9700\u8981\u4f20\u9012\u4e00\u4e9b\u53c2\u6570\uff0c\u5305\u62ec\u4e3b\u673a\u540d\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u548c\u6570\u636e\u5e93\u540d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<p>try:<\/p>\n<p>    connection = mysql.connector.connect(<\/p>\n<p>        host=&#39;localhost&#39;,<\/p>\n<p>        user=&#39;root&#39;,<\/p>\n<p>        password=&#39;password&#39;,<\/p>\n<p>        database=&#39;test_db&#39;<\/p>\n<p>    )<\/p>\n<p>    if connection.is_connected():<\/p>\n<p>        print(&quot;Successfully connected to the database&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>    if connection.is_connected():<\/p>\n<p>        connection.close()<\/p>\n<p>        print(&quot;Connection closed&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c1d\u8bd5\u8fde\u63a5\u5230\u672c\u5730\u7684MySQL\u670d\u52a1\u5668\uff0c\u5e76\u8bbf\u95ee\u540d\u4e3a<code>test_db<\/code>\u7684\u6570\u636e\u5e93\u3002\u5982\u679c\u8fde\u63a5\u6210\u529f\uff0c\u4f1a\u6253\u5370\u6210\u529f\u4fe1\u606f\uff1b\u5426\u5219\uff0c\u4f1a\u6355\u83b7\u5e76\u6253\u5370\u9519\u8bef\u4fe1\u606f\u3002\u6700\u540e\uff0c\u65e0\u8bba\u8fde\u63a5\u662f\u5426\u6210\u529f\uff0c\u90fd\u4f1a\u5173\u95ed\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6267\u884cSQL\u67e5\u8be2<\/h3>\n<\/p>\n<p><p>\u5728\u6210\u529f\u8fde\u63a5\u5230\u6570\u636e\u5e93\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fde\u63a5\u5bf9\u8c61\u7684<code>cursor()<\/code>\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61\u3002\u901a\u8fc7\u6e38\u6807\u5bf9\u8c61\uff0c\u53ef\u4ee5\u6267\u884cSQL\u67e5\u8be2\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6267\u884cSQL\u67e5\u8be2\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<p>connection = mysql.connector.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;root&#39;,<\/p>\n<p>    password=&#39;password&#39;,<\/p>\n<p>    database=&#39;test_db&#39;<\/p>\n<p>)<\/p>\n<p>cursor = connection.cursor()<\/p>\n<h2><strong>\u6267\u884c\u67e5\u8be2<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT * FROM users&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c<\/strong><\/h2>\n<p>result = cursor.fetchall()<\/p>\n<p>for row in result:<\/p>\n<p>    print(row)<\/p>\n<p>cursor.close()<\/p>\n<p>connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6e38\u6807\u5bf9\u8c61\u6267\u884c\u4e86\u4e00\u6761\u7b80\u5355\u7684SELECT\u67e5\u8be2\uff0c\u5e76\u901a\u8fc7<code>fetchall()<\/code>\u65b9\u6cd5\u83b7\u53d6\u6240\u6709\u7684\u67e5\u8be2\u7ed3\u679c\u3002\u7ed3\u679c\u4ee5\u5217\u8868\u5f62\u5f0f\u8fd4\u56de\uff0c\u5176\u4e2d\u6bcf\u4e00\u9879\u90fd\u662f\u4e00\u4e2a\u5143\u7ec4\uff0c\u4ee3\u8868\u4e00\u884c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u63d2\u5165\u3001\u66f4\u65b0\u548c\u5220\u9664\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u9664\u4e86SELECT\u67e5\u8be2\uff0c\u6e38\u6807\u5bf9\u8c61\u8fd8\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u63d2\u5165\u3001\u66f4\u65b0\u548c\u5220\u9664\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>\u63d2\u5165\u6570\u636e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.execute(&quot;INSERT INTO users (name, age) VALUES (%s, %s)&quot;, (&#39;John Doe&#39;, 30))<\/p>\n<p>connection.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u63d2\u5165\u6570\u636e\u65f6\uff0c\u4f7f\u7528\u53c2\u6570\u5316\u67e5\u8be2\u53ef\u4ee5\u6709\u6548\u9632\u6b62SQL\u6ce8\u5165\u653b\u51fb\u3002\u5728\u6267\u884c\u63d2\u5165\u64cd\u4f5c\u540e\uff0c\u9700\u8981\u8c03\u7528<code>connection.commit()<\/code>\u6765\u63d0\u4ea4\u4e8b\u52a1\u3002<\/p>\n<\/p>\n<p><h4>\u66f4\u65b0\u6570\u636e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.execute(&quot;UPDATE users SET age = %s WHERE name = %s&quot;, (31, &#39;John Doe&#39;))<\/p>\n<p>connection.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u66f4\u65b0\u6570\u636e\u7684\u64cd\u4f5c\u4e0e\u63d2\u5165\u7c7b\u4f3c\uff0c\u4e5f\u662f\u4f7f\u7528\u53c2\u6570\u5316\u67e5\u8be2\uff0c\u5e76\u5728\u6267\u884c\u540e\u63d0\u4ea4\u4e8b\u52a1\u3002<\/p>\n<\/p>\n<p><h4>\u5220\u9664\u6570\u636e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.execute(&quot;DELETE FROM users WHERE name = %s&quot;, (&#39;John Doe&#39;,))<\/p>\n<p>connection.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5220\u9664\u6570\u636e\u65f6\u540c\u6837\u4f7f\u7528\u53c2\u6570\u5316\u67e5\u8be2\uff0c\u5e76\u5728\u6267\u884c\u540e\u63d0\u4ea4\u4e8b\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u4f8b\u5982\u8fde\u63a5\u5931\u8d25\u3001SQL\u8bed\u6cd5\u9519\u8bef\u7b49\u3002\u4e3a\u4e86\u63d0\u9ad8\u7a0b\u5e8f\u7684\u9c81\u68d2\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528try-except\u7ed3\u6784\u6765\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\uff1a<\/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;root&#39;,<\/p>\n<p>        password=&#39;password&#39;,<\/p>\n<p>        database=&#39;test_db&#39;<\/p>\n<p>    )<\/p>\n<p>    cursor = connection.cursor()<\/p>\n<p>    cursor.execute(&quot;SELECT * FROM non_existent_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>    if connection.is_connected():<\/p>\n<p>        cursor.close()<\/p>\n<p>        connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c1d\u8bd5\u67e5\u8be2\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u8868\uff0c\u6355\u83b7\u5230\u7684\u5f02\u5e38\u4f1a\u88ab\u6253\u5370\u51fa\u6765\u3002\u540c\u65f6\uff0c\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u90fd\u4f1a\u6267\u884cfinally\u5757\u4e2d\u7684\u4ee3\u7801\uff0c\u786e\u4fdd\u8d44\u6e90\u88ab\u6b63\u786e\u91ca\u653e\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u8fde\u63a5\u6c60\u63d0\u9ad8\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u5e93\u8fde\u63a5\u8bf7\u6c42\u65f6\uff0c\u4f7f\u7528\u8fde\u63a5\u6c60\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002MySQL Connector\u63d0\u4f9b\u4e86\u8fde\u63a5\u6c60\u7684\u652f\u6301\uff0c\u53ef\u4ee5\u901a\u8fc7<code>mysql.connector.pooling<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mysql.connector import pooling<\/p>\n<p>dbconfig = {<\/p>\n<p>    &quot;host&quot;: &quot;localhost&quot;,<\/p>\n<p>    &quot;user&quot;: &quot;root&quot;,<\/p>\n<p>    &quot;password&quot;: &quot;password&quot;,<\/p>\n<p>    &quot;database&quot;: &quot;test_db&quot;<\/p>\n<p>}<\/p>\n<p>pool = pooling.MySQLConnectionPool(pool_name=&quot;mypool&quot;, pool_size=3, dbconfig)<\/p>\n<p>connection = pool.get_connection()<\/p>\n<p>cursor = connection.cursor()<\/p>\n<p>cursor.execute(&quot;SELECT * FROM users&quot;)<\/p>\n<p>result = cursor.fetchall()<\/p>\n<p>for row in result:<\/p>\n<p>    print(row)<\/p>\n<p>cursor.close()<\/p>\n<p>connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>mypool<\/code>\u7684\u8fde\u63a5\u6c60\uff0c\u6c60\u4e2d\u5305\u542b\u4e09\u4e2a\u8fde\u63a5\u3002\u901a\u8fc7\u8c03\u7528<code>get_connection()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4ece\u6c60\u4e2d\u83b7\u53d6\u4e00\u4e2a\u8fde\u63a5\u5bf9\u8c61\u3002\u4f7f\u7528\u8fde\u63a5\u6c60\u53ef\u4ee5\u51cf\u5c11\u8fde\u63a5\u5efa\u7acb\u548c\u5173\u95ed\u7684\u5f00\u9500\uff0c\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u54cd\u5e94\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u7ba1\u7406\u6570\u636e\u5e93\u914d\u7f6e<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u5b89\u5168\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u6765\u7ba1\u7406\u6570\u636e\u5e93\u914d\u7f6e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from mysql.connector import connect<\/p>\n<p>host = os.getenv(&#39;DB_HOST&#39;, &#39;localhost&#39;)<\/p>\n<p>user = os.getenv(&#39;DB_USER&#39;, &#39;root&#39;)<\/p>\n<p>password = os.getenv(&#39;DB_PASSWORD&#39;, &#39;password&#39;)<\/p>\n<p>database = os.getenv(&#39;DB_DATABASE&#39;, &#39;test_db&#39;)<\/p>\n<p>connection = connect(host=host, user=user, password=password, database=database)<\/p>\n<p>cursor = connection.cursor()<\/p>\n<p>cursor.execute(&quot;SELECT * FROM users&quot;)<\/p>\n<p>result = cursor.fetchall()<\/p>\n<p>for row in result:<\/p>\n<p>    print(row)<\/p>\n<p>cursor.close()<\/p>\n<p>connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>os.getenv()<\/code>\u51fd\u6570\u83b7\u53d6\u73af\u5883\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002\u5f53\u9700\u8981\u66f4\u6539\u6570\u636e\u5e93\u914d\u7f6e\u65f6\uff0c\u53ea\u9700\u4fee\u6539\u73af\u5883\u53d8\u91cf\uff0c\u800c\u65e0\u9700\u66f4\u6539\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u4e0eMySQL\u7684\u8fde\u63a5\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0cMySQL Connector\u662f\u5176\u4e2d\u4e00\u79cd\u5e38\u7528\u4e14\u5f3a\u5927\u7684\u5de5\u5177\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u5b89\u88c5MySQL Connector\u3001\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93\u3001\u6267\u884cSQL\u67e5\u8be2\u3001\u63d2\u5165\u66f4\u65b0\u5220\u9664\u6570\u636e\u3001\u5904\u7406\u5f02\u5e38\u3001\u4f7f\u7528\u8fde\u63a5\u6c60\u548c\u7ba1\u7406\u6570\u636e\u5e93\u914d\u7f6e\u3002\u901a\u8fc7\u8fd9\u4e9b\u77e5\u8bc6\uff0c\u60a8\u53ef\u4ee5\u5728Python\u9879\u76ee\u4e2d\u9ad8\u6548\u5730\u64cd\u4f5cMySQL\u6570\u636e\u5e93\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u67e5\u8be2\u8fd8\u662f\u590d\u6742\u7684\u4e8b\u52a1\u5904\u7406\uff0cMySQL Connector\u90fd\u80fd\u63d0\u4f9b\u5f3a\u6709\u529b\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5MySQL\u8fde\u63a5\u5e93\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u8fde\u63a5MySQL\uff0c\u60a8\u9700\u8981\u5b89\u88c5\u4e00\u4e2a\u9002\u5408\u7684\u8fde\u63a5\u5e93\uff0c\u6bd4\u5982<code>mysql-connector-python<\/code>\u6216<code>PyMySQL<\/code>\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u5b89\u88c5\uff1a  <\/p>\n<pre><code>pip install mysql-connector-python\n<\/code><\/pre>\n<p>\u6216  <\/p>\n<pre><code>pip install PyMySQL\n<\/code><\/pre>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5f15\u5165\u8be5\u5e93\u4ee5\u8fdb\u884c\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n<p><strong>\u5982\u4f55\u901a\u8fc7Python\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93\uff1f<\/strong><br \/>\u4e00\u65e6\u5b89\u88c5\u4e86\u8fde\u63a5\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u6765\u5efa\u7acb\u8fde\u63a5\uff1a  <\/p>\n<pre><code class=\"language-python\">import mysql.connector\n\nconnection = mysql.connector.connect(\n    host=&#39;localhost&#39;,\n    user=&#39;your_username&#39;,\n    password=&#39;your_password&#39;,\n    database=&#39;your_database&#39;\n)\n\nif connection.is_connected():\n    print(&quot;\u6210\u529f\u8fde\u63a5\u5230\u6570\u636e\u5e93&quot;)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5c06<code>your_username<\/code>\u3001<code>your_password<\/code>\u548c<code>your_database<\/code>\u66ff\u6362\u4e3a\u60a8\u7684\u5b9e\u9645\u6570\u636e\u5e93\u51ed\u636e\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406MySQL\u67e5\u8be2\u7ed3\u679c\uff1f<\/strong><br \/>\u5728\u6267\u884c\u67e5\u8be2\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u6e38\u6807\u5bf9\u8c61\u6765\u5904\u7406\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u67e5\u8be2\u5e76\u6253\u5370\u6240\u6709\u884c\uff1a  <\/p>\n<pre><code class=\"language-python\">cursor = connection.cursor()\ncursor.execute(&quot;SELECT * FROM your_table&quot;)\n\nfor row in cursor.fetchall():\n    print(row)\n\ncursor.close()\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u60a8\u83b7\u53d6\u548c\u5904\u7406\u4ece\u6570\u636e\u5e93\u4e2d\u8fd4\u56de\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e0eMySQL\u7684\u8fde\u63a5\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528MySQL Connector\u3001PyMySQL\u3001S [&hellip;]","protected":false},"author":3,"featured_media":977535,"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\/977533"}],"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=977533"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977533\/revisions"}],"predecessor-version":[{"id":977539,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977533\/revisions\/977539"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/977535"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=977533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=977533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=977533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}