{"id":1132341,"date":"2025-01-08T20:56:13","date_gmt":"2025-01-08T12:56:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1132341.html"},"modified":"2025-01-08T20:56:15","modified_gmt":"2025-01-08T12:56:15","slug":"python-%e5%a6%82%e4%bd%95%e8%ae%bf%e9%97%ae%e6%95%b0%e6%8d%ae%e5%ba%93%e6%95%b0%e6%8d%ae%e5%ba%93%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1132341.html","title":{"rendered":"python \u5982\u4f55\u8bbf\u95ee\u6570\u636e\u5e93\u6570\u636e\u5e93\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101916\/c1ca0760-d765-4c01-90a0-592cdadf1817.webp\" alt=\"python \u5982\u4f55\u8bbf\u95ee\u6570\u636e\u5e93\u6570\u636e\u5e93\u8868\" \/><\/p>\n<p><p> <strong>Python\u8bbf\u95ee\u6570\u636e\u5e93\u8868\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5\u6570\u636e\u5e93\u9a71\u52a8\u3001\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\u3001\u6267\u884cSQL\u67e5\u8be2\u3001\u5904\u7406\u67e5\u8be2\u7ed3\u679c\u3001\u5173\u95ed\u8fde\u63a5<\/strong>\u3002\u4ee5\u4e0b\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u7684\u4e00\u4e2a\u6b65\u9aa4\u2014\u2014\u6267\u884cSQL\u67e5\u8be2\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6267\u884cSQL\u67e5\u8be2\u662f\u8bbf\u95ee\u6570\u636e\u5e93\u7684\u6838\u5fc3\u6b65\u9aa4\u3002\u901a\u8fc7\u4f7f\u7528\u6570\u636e\u5e93\u9a71\u52a8\u5e93\uff08\u5982PyMySQL\u3001psycopg2\u7b49\uff09\uff0c\u53ef\u4ee5\u5c06SQL\u8bed\u53e5\u53d1\u9001\u5230\u6570\u636e\u5e93\u670d\u52a1\u5668\u3002\u901a\u5e38\uff0c\u4f60\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61\uff0c\u8c03\u7528\u5176execute()\u65b9\u6cd5\u6765\u6267\u884cSQL\u67e5\u8be2\u3002\u6267\u884c\u5b8c\u67e5\u8be2\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7fetchone()\u3001fetchall()\u7b49\u65b9\u6cd5\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c\u3002\u8fd9\u6837\uff0c\u4f60\u53ef\u4ee5\u83b7\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u5e76\u5728Python\u4e2d\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u6570\u636e\u5e93\u9a71\u52a8<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u8bbf\u95ee\u6570\u636e\u5e93\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u76f8\u5e94\u7684\u6570\u636e\u5e93\u9a71\u52a8\u3002\u4e0d\u540c\u7684\u6570\u636e\u5e93\u6709\u4e0d\u540c\u7684\u9a71\u52a8\u5305\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>MySQL<\/strong>\uff1a\u4f7f\u7528<code>PyMySQL<\/code>\u6216<code>MySQL Connector\/Python<\/code><\/li>\n<li><strong>PostgreSQL<\/strong>\uff1a\u4f7f\u7528<code>psycopg2<\/code><\/li>\n<li><strong>SQLite<\/strong>\uff1aSQLite\u9a71\u52a8\u5df2\u7ecf\u5185\u7f6e\u5728Python\u4e2d\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5<\/li>\n<\/ul>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>pip<\/code>\u5b89\u88c5\u8fd9\u4e9b\u9a71\u52a8\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pymysql<\/p>\n<p>pip install psycopg2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\u662f\u8bbf\u95ee\u6570\u636e\u5e93\u7684\u7b2c\u4e00\u6b65\u3002\u4f60\u9700\u8981\u63d0\u4f9b\u6570\u636e\u5e93\u7684\u8fde\u63a5\u53c2\u6570\uff0c\u5982\u4e3b\u673a\u5730\u5740\u3001\u7aef\u53e3\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u548c\u6570\u636e\u5e93\u540d\u79f0\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u4e0d\u540c\u9a71\u52a8\u5efa\u7acb\u8fde\u63a5\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1. MySQL<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>connection = pymysql.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<p><h4>2. PostgreSQL<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import psycopg2<\/p>\n<p>connection = psycopg2.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>    dbname=&#39;your_database&#39;<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. SQLite<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>connection = sqlite3.connect(&#39;your_database.db&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6267\u884cSQL\u67e5\u8be2<\/h3>\n<\/p>\n<p><p>\u5efa\u7acb\u8fde\u63a5\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u6e38\u6807\u5bf9\u8c61\u6765\u6267\u884cSQL\u67e5\u8be2\u3002\u4ee5\u4e0b\u662f\u6267\u884c\u67e5\u8be2\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1. MySQL<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">cursor = connection.cursor()<\/p>\n<p>sql = &quot;SELECT * FROM your_table&quot;<\/p>\n<p>cursor.execute(sql)<\/p>\n<p>results = cursor.fetchall()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. PostgreSQL<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">cursor = connection.cursor()<\/p>\n<p>sql = &quot;SELECT * FROM your_table&quot;<\/p>\n<p>cursor.execute(sql)<\/p>\n<p>results = cursor.fetchall()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. SQLite<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">cursor = connection.cursor()<\/p>\n<p>sql = &quot;SELECT * FROM your_table&quot;<\/p>\n<p>cursor.execute(sql)<\/p>\n<p>results = cursor.fetchall()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u67e5\u8be2\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u67e5\u8be2\u7ed3\u679c\u901a\u5e38\u6d89\u53ca\u904d\u5386\u67e5\u8be2\u7ed3\u679c\u96c6\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u5904\u7406\u67e5\u8be2\u7ed3\u679c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for row in results:<\/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>\u5b8c\u6210\u67e5\u8be2\u548c\u5904\u7406\u540e\uff0c\u52a1\u5fc5\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5\uff0c\u4ee5\u91ca\u653e\u8d44\u6e90\uff1a<\/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<p><h3>\u516d\u3001\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u5e93\u64cd\u4f5c\u53ef\u80fd\u4f1a\u51fa\u73b0\u5404\u79cd\u9519\u8bef\uff0c\u56e0\u6b64\u9700\u8981\u52a0\u5165\u9519\u8bef\u5904\u7406\u673a\u5236\u3002\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>try:<\/p>\n<p>    connection = pymysql.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>    cursor = connection.cursor()<\/p>\n<p>    sql = &quot;SELECT * FROM your_table&quot;<\/p>\n<p>    cursor.execute(sql)<\/p>\n<p>    results = cursor.fetchall()<\/p>\n<p>    for row in results:<\/p>\n<p>        print(row)<\/p>\n<p>except pymysql.MySQLError as e:<\/p>\n<p>    print(f&quot;Error: {e}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    cursor.close()<\/p>\n<p>    connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528ORM\u6846\u67b6<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u76f4\u63a5\u4f7f\u7528SQL\u67e5\u8be2\uff0cPython\u8fd8\u6709\u4e00\u4e9bORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u6846\u67b6\uff0c\u5982SQLAlchemy\u548cDjango ORM\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u64cd\u4f5c\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><h4>1. SQLAlchemy<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine, Table, MetaData<\/p>\n<p>engine = create_engine(&#39;mysql+pymysql:\/\/your_username:your_password@localhost\/your_database&#39;)<\/p>\n<p>metadata = MetaData()<\/p>\n<p>table = Table(&#39;your_table&#39;, metadata, autoload=True, autoload_with=engine)<\/p>\n<p>with engine.connect() as connection:<\/p>\n<p>    result = connection.execute(table.select())<\/p>\n<p>    for row in result:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. Django ORM<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f7f\u7528Django\u6846\u67b6\uff0c\u53ef\u4ee5\u5229\u7528\u5176\u5185\u7f6e\u7684ORM\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from myapp.models import YourModel<\/p>\n<p>results = YourModel.objects.all()<\/p>\n<p>for row in results:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u8bbf\u95ee\u6570\u636e\u5e93\u8868\u65f6\uff0c\u6027\u80fd\u4f18\u5316\u4e5f\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u4f18\u5316\u7b56\u7565\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u7d22\u5f15<\/strong>\uff1a\u4e3a\u5e38\u7528\u7684\u67e5\u8be2\u5b57\u6bb5\u5efa\u7acb\u7d22\u5f15\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u67e5\u8be2\u901f\u5ea6\u3002<\/li>\n<li><strong>\u8fde\u63a5\u6c60<\/strong>\uff1a\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u53ef\u4ee5\u51cf\u5c11\u5efa\u7acb\u548c\u5173\u95ed\u8fde\u63a5\u7684\u5f00\u9500\u3002<\/li>\n<li><strong>\u6279\u91cf\u64cd\u4f5c<\/strong>\uff1a\u5728\u63d2\u5165\u6216\u66f4\u65b0\u5927\u91cf\u6570\u636e\u65f6\uff0c\u5c3d\u91cf\u4f7f\u7528\u6279\u91cf\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u7f13\u5b58<\/strong>\uff1a\u5bf9\u4e8e\u9891\u7e41\u67e5\u8be2\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u7f13\u5b58\u673a\u5236\u51cf\u5c11\u6570\u636e\u5e93\u8bbf\u95ee\u6b21\u6570\u3002<\/li>\n<li><strong>\u4f18\u5316\u67e5\u8be2<\/strong>\uff1a\u907f\u514d\u590d\u6742\u7684\u5d4c\u5957\u67e5\u8be2\u548c\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\uff0c\u5c3d\u91cf\u7b80\u5316SQL\u8bed\u53e5\u3002<\/li>\n<\/ul>\n<p><h3>\u4e5d\u3001\u5b89\u5168\u6027<\/h3>\n<\/p>\n<p><p>\u8bbf\u95ee\u6570\u636e\u5e93\u65f6\uff0c\u5b89\u5168\u6027\u4e5f\u662f\u5fc5\u987b\u5173\u6ce8\u7684\u65b9\u9762\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5b89\u5168\u63aa\u65bd\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u53c2\u6570\u5316\u67e5\u8be2<\/strong>\uff1a\u907f\u514dSQL\u6ce8\u5165\u653b\u51fb\u3002<\/li>\n<li><strong>\u52a0\u5bc6\u4f20\u8f93<\/strong>\uff1a\u4f7f\u7528SSL\/TLS\u52a0\u5bc6\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u4fdd\u62a4\u4f20\u8f93\u4e2d\u7684\u6570\u636e\u3002<\/li>\n<li><strong>\u6743\u9650\u63a7\u5236<\/strong>\uff1a\u4e3a\u6570\u636e\u5e93\u7528\u6237\u5206\u914d\u6700\u5c0f\u6743\u9650\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6570\u636e\u8bbf\u95ee\u3002<\/li>\n<li><strong>\u65e5\u5fd7\u8bb0\u5f55<\/strong>\uff1a\u542f\u7528\u6570\u636e\u5e93\u65e5\u5fd7\u8bb0\u5f55\uff0c\u76d1\u63a7\u548c\u5ba1\u8ba1\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><h3>\u5341\u3001\u793a\u4f8b\u9879\u76ee<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u9879\u76ee\uff0c\u5c55\u793a\u5982\u4f55\u7efc\u5408\u8fd0\u7528\u4e0a\u8ff0\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>def fetch_data():<\/p>\n<p>    try:<\/p>\n<p>        connection = pymysql.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>        cursor = connection.cursor()<\/p>\n<p>        sql = &quot;SELECT * FROM your_table&quot;<\/p>\n<p>        cursor.execute(sql)<\/p>\n<p>        results = cursor.fetchall()<\/p>\n<p>        for row in results:<\/p>\n<p>            print(row)<\/p>\n<p>    except pymysql.MySQLError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>    finally:<\/p>\n<p>        cursor.close()<\/p>\n<p>        connection.close()<\/p>\n<p>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;:<\/p>\n<p>    fetch_data()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>fetch_data<\/code>\u51fd\u6570\uff0c\u7528\u4e8e\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u636e\u5e76\u6253\u5370\u51fa\u6765\u3002\u901a\u8fc7\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u5c55\u793aPython\u8bbf\u95ee\u6570\u636e\u5e93\u8868\u7684\u5b8c\u6574\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u4e86\u89e3\u4e86Python\u8bbf\u95ee\u6570\u636e\u5e93\u8868\u7684\u5404\u4e2a\u6b65\u9aa4\uff0c\u5305\u62ec\u5b89\u88c5\u6570\u636e\u5e93\u9a71\u52a8\u3001\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\u3001\u6267\u884cSQL\u67e5\u8be2\u3001\u5904\u7406\u67e5\u8be2\u7ed3\u679c\u3001\u5173\u95ed\u8fde\u63a5\u3001\u9519\u8bef\u5904\u7406\u3001\u4f7f\u7528ORM\u6846\u67b6\u3001\u6027\u80fd\u4f18\u5316\u548c\u5b89\u5168\u6027\u7b49\u65b9\u9762\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u5e2e\u52a9\u4f60\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u66f4\u597d\u5730\u64cd\u4f5c\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u60a8\u9700\u8981\u4f7f\u7528\u9002\u5f53\u7684\u6570\u636e\u5e93\u9a71\u52a8\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8eMySQL\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>mysql-connector<\/code>\u6216<code>PyMySQL<\/code>\u5e93\uff1b\u5bf9\u4e8eSQLite\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sqlite3<\/code>\u6a21\u5757\u3002\u786e\u4fdd\u5b89\u88c5\u76f8\u5173\u5e93\uff0c\u5e76\u4f7f\u7528\u9002\u5f53\u7684\u8fde\u63a5\u5b57\u7b26\u4e32\u6765\u5efa\u7acb\u8fde\u63a5\u3002\u8fde\u63a5\u540e\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u6e38\u6807\u5bf9\u8c61\u6765\u6267\u884cSQL\u67e5\u8be2\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8bbf\u95ee\u6570\u636e\u5e93\u65f6\uff0c\u5982\u4f55\u5b89\u5168\u5730\u6267\u884cSQL\u67e5\u8be2\uff1f<\/strong><br \/>\u5b89\u5168\u5730\u6267\u884cSQL\u67e5\u8be2\u53ef\u4ee5\u9632\u6b62SQL\u6ce8\u5165\u653b\u51fb\u3002\u4f7f\u7528\u53c2\u6570\u5316\u67e5\u8be2\u6216\u9884\u7f16\u8bd1\u8bed\u53e5\u662f\u6709\u6548\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5728\u4f7f\u7528<code>sqlite3<\/code>\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6e38\u6807\u7684<code>execute<\/code>\u65b9\u6cd5\u4f20\u9012\u53c2\u6570\uff0c\u800c\u4e0d\u662f\u5c06\u53d8\u91cf\u76f4\u63a5\u62fc\u63a5\u5230SQL\u5b57\u7b26\u4e32\u4e2d\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u8f93\u5165\u6570\u636e\u7684\u5b89\u5168\u6027\uff0c\u51cf\u5c11\u6f5c\u5728\u7684\u5b89\u5168\u98ce\u9669\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u6570\u636e\u5e93\u67e5\u8be2\u7684\u7ed3\u679c\uff1f<\/strong><br \/>\u67e5\u8be2\u6570\u636e\u5e93\u540e\uff0c\u60a8\u4f1a\u5f97\u5230\u4e00\u4e2a\u7ed3\u679c\u96c6\u3002\u53ef\u4ee5\u4f7f\u7528\u6e38\u6807\u5bf9\u8c61\u7684\u65b9\u6cd5\uff0c\u5982<code>fetchone()<\/code>\u3001<code>fetchall()<\/code>\u6216<code>fetchmany(size)<\/code>\u6765\u83b7\u53d6\u7ed3\u679c\u3002\u6bcf\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u6b64\u5916\uff0c\u7ed3\u679c\u901a\u5e38\u4ee5\u5143\u7ec4\u6216\u5b57\u5178\u7684\u5f62\u5f0f\u8fd4\u56de\uff0c\u4fbf\u4e8e\u8fdb\u4e00\u6b65\u5904\u7406\u548c\u4f7f\u7528\u3002<\/p>\n<p><strong>\u5982\u679c\u5728\u8bbf\u95ee\u6570\u636e\u5e93\u65f6\u9047\u5230\u9519\u8bef\uff0c\u5e94\u8be5\u5982\u4f55\u8c03\u8bd5\uff1f<\/strong><br \/>\u5728\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\u3002\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\u975e\u5e38\u91cd\u8981\u3002\u4f7f\u7528<code>try-except<\/code>\u5757\u6765\u6355\u83b7\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\u7684\u5f02\u5e38\uff0c\u5e76\u6253\u5370\u51fa\u9519\u8bef\u4fe1\u606f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5b9a\u4f4d\u95ee\u9898\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u68c0\u67e5SQL\u8bed\u6cd5\u3001\u8fde\u63a5\u53c2\u6570\u548c\u6570\u636e\u5e93\u6743\u9650\u7b49\u5e38\u89c1\u9519\u8bef\u6765\u6e90\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bbf\u95ee\u6570\u636e\u5e93\u8868\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5\u6570\u636e\u5e93\u9a71\u52a8\u3001\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\u3001\u6267\u884cSQL\u67e5\u8be2\u3001\u5904\u7406\u67e5\u8be2\u7ed3\u679c\u3001\u5173\u95ed\u8fde\u63a5\u3002\u4ee5 [&hellip;]","protected":false},"author":3,"featured_media":1132348,"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\/1132341"}],"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=1132341"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132341\/revisions"}],"predecessor-version":[{"id":1132351,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132341\/revisions\/1132351"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1132348"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1132341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1132341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1132341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}