{"id":1098531,"date":"2025-01-08T15:22:01","date_gmt":"2025-01-08T07:22:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1098531.html"},"modified":"2025-01-08T15:22:03","modified_gmt":"2025-01-08T07:22:03","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e6%89%93%e5%bc%80%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bf%9e%e6%8e%a5-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1098531.html","title":{"rendered":"\u5982\u4f55\u7528python\u6253\u5f00\u6570\u636e\u5e93\u8fde\u63a5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25062658\/8db16736-6f4f-438d-91e0-51f0affda002.webp\" alt=\"\u5982\u4f55\u7528python\u6253\u5f00\u6570\u636e\u5e93\u8fde\u63a5\" \/><\/p>\n<p><p> <strong>\u7528Python\u6253\u5f00\u6570\u636e\u5e93\u8fde\u63a5\u7684\u6b65\u9aa4\u4e3a\uff1a\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u9a71\u52a8\u3001\u5b89\u88c5\u6570\u636e\u5e93\u9a71\u52a8\u5e93\u3001\u7f16\u5199\u8fde\u63a5\u4ee3\u7801\u3001\u5904\u7406\u8fde\u63a5\u5f02\u5e38\u3002<\/strong> \u4e0b\u9762\u5177\u4f53\u8bb2\u89e3\u5982\u4f55\u7528Python\u6253\u5f00\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u9a71\u52a8<\/h3>\n<\/p>\n<p><p>Python\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u5305\u62ecMySQL\u3001PostgreSQL\u3001SQLite\u3001Oracle\u7b49\u3002\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u9a71\u52a8\u662f\u6210\u529f\u8fde\u63a5\u6570\u636e\u5e93\u7684\u7b2c\u4e00\u6b65\u3002\u5e38\u7528\u7684\u6570\u636e\u5e93\u9a71\u52a8\u6709\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>MySQL<\/strong>: \u4f7f\u7528<code>mysql-connector-python<\/code>\u6216<code>PyMySQL<\/code><\/li>\n<li><strong>PostgreSQL<\/strong>: \u4f7f\u7528<code>psycopg2<\/code><\/li>\n<li><strong>SQLite<\/strong>: \u4f7f\u7528Python\u81ea\u5e26\u7684<code>sqlite3<\/code><\/li>\n<li><strong>Oracle<\/strong>: \u4f7f\u7528<code>cx_Oracle<\/code><\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u5b89\u88c5\u6570\u636e\u5e93\u9a71\u52a8\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u8fde\u63a5\u6570\u636e\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u5b89\u88c5\u76f8\u5e94\u7684\u6570\u636e\u5e93\u9a71\u52a8\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u547d\u4ee4\u6765\u5b89\u88c5\u3002\u4f8b\u5982\uff0c\u5b89\u88c5<code>mysql-connector-python<\/code>:<\/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>\u5b89\u88c5<code>psycopg2<\/code>:<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install psycopg2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5<code>cx_Oracle<\/code>:<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install cx_Oracle<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e8eSQLite\uff0c\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\uff0c\u56e0\u4e3aPython\u5185\u7f6e\u4e86<code>sqlite3<\/code>\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7f16\u5199\u8fde\u63a5\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u8fde\u63a5\u6570\u636e\u5e93\u7684\u4ee3\u7801\u4e00\u822c\u5305\u62ec\u56db\u4e2a\u6b65\u9aa4\uff1a\u521b\u5efa\u8fde\u63a5\u3001\u521b\u5efa\u6e38\u6807\u3001\u6267\u884cSQL\u8bed\u53e5\u3001\u5173\u95ed\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u8fde\u63a5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<h2><strong>\u521b\u5efa\u8fde\u63a5<\/strong><\/h2>\n<p>conn = 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<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e8ePostgreSQL:<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import psycopg2<\/p>\n<h2><strong>\u521b\u5efa\u8fde\u63a5<\/strong><\/h2>\n<p>conn = psycopg2.connect(<\/p>\n<p>    host=&quot;localhost&quot;,<\/p>\n<p>    database=&quot;database_name&quot;,<\/p>\n<p>    user=&quot;username&quot;,<\/p>\n<p>    password=&quot;password&quot;<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e8eSQLite:<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u521b\u5efa\u8fde\u63a5<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;database.db&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e8eOracle:<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cx_Oracle<\/p>\n<h2><strong>\u521b\u5efa\u8fde\u63a5<\/strong><\/h2>\n<p>conn = cx_Oracle.connect(&#39;username\/password@localhost:1521\/orcl&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u6e38\u6807<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">cursor = conn.cursor()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6267\u884cSQL\u8bed\u53e5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.execute(&quot;SELECT * FROM table_name&quot;)<\/p>\n<p>rows = cursor.fetchall()<\/p>\n<p>for row in rows:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u5173\u95ed\u8fde\u63a5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u8fde\u63a5\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u5e93\u8fde\u63a5\u53ef\u80fd\u4f1a\u51fa\u73b0\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u8fde\u63a5\u8d85\u65f6\u3001\u8ba4\u8bc1\u5931\u8d25\u7b49\u3002\u56e0\u6b64\uff0c\u9700\u8981\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6765\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5065\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<p>from mysql.connector import Error<\/p>\n<p>try:<\/p>\n<p>    conn = 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<p>    if conn.is_connected():<\/p>\n<p>        print(&quot;Successfully connected to the database&quot;)<\/p>\n<p>        cursor = conn.cursor()<\/p>\n<p>        cursor.execute(&quot;SELECT * FROM table_name&quot;)<\/p>\n<p>        rows = cursor.fetchall()<\/p>\n<p>        for row in rows:<\/p>\n<p>            print(row)<\/p>\n<p>except Error as e:<\/p>\n<p>    print(f&quot;Error: {e}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    if conn.is_connected():<\/p>\n<p>        cursor.close()<\/p>\n<p>        conn.close()<\/p>\n<p>        print(&quot;The connection is closed&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e8ePostgreSQL:<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import psycopg2<\/p>\n<p>from psycopg2 import OperationalError<\/p>\n<p>try:<\/p>\n<p>    conn = psycopg2.connect(<\/p>\n<p>        host=&quot;localhost&quot;,<\/p>\n<p>        database=&quot;database_name&quot;,<\/p>\n<p>        user=&quot;username&quot;,<\/p>\n<p>        password=&quot;password&quot;<\/p>\n<p>    )<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&quot;SELECT * FROM table_name&quot;)<\/p>\n<p>    rows = cursor.fetchall()<\/p>\n<p>    for row in rows:<\/p>\n<p>        print(row)<\/p>\n<p>except OperationalError as e:<\/p>\n<p>    print(f&quot;The error &#39;{e}&#39; occurred&quot;)<\/p>\n<p>finally:<\/p>\n<p>    if conn:<\/p>\n<p>        cursor.close()<\/p>\n<p>        conn.close()<\/p>\n<p>        print(&quot;The connection is closed&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e8eSQLite:<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>try:<\/p>\n<p>    conn = sqlite3.connect(&#39;database.db&#39;)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&quot;SELECT * FROM table_name&quot;)<\/p>\n<p>    rows = cursor.fetchall()<\/p>\n<p>    for row in rows:<\/p>\n<p>        print(row)<\/p>\n<p>except sqlite3.Error as e:<\/p>\n<p>    print(f&quot;Error: {e}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    if conn:<\/p>\n<p>        cursor.close()<\/p>\n<p>        conn.close()<\/p>\n<p>        print(&quot;The connection is closed&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e8eOracle:<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cx_Oracle<\/p>\n<p>try:<\/p>\n<p>    conn = cx_Oracle.connect(&#39;username\/password@localhost:1521\/orcl&#39;)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&quot;SELECT * FROM table_name&quot;)<\/p>\n<p>    rows = cursor.fetchall()<\/p>\n<p>    for row in rows:<\/p>\n<p>        print(row)<\/p>\n<p>except cx_Oracle.DatabaseError as e:<\/p>\n<p>    print(f&quot;Error: {e}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    if conn:<\/p>\n<p>        cursor.close()<\/p>\n<p>        conn.close()<\/p>\n<p>        print(&quot;The connection is closed&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u8fde\u63a5\u6c60<\/h3>\n<\/p>\n<p><p>\u5728\u9ad8\u5e76\u53d1\u7684\u573a\u666f\u4e0b\uff0c\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u6570\u636e\u5e93\u8fde\u63a5\u4f1a\u5e26\u6765\u5f88\u5927\u7684\u5f00\u9500\u3002\u4f7f\u7528\u8fde\u63a5\u6c60\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u9ad8\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u8fde\u63a5\u6c60\u7684\u4f8b\u5b50\u3002<\/p>\n<\/p>\n<p><h4>MySQL\u8fde\u63a5\u6c60<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<p>from mysql.connector import pooling<\/p>\n<p>dbconfig = {<\/p>\n<p>    &quot;database&quot;: &quot;database_name&quot;,<\/p>\n<p>    &quot;user&quot;:     &quot;username&quot;,<\/p>\n<p>    &quot;password&quot;: &quot;password&quot;,<\/p>\n<p>    &quot;host&quot;:     &quot;localhost&quot;<\/p>\n<p>}<\/p>\n<p>try:<\/p>\n<p>    connection_pool = mysql.connector.pooling.MySQLConnectionPool(<\/p>\n<p>        pool_name=&quot;mypool&quot;,<\/p>\n<p>        pool_size=5,<\/p>\n<p>        dbconfig<\/p>\n<p>    )<\/p>\n<p>    conn = connection_pool.get_connection()<\/p>\n<p>    if conn.is_connected():<\/p>\n<p>        print(&quot;Successfully connected to the database using connection pool&quot;)<\/p>\n<p>        cursor = conn.cursor()<\/p>\n<p>        cursor.execute(&quot;SELECT * FROM table_name&quot;)<\/p>\n<p>        rows = cursor.fetchall()<\/p>\n<p>        for row in rows:<\/p>\n<p>            print(row)<\/p>\n<p>finally:<\/p>\n<p>    if conn.is_connected():<\/p>\n<p>        cursor.close()<\/p>\n<p>        conn.close()<\/p>\n<p>        print(&quot;The connection is closed&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>PostgreSQL\u8fde\u63a5\u6c60<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import psycopg2<\/p>\n<p>from psycopg2 import pool<\/p>\n<p>try:<\/p>\n<p>    connection_pool = psycopg2.pool.SimpleConnectionPool(1, 20,<\/p>\n<p>                                                         user=&quot;username&quot;,<\/p>\n<p>                                                         password=&quot;password&quot;,<\/p>\n<p>                                                         host=&quot;localhost&quot;,<\/p>\n<p>                                                         port=&quot;5432&quot;,<\/p>\n<p>                                                         database=&quot;database_name&quot;)<\/p>\n<p>    if connection_pool:<\/p>\n<p>        print(&quot;Connection pool created successfully&quot;)<\/p>\n<p>    conn = connection_pool.getconn()<\/p>\n<p>    if conn:<\/p>\n<p>        print(&quot;Successfully connected to the database using connection pool&quot;)<\/p>\n<p>        cursor = conn.cursor()<\/p>\n<p>        cursor.execute(&quot;SELECT * FROM table_name&quot;)<\/p>\n<p>        rows = cursor.fetchall()<\/p>\n<p>        for row in rows:<\/p>\n<p>            print(row)<\/p>\n<p>finally:<\/p>\n<p>    if conn:<\/p>\n<p>        cursor.close()<\/p>\n<p>        connection_pool.putconn(conn)<\/p>\n<p>        print(&quot;The connection is returned to the pool&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>SQLite\u8fde\u63a5\u6c60<\/h4>\n<\/p>\n<p><p>SQLite\u4e0d\u652f\u6301\u8fde\u63a5\u6c60\uff0c\u56e0\u4e3a\u5b83\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u5e93\uff0c\u901a\u5e38\u7528\u4e8e\u5355\u7ebf\u7a0b\u548c\u5c0f\u578b\u9879\u76ee\u3002\u5982\u679c\u9700\u8981\u4f7f\u7528\u8fde\u63a5\u6c60\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5916\u90e8\u5e93\u5982<code>sqlite-pool<\/code>.<\/p>\n<\/p>\n<p><h4>Oracle\u8fde\u63a5\u6c60<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import cx_Oracle<\/p>\n<p>try:<\/p>\n<p>    pool = cx_Oracle.SessionPool(&quot;username&quot;, &quot;password&quot;, &quot;localhost:1521\/orcl&quot;, min=2, max=5, increment=1, threaded=True)<\/p>\n<p>    conn = pool.acquire()<\/p>\n<p>    print(&quot;Successfully connected to the database using connection pool&quot;)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&quot;SELECT * FROM table_name&quot;)<\/p>\n<p>    rows = cursor.fetchall()<\/p>\n<p>    for row in rows:<\/p>\n<p>        print(row)<\/p>\n<p>finally:<\/p>\n<p>    if conn:<\/p>\n<p>        cursor.close()<\/p>\n<p>        pool.release(conn)<\/p>\n<p>        print(&quot;The connection is returned to the pool&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528ORM<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u53ef\u4ee5\u7b80\u5316\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u5e38\u7528\u7684ORM\u5e93\u6709SQLAlchemy\u548cDjango ORM\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528SQLAlchemy<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine<\/p>\n<p>from sqlalchemy.orm import sessionmaker<\/p>\n<p>DATABASE_URL = &quot;mysql+mysqlconnector:\/\/username:password@localhost:3306\/database_name&quot;<\/p>\n<p>engine = create_engine(DATABASE_URL)<\/p>\n<p>Session = sessionmaker(bind=engine)<\/p>\n<p>session = Session()<\/p>\n<p>result = session.execute(&quot;SELECT * FROM table_name&quot;)<\/p>\n<p>for row in result:<\/p>\n<p>    print(row)<\/p>\n<p>session.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528Django ORM<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Django:<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install django<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u521b\u5efa\u4e00\u4e2aDjango\u9879\u76ee\uff0c\u5e76\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u3002\u5728<code>settings.py<\/code>\u4e2d\u914d\u7f6e\u6570\u636e\u5e93:<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">DATABASES = {<\/p>\n<p>    &#39;default&#39;: {<\/p>\n<p>        &#39;ENGINE&#39;: &#39;django.db.backends.mysql&#39;,<\/p>\n<p>        &#39;NAME&#39;: &#39;database_name&#39;,<\/p>\n<p>        &#39;USER&#39;: &#39;username&#39;,<\/p>\n<p>        &#39;PASSWORD&#39;: &#39;password&#39;,<\/p>\n<p>        &#39;HOST&#39;: &#39;localhost&#39;,<\/p>\n<p>        &#39;PORT&#39;: &#39;3306&#39;,<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528Django ORM\u8fdb\u884c\u67e5\u8be2:<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.db import models<\/p>\n<p>class MyModel(models.Model):<\/p>\n<p>    field1 = models.CharField(max_length=100)<\/p>\n<p>    field2 = models.IntegerField()<\/p>\n<p>results = MyModel.objects.all()<\/p>\n<p>for result in results:<\/p>\n<p>    print(result.field1, result.field2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\uff0c\u4f7f\u7528Python\u8fde\u63a5\u6570\u636e\u5e93\u6d89\u53ca\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u9a71\u52a8\u3001\u5b89\u88c5\u9a71\u52a8\u5e93\u3001\u7f16\u5199\u8fde\u63a5\u4ee3\u7801\u3001\u5904\u7406\u5f02\u5e38\u3001\u4f7f\u7528\u8fde\u63a5\u6c60\u53caORM\u7b49\u6b65\u9aa4\u3002\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u53ef\u4ee5\u9ad8\u6548\u3001\u7a33\u5065\u5730\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\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u5e93\u6765\u8fde\u63a5Python\uff1f<\/strong><br \/>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u5e93\u4e3b\u8981\u53d6\u51b3\u4e8e\u4f60\u6240\u4f7f\u7528\u7684\u6570\u636e\u5e93\u7c7b\u578b\u3002\u5e38\u89c1\u7684\u6570\u636e\u5e93\u5305\u62ecMySQL\u3001PostgreSQL\u3001SQLite\u3001MongoDB\u7b49\u3002\u5bf9\u4e8e\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff0c\u4f7f\u7528<code>MySQL Connector<\/code>\u6216<code>psycopg2<\/code>\uff08\u7528\u4e8ePostgreSQL\uff09\u662f\u5e38\u89c1\u7684\u9009\u62e9\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fSQLite\uff0c\u5185\u7f6e\u7684<code>sqlite3<\/code>\u5e93\u5c31\u80fd\u6ee1\u8db3\u9700\u6c42\u3002\u800c\u5bf9\u4e8e\u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff0c\u4f8b\u5982MongoDB\uff0c\u53ef\u4ee5\u4f7f\u7528<code>PyMongo<\/code>\u5e93\u3002\u786e\u4fdd\u9009\u62e9\u7684\u5e93\u4e0e\u6570\u636e\u5e93\u7c7b\u578b\u517c\u5bb9\uff0c\u5e76\u68c0\u67e5\u5176\u6587\u6863\u4ee5\u83b7\u53d6\u8be6\u7ec6\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u6570\u636e\u5e93\u8fde\u63a5\u9519\u8bef\uff1f<\/strong><br \/>\u5904\u7406\u6570\u636e\u5e93\u8fde\u63a5\u9519\u8bef\u662f\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u7a33\u5b9a\u6027\u7684\u91cd\u8981\u90e8\u5206\u3002\u5728\u6253\u5f00\u8fde\u63a5\u65f6\uff0c\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u5f02\u5e38\u3002\u5e38\u89c1\u7684\u5f02\u5e38\u5305\u62ec\u8fde\u63a5\u8d85\u65f6\u3001\u8ba4\u8bc1\u5931\u8d25\u7b49\u3002\u5728\u6355\u83b7\u5230\u5f02\u5e38\u540e\uff0c\u53ef\u4ee5\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\u5e76\u91c7\u53d6\u76f8\u5e94\u7684\u63aa\u65bd\uff0c\u6bd4\u5982\u91cd\u8bd5\u8fde\u63a5\u6216\u63d0\u793a\u7528\u6237\u68c0\u67e5\u6570\u636e\u5e93\u914d\u7f6e\u3002\u5229\u7528Python\u7684<code>logging<\/code>\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u6545\u969c\u6392\u67e5\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u5168\u5730\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\uff1f<\/strong><br \/>\u786e\u4fdd\u6570\u636e\u5e93\u8fde\u63a5\u88ab\u6b63\u786e\u5173\u95ed\u662f\u4e00\u4e2a\u826f\u597d\u7684\u7f16\u7a0b\u4e60\u60ef\u3002\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u8fde\u63a5\u7684\u6253\u5f00\u4e0e\u5173\u95ed\uff0c\u907f\u514d\u8fde\u63a5\u6cc4\u9732\u3002\u4f8b\u5982\uff0c\u5728\u4f7f\u7528<code>sqlite3<\/code>\u5e93\u65f6\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<code>with sqlite3.connect(&#39;database.db&#39;) as conn:<\/code>\u3002\u5728<code>with<\/code>\u5757\u7ed3\u675f\u65f6\uff0c\u8fde\u63a5\u4f1a\u81ea\u52a8\u5173\u95ed\u3002\u5982\u679c\u4e0d\u4f7f\u7528<code>with<\/code>\uff0c\u52a1\u5fc5\u5728\u5b8c\u6210\u64cd\u4f5c\u540e\u8c03\u7528<code>connection.close()<\/code>\u65b9\u6cd5\u6765\u624b\u52a8\u5173\u95ed\u8fde\u63a5\uff0c\u786e\u4fdd\u8d44\u6e90\u5f97\u5230\u91ca\u653e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u6253\u5f00\u6570\u636e\u5e93\u8fde\u63a5\u7684\u6b65\u9aa4\u4e3a\uff1a\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u9a71\u52a8\u3001\u5b89\u88c5\u6570\u636e\u5e93\u9a71\u52a8\u5e93\u3001\u7f16\u5199\u8fde\u63a5\u4ee3\u7801\u3001\u5904\u7406\u8fde\u63a5\u5f02\u5e38\u3002 \u4e0b [&hellip;]","protected":false},"author":3,"featured_media":1098538,"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\/1098531"}],"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=1098531"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098531\/revisions"}],"predecessor-version":[{"id":1098539,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098531\/revisions\/1098539"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1098538"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1098531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1098531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1098531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}