{"id":1102565,"date":"2025-01-08T16:01:21","date_gmt":"2025-01-08T08:01:21","guid":{"rendered":""},"modified":"2025-01-08T16:01:25","modified_gmt":"2025-01-08T08:01:25","slug":"python-%e6%95%b0%e6%8d%ae%e5%ba%93%e6%9f%a5%e8%af%a2%e7%bb%93%e6%9e%9c%e5%a6%82%e4%bd%95%e6%93%8d%e4%bd%9c-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1102565.html","title":{"rendered":"python \u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c\u5982\u4f55\u64cd\u4f5c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064812\/56b485a4-da6a-45b2-ac8b-7e78e3be5b3f.webp\" alt=\"python \u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c\u5982\u4f55\u64cd\u4f5c\" \/><\/p>\n<p><p> <strong>Python\u64cd\u4f5c\u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528cursor.fetchall()\u83b7\u53d6\u6240\u6709\u7ed3\u679c\u3001\u4f7f\u7528cursor.fetchone()\u9010\u6761\u83b7\u53d6\u7ed3\u679c\u3001\u5bf9\u7ed3\u679c\u8fdb\u884c\u904d\u5386\u5904\u7406\u7b49\u3002<\/strong> \u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5728Python\u4e2d\u64cd\u4f5c\u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c\uff0c\u4ee5\u53ca\u5bf9\u5176\u4e2d\u7684<strong>\u4f7f\u7528cursor.fetchall()\u83b7\u53d6\u6240\u6709\u7ed3\u679c<\/strong>\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u64cd\u4f5c\u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c\u901a\u5e38\u6d89\u53ca\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u8fde\u63a5\u6570\u636e\u5e93\u3001\u6267\u884c\u67e5\u8be2\u8bed\u53e5\u3001\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c\u3001\u5904\u7406\u67e5\u8be2\u7ed3\u679c\u3002\u4ee5\u4e0b\u5c06\u901a\u8fc7\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\u6765\u5c55\u793a\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e9b\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8fde\u63a5\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u9002\u5f53\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u6765\u8fde\u63a5\u6570\u636e\u5e93\u662f\u8fdb\u884c\u67e5\u8be2\u64cd\u4f5c\u7684\u7b2c\u4e00\u6b65\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 <code>sqlite3<\/code> \u8fde\u63a5SQLite\u6570\u636e\u5e93\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93<\/strong><\/h2>\n<p>connection = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = connection.cursor()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6267\u884c\u67e5\u8be2\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u6e38\u6807\u5bf9\u8c61\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u6e38\u6807\u5bf9\u8c61\u7684 <code>execute()<\/code> \u65b9\u6cd5\u6267\u884cSQL\u67e5\u8be2\u8bed\u53e5\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u67e5\u8be2\u6240\u6709\u8bb0\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6267\u884c\u67e5\u8be2\u8bed\u53e5<\/p>\n<p>cursor.execute(&#39;SELECT * FROM tablename&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u6267\u884c\u67e5\u8be2\u8bed\u53e5\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528cursor.fetchall()\u83b7\u53d6\u6240\u6709\u7ed3\u679c<\/strong><\/li>\n<\/ol>\n<p><p><code>cursor.fetchall()<\/code> \u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u67e5\u8be2\u7684\u6240\u6709\u7ed3\u679c\uff0c\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u8bb0\u5f55\u7684\u5217\u8868\u3002\u6bcf\u4e2a\u8bb0\u5f55\u901a\u5e38\u662f\u4e00\u4e2a\u5143\u7ec4\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u6240\u6709\u67e5\u8be2\u7ed3\u679c<\/p>\n<p>results = cursor.fetchall()<\/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<ol start=\"2\">\n<li><strong>\u4f7f\u7528cursor.fetchone()\u9010\u6761\u83b7\u53d6\u7ed3\u679c<\/strong><\/li>\n<\/ol>\n<p><p><code>cursor.fetchone()<\/code> \u65b9\u6cd5\u7528\u4e8e\u9010\u6761\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c\uff0c\u8fd4\u56de\u4e00\u6761\u8bb0\u5f55\uff0c\u5982\u679c\u6ca1\u6709\u66f4\u591a\u8bb0\u5f55\u5219\u8fd4\u56de <code>None<\/code>\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u5355\u6761\u67e5\u8be2\u7ed3\u679c<\/p>\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<p><h3>\u56db\u3001\u5bf9\u7ed3\u679c\u8fdb\u884c\u904d\u5386\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u53ef\u4ee5\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u904d\u5386\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5904\u7406\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5904\u7406\u6bcf\u6761\u8bb0\u5f55<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u5bf9 <code>results<\/code> \u5217\u8868\u4e2d\u7684\u6bcf\u6761\u8bb0\u5f55\u8fdb\u884c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5c06\u8bb0\u5f55\u63d2\u5165\u5230\u53e6\u4e00\u4e2a\u8868\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u5df2\u5b58\u5728\u53e6\u4e00\u4e2a\u8868 &#39;new_tablename&#39;<\/p>\n<p>for row in results:<\/p>\n<p>    cursor.execute(&#39;INSERT INTO new_tablename VALUES (?, ?, ?)&#39;, row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528\u751f\u6210\u5668\u9010\u6761\u5904\u7406<\/strong><\/li>\n<\/ol>\n<p><p>\u5982\u679c\u67e5\u8be2\u7ed3\u679c\u975e\u5e38\u5927\uff0c\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u9010\u6761\u5904\u7406\uff0c\u4ee5\u8282\u7701\u5185\u5b58\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def fetchone_generator(cursor):<\/p>\n<p>    while True:<\/p>\n<p>        row = cursor.fetchone()<\/p>\n<p>        if row is None:<\/p>\n<p>            break<\/p>\n<p>        yield row<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668\u9010\u6761\u5904\u7406<\/strong><\/h2>\n<p>for row in fetchone_generator(cursor):<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5173\u95ed\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u5b8c\u67e5\u8be2\u7ed3\u679c\u540e\uff0c\u8bb0\u5f97\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u4ee5\u91ca\u653e\u8d44\u6e90\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/p>\n<p>cursor.close()<\/p>\n<p>connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u64cd\u4f5c\u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93<\/strong><\/h2>\n<p>connection = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = connection.cursor()<\/p>\n<h2><strong>\u6267\u884c\u67e5\u8be2\u8bed\u53e5<\/strong><\/h2>\n<p>cursor.execute(&#39;SELECT * FROM tablename&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u67e5\u8be2\u7ed3\u679c<\/strong><\/h2>\n<p>results = cursor.fetchall()<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>for row in results:<\/p>\n<p>    print(row)<\/p>\n<h2><strong>\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/strong><\/h2>\n<p>cursor.close()<\/p>\n<p>connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528pandas\u5904\u7406\u67e5\u8be2\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u9700\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>pandas<\/code> \u5e93\u5c06\u67e5\u8be2\u7ed3\u679c\u8f6c\u6362\u4e3aDataFrame\u8fdb\u884c\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93<\/strong><\/h2>\n<p>connection = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<h2><strong>\u4f7f\u7528pandas\u8bfb\u53d6SQL\u67e5\u8be2\u7ed3\u679c<\/strong><\/h2>\n<p>df = pd.read_sql_query(&#39;SELECT * FROM tablename&#39;, connection)<\/p>\n<h2><strong>\u5904\u7406DataFrame\uff0c\u4f8b\u5982\u7b5b\u9009\u6570\u636e<\/strong><\/h2>\n<p>filtered_df = df[df[&#39;column_name&#39;] &gt; 10]<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(filtered_df)<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528 <code>pandas<\/code> \u5904\u7406\u67e5\u8be2\u7ed3\u679c\u53ef\u4ee5\u4f7f\u6570\u636e\u5206\u6790\u5de5\u4f5c\u66f4\u52a0\u7b80\u4fbf\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u4e0a\u8ff0\u793a\u4f8b\u4e3b\u8981\u9488\u5bf9SQLite\u6570\u636e\u5e93\u3002\u5bf9\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\u5e93\uff0c\u5982MySQL\u3001PostgreSQL\u3001SQL Server\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5e94\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u7528\u5e93\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>MySQL<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528 <code>mysql-connector-python<\/code> \u5e93\u8fde\u63a5MySQL\u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<h2><strong>\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93<\/strong><\/h2>\n<p>connection = mysql.connector.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;username&#39;,<\/p>\n<p>    password=&#39;password&#39;,<\/p>\n<p>    database=&#39;database_name&#39;<\/p>\n<p>)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = connection.cursor()<\/p>\n<h2><strong>\u6267\u884c\u67e5\u8be2\u8bed\u53e5<\/strong><\/h2>\n<p>cursor.execute(&#39;SELECT * FROM tablename&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u67e5\u8be2\u7ed3\u679c<\/strong><\/h2>\n<p>results = cursor.fetchall()<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>for row in results:<\/p>\n<p>    print(row)<\/p>\n<h2><strong>\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/strong><\/h2>\n<p>cursor.close()<\/p>\n<p>connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>PostgreSQL<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528 <code>psycopg2<\/code> \u5e93\u8fde\u63a5PostgreSQL\u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import psycopg2<\/p>\n<h2><strong>\u8fde\u63a5\u5230PostgreSQL\u6570\u636e\u5e93<\/strong><\/h2>\n<p>connection = psycopg2.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;username&#39;,<\/p>\n<p>    password=&#39;password&#39;,<\/p>\n<p>    dbname=&#39;database_name&#39;<\/p>\n<p>)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = connection.cursor()<\/p>\n<h2><strong>\u6267\u884c\u67e5\u8be2\u8bed\u53e5<\/strong><\/h2>\n<p>cursor.execute(&#39;SELECT * FROM tablename&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u67e5\u8be2\u7ed3\u679c<\/strong><\/h2>\n<p>results = cursor.fetchall()<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>for row in results:<\/p>\n<p>    print(row)<\/p>\n<h2><strong>\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/strong><\/h2>\n<p>cursor.close()<\/p>\n<p>connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>SQL Server<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528 <code>pyodbc<\/code> \u5e93\u8fde\u63a5SQL Server\u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyodbc<\/p>\n<h2><strong>\u8fde\u63a5\u5230SQL Server\u6570\u636e\u5e93<\/strong><\/h2>\n<p>connection = pyodbc.connect(<\/p>\n<p>    &#39;DRIVER={SQL Server};&#39;<\/p>\n<p>    &#39;SERVER=server_name;&#39;<\/p>\n<p>    &#39;DATABASE=database_name;&#39;<\/p>\n<p>    &#39;UID=username;&#39;<\/p>\n<p>    &#39;PWD=password&#39;<\/p>\n<p>)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = connection.cursor()<\/p>\n<h2><strong>\u6267\u884c\u67e5\u8be2\u8bed\u53e5<\/strong><\/h2>\n<p>cursor.execute(&#39;SELECT * FROM tablename&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u67e5\u8be2\u7ed3\u679c<\/strong><\/h2>\n<p>results = cursor.fetchall()<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>for row in results:<\/p>\n<p>    print(row)<\/p>\n<h2><strong>\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/strong><\/h2>\n<p>cursor.close()<\/p>\n<p>connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u64cd\u4f5c\u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c\u65f6\uff0c\u5e94\u8be5\u5904\u7406\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\uff0c\u4ee5\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>try:<\/p>\n<p>    # \u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93<\/p>\n<p>    connection = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>    # \u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61<\/p>\n<p>    cursor = connection.cursor()<\/p>\n<p>    # \u6267\u884c\u67e5\u8be2\u8bed\u53e5<\/p>\n<p>    cursor.execute(&#39;SELECT * FROM tablename&#39;)<\/p>\n<p>    # \u83b7\u53d6\u6240\u6709\u67e5\u8be2\u7ed3\u679c<\/p>\n<p>    results = cursor.fetchall()<\/p>\n<p>    # \u8f93\u51fa\u7ed3\u679c<\/p>\n<p>    for row in results:<\/p>\n<p>        print(row)<\/p>\n<p>except sqlite3.Error as e:<\/p>\n<p>    print(f&quot;\u6570\u636e\u5e93\u9519\u8bef: {e}&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;\u5176\u4ed6\u9519\u8bef: {e}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    # \u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/p>\n<p>    if cursor:<\/p>\n<p>        cursor.close()<\/p>\n<p>    if connection:<\/p>\n<p>        connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u8be6\u7ec6\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5728Python\u4e2d\u9ad8\u6548\u5730\u64cd\u4f5c\u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u6570\u636e\u5e93\u7c7b\u578b\u548c\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u8fde\u63a5\u5230\u6570\u636e\u5e93\u4ee5\u8fdb\u884c\u67e5\u8be2\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u5e93\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u60a8\u4f7f\u7528\u7684\u6570\u636e\u5e93\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u4f7f\u7528SQLite\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684sqlite3\u5e93\uff1b\u5bf9\u4e8eMySQL\uff0c\u53ef\u4ee5\u4f7f\u7528mysql-connector-python\u6216SQLAlchemy\u5e93\u3002\u5efa\u7acb\u8fde\u63a5\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528SQL\u8bed\u53e5\u8fdb\u884c\u67e5\u8be2\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u4ece\u6570\u636e\u5e93\u67e5\u8be2\u8fd4\u56de\u7684\u7ed3\u679c\uff1f<\/strong><br \/>\u67e5\u8be2\u7ed3\u679c\u901a\u5e38\u4ee5\u6e38\u6807\u5bf9\u8c61\u7684\u5f62\u5f0f\u8fd4\u56de\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u6e38\u6807\u7684fetchone()\u3001fetchall()\u6216fetchmany(size)\u65b9\u6cd5\u6765\u83b7\u53d6\u6570\u636e\u3002\u83b7\u53d6\u540e\uff0c\u7ed3\u679c\u5c06\u4ee5\u5143\u7ec4\u7684\u5f62\u5f0f\u5448\u73b0\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u7279\u5b9a\u7684\u5217\u503c\uff0c\u6216\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u5b57\u5178\u4ee5\u4fbf\u4e8e\u5904\u7406\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u6570\u636e\u5e93\u67e5\u8be2\u4e2d\u7684\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6570\u636e\u5e93\u67e5\u8be2\u65f6\uff0c\u5904\u7406\u5f02\u5e38\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u4f7f\u7528try-except\u5757\u6765\u6355\u83b7\u6f5c\u5728\u7684\u9519\u8bef\uff0c\u4f8b\u5982\u8fde\u63a5\u5931\u8d25\u6216SQL\u8bed\u6cd5\u9519\u8bef\u3002\u901a\u8fc7\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\uff0c\u60a8\u53ef\u4ee5\u91c7\u53d6\u76f8\u5e94\u7684\u63aa\u65bd\uff0c\u6bd4\u5982\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\u6216\u8005\u901a\u77e5\u7528\u6237\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u7a33\u5b9a\u8fd0\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u64cd\u4f5c\u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528cursor.fetchall()\u83b7\u53d6\u6240\u6709\u7ed3\u679c\u3001\u4f7f\u7528cursor [&hellip;]","protected":false},"author":3,"featured_media":1102581,"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\/1102565"}],"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=1102565"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1102565\/revisions"}],"predecessor-version":[{"id":1102585,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1102565\/revisions\/1102585"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1102581"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1102565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1102565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1102565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}