{"id":1139884,"date":"2025-01-08T22:14:43","date_gmt":"2025-01-08T14:14:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1139884.html"},"modified":"2025-01-08T22:14:45","modified_gmt":"2025-01-08T14:14:45","slug":"python-%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e6%95%b0%e6%8d%ae%e5%ba%93%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bf%9e%e6%8e%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1139884.html","title":{"rendered":"python \u5982\u4f55\u6dfb\u52a0\u6570\u636e\u5e93\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\/25103028\/6bf14ee6-9277-46c1-84fd-5f4d1b441a5e.webp\" alt=\"python \u5982\u4f55\u6dfb\u52a0\u6570\u636e\u5e93\u6570\u636e\u5e93\u8fde\u63a5\" \/><\/p>\n<p><p> <strong>Python \u6dfb\u52a0\u6570\u636e\u5e93\u8fde\u63a5\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u9002\u5f53\u7684\u6570\u636e\u5e93\u9a71\u52a8\u3001\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u53c2\u6570\u3001\u5efa\u7acb\u8fde\u63a5\u548c\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c<\/strong>\u3002\u5176\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u9a71\u52a8\u662f\u5173\u952e\uff0c\u56e0\u4e3a\u4e0d\u540c\u7684\u6570\u636e\u5e93\u7cfb\u7edf\u9700\u8981\u4e0d\u540c\u7684\u9a71\u52a8\u3002\u4f8b\u5982\uff0cMySQL \u4f7f\u7528 <code>mysql-connector-python<\/code> \u6216 <code>PyMySQL<\/code>\uff0cPostgreSQL \u4f7f\u7528 <code>psycopg2<\/code>\uff0cSQLite \u4f7f\u7528 Python \u5185\u7f6e\u7684 <code>sqlite3<\/code>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u9a71\u52a8\u6dfb\u52a0\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u9009\u62e9\u9002\u5f53\u7684\u6570\u636e\u5e93\u9a71\u52a8<\/p>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u6570\u636e\u5e93\u7cfb\u7edf\u9700\u8981\u4e0d\u540c\u7684\u9a71\u52a8\u7a0b\u5e8f\u3002\u9009\u62e9\u5408\u9002\u7684\u9a71\u52a8\u7a0b\u5e8f\u662f\u6dfb\u52a0\u6570\u636e\u5e93\u8fde\u63a5\u7684\u7b2c\u4e00\u6b65\u3002<\/p>\n<\/p>\n<p><h3>MySQL<\/h3>\n<\/p>\n<p><p>MySQL \u662f\u4e00\u79cd\u6d41\u884c\u7684\u5173\u7cfb\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\u3002Python \u4e2d\u5e38\u7528\u7684 MySQL \u9a71\u52a8\u5305\u62ec <code>mysql-connector-python<\/code> \u548c <code>PyMySQL<\/code>\u3002\u4e0b\u9762\u662f\u5982\u4f55\u4f7f\u7528\u8fd9\u4e24\u79cd\u9a71\u52a8\u8fde\u63a5 MySQL \u6570\u636e\u5e93\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528 mysql-connector-python<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5 <code>mysql-connector-python<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install mysql-connector-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fde\u63a5\u5230 MySQL \u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<p>def connect_to_mysql():<\/p>\n<p>    try:<\/p>\n<p>        conn = mysql.connector.connect(<\/p>\n<p>            host=&#39;localhost&#39;,<\/p>\n<p>            user=&#39;yourusername&#39;,<\/p>\n<p>            password=&#39;yourpassword&#39;,<\/p>\n<p>            database=&#39;yourdatabase&#39;<\/p>\n<p>        )<\/p>\n<p>        if conn.is_connected():<\/p>\n<p>            print(&#39;Connected to MySQL database&#39;)<\/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 conn.is_connected():<\/p>\n<p>            conn.close()<\/p>\n<p>connect_to_mysql()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528 PyMySQL<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5 <code>PyMySQL<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install pymysql<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fde\u63a5\u5230 MySQL \u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>def connect_to_mysql():<\/p>\n<p>    try:<\/p>\n<p>        conn = pymysql.connect(<\/p>\n<p>            host=&#39;localhost&#39;,<\/p>\n<p>            user=&#39;yourusername&#39;,<\/p>\n<p>            password=&#39;yourpassword&#39;,<\/p>\n<p>            database=&#39;yourdatabase&#39;<\/p>\n<p>        )<\/p>\n<p>        print(&#39;Connected to MySQL database&#39;)<\/p>\n<p>    except pymysql.MySQLError as err:<\/p>\n<p>        print(f&quot;Error: {err}&quot;)<\/p>\n<p>    finally:<\/p>\n<p>        conn.close()<\/p>\n<p>connect_to_mysql()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>PostgreSQL<\/h3>\n<\/p>\n<p><p>PostgreSQL \u662f\u53e6\u4e00\u79cd\u6d41\u884c\u7684\u5173\u7cfb\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\u3002Python \u4e2d\u5e38\u7528\u7684 PostgreSQL \u9a71\u52a8\u662f <code>psycopg2<\/code>\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528 psycopg2<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5 <code>psycopg2<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install psycopg2-binary<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fde\u63a5\u5230 PostgreSQL \u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import psycopg2<\/p>\n<p>def connect_to_postgresql():<\/p>\n<p>    try:<\/p>\n<p>        conn = psycopg2.connect(<\/p>\n<p>            host=&#39;localhost&#39;,<\/p>\n<p>            user=&#39;yourusername&#39;,<\/p>\n<p>            password=&#39;yourpassword&#39;,<\/p>\n<p>            dbname=&#39;yourdatabase&#39;<\/p>\n<p>        )<\/p>\n<p>        print(&#39;Connected to PostgreSQL database&#39;)<\/p>\n<p>    except psycopg2.Error as err:<\/p>\n<p>        print(f&quot;Error: {err}&quot;)<\/p>\n<p>    finally:<\/p>\n<p>        conn.close()<\/p>\n<p>connect_to_postgresql()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>SQLite<\/h3>\n<\/p>\n<p><p>SQLite \u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u5d4c\u5165\u5f0f\u6570\u636e\u5e93\u3002Python \u6807\u51c6\u5e93\u81ea\u5e26 <code>sqlite3<\/code> \u6a21\u5757\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528 sqlite3<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fde\u63a5\u5230 SQLite \u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>def connect_to_sqlite():<\/p>\n<p>    try:<\/p>\n<p>        conn = sqlite3.connect(&#39;yourdatabase.db&#39;)<\/p>\n<p>        print(&#39;Connected to SQLite database&#39;)<\/p>\n<p>    except sqlite3.Error as err:<\/p>\n<p>        print(f&quot;Error: {err}&quot;)<\/p>\n<p>    finally:<\/p>\n<p>        conn.close()<\/p>\n<p>connect_to_sqlite()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u53c2\u6570<\/p>\n<\/p>\n<p><p>\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u53c2\u6570\u662f\u6dfb\u52a0\u6570\u636e\u5e93\u8fde\u63a5\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u4e0d\u540c\u7684\u6570\u636e\u5e93\u9a71\u52a8\u6709\u4e0d\u540c\u7684\u53c2\u6570\u914d\u7f6e\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>MySQL<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e MySQL\uff0c\u5e38\u7528\u7684\u8fde\u63a5\u53c2\u6570\u5305\u62ec <code>host<\/code>\u3001<code>user<\/code>\u3001<code>password<\/code> \u548c <code>database<\/code>\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u914d\u7f6e <code>port<\/code>\u3001<code>charset<\/code> \u7b49\u53c2\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">conn = mysql.connector.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;yourusername&#39;,<\/p>\n<p>    password=&#39;yourpassword&#39;,<\/p>\n<p>    database=&#39;yourdatabase&#39;,<\/p>\n<p>    port=3306,<\/p>\n<p>    charset=&#39;utf8mb4&#39;<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>PostgreSQL<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e PostgreSQL\uff0c\u5e38\u7528\u7684\u8fde\u63a5\u53c2\u6570\u5305\u62ec <code>host<\/code>\u3001<code>user<\/code>\u3001<code>password<\/code> \u548c <code>dbname<\/code>\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u914d\u7f6e <code>port<\/code>\u3001<code>sslmode<\/code> \u7b49\u53c2\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">conn = psycopg2.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;yourusername&#39;,<\/p>\n<p>    password=&#39;yourpassword&#39;,<\/p>\n<p>    dbname=&#39;yourdatabase&#39;,<\/p>\n<p>    port=5432,<\/p>\n<p>    sslmode=&#39;require&#39;<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>SQLite<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e SQLite\uff0c\u8fde\u63a5\u53c2\u6570\u901a\u5e38\u662f\u6570\u636e\u5e93\u6587\u4ef6\u7684\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">conn = sqlite3.connect(&#39;yourdatabase.db&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5efa\u7acb\u8fde\u63a5\u548c\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u5efa\u7acb\u8fde\u63a5\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fde\u63a5\u5bf9\u8c61\u6267\u884c\u5404\u79cd\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u5982\u67e5\u8be2\u3001\u63d2\u5165\u3001\u66f4\u65b0\u548c\u5220\u9664\u3002<\/p>\n<\/p>\n<p><h3>MySQL<\/h3>\n<\/p>\n<p><h4>\u67e5\u8be2\u64cd\u4f5c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def query_mysql():<\/p>\n<p>    try:<\/p>\n<p>        conn = mysql.connector.connect(<\/p>\n<p>            host=&#39;localhost&#39;,<\/p>\n<p>            user=&#39;yourusername&#39;,<\/p>\n<p>            password=&#39;yourpassword&#39;,<\/p>\n<p>            database=&#39;yourdatabase&#39;<\/p>\n<p>        )<\/p>\n<p>        cursor = conn.cursor()<\/p>\n<p>        cursor.execute(&quot;SELECT * FROM yourtable&quot;)<\/p>\n<p>        rows = cursor.fetchall()<\/p>\n<p>        for row in rows:<\/p>\n<p>            print(row)<\/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>        conn.close()<\/p>\n<p>query_mysql()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u63d2\u5165\u64cd\u4f5c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def insert_mysql():<\/p>\n<p>    try:<\/p>\n<p>        conn = mysql.connector.connect(<\/p>\n<p>            host=&#39;localhost&#39;,<\/p>\n<p>            user=&#39;yourusername&#39;,<\/p>\n<p>            password=&#39;yourpassword&#39;,<\/p>\n<p>            database=&#39;yourdatabase&#39;<\/p>\n<p>        )<\/p>\n<p>        cursor = conn.cursor()<\/p>\n<p>        cursor.execute(&quot;INSERT INTO yourtable (column1, column2) VALUES (%s, %s)&quot;, (&#39;value1&#39;, &#39;value2&#39;))<\/p>\n<p>        conn.commit()<\/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>        conn.close()<\/p>\n<p>insert_mysql()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>PostgreSQL<\/h3>\n<\/p>\n<p><h4>\u67e5\u8be2\u64cd\u4f5c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def query_postgresql():<\/p>\n<p>    try:<\/p>\n<p>        conn = psycopg2.connect(<\/p>\n<p>            host=&#39;localhost&#39;,<\/p>\n<p>            user=&#39;yourusername&#39;,<\/p>\n<p>            password=&#39;yourpassword&#39;,<\/p>\n<p>            dbname=&#39;yourdatabase&#39;<\/p>\n<p>        )<\/p>\n<p>        cursor = conn.cursor()<\/p>\n<p>        cursor.execute(&quot;SELECT * FROM yourtable&quot;)<\/p>\n<p>        rows = cursor.fetchall()<\/p>\n<p>        for row in rows:<\/p>\n<p>            print(row)<\/p>\n<p>    except psycopg2.Error as err:<\/p>\n<p>        print(f&quot;Error: {err}&quot;)<\/p>\n<p>    finally:<\/p>\n<p>        cursor.close()<\/p>\n<p>        conn.close()<\/p>\n<p>query_postgresql()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u63d2\u5165\u64cd\u4f5c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def insert_postgresql():<\/p>\n<p>    try:<\/p>\n<p>        conn = psycopg2.connect(<\/p>\n<p>            host=&#39;localhost&#39;,<\/p>\n<p>            user=&#39;yourusername&#39;,<\/p>\n<p>            password=&#39;yourpassword&#39;,<\/p>\n<p>            dbname=&#39;yourdatabase&#39;<\/p>\n<p>        )<\/p>\n<p>        cursor = conn.cursor()<\/p>\n<p>        cursor.execute(&quot;INSERT INTO yourtable (column1, column2) VALUES (%s, %s)&quot;, (&#39;value1&#39;, &#39;value2&#39;))<\/p>\n<p>        conn.commit()<\/p>\n<p>    except psycopg2.Error as err:<\/p>\n<p>        print(f&quot;Error: {err}&quot;)<\/p>\n<p>    finally:<\/p>\n<p>        cursor.close()<\/p>\n<p>        conn.close()<\/p>\n<p>insert_postgresql()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>SQLite<\/h3>\n<\/p>\n<p><h4>\u67e5\u8be2\u64cd\u4f5c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def query_sqlite():<\/p>\n<p>    try:<\/p>\n<p>        conn = sqlite3.connect(&#39;yourdatabase.db&#39;)<\/p>\n<p>        cursor = conn.cursor()<\/p>\n<p>        cursor.execute(&quot;SELECT * FROM yourtable&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 err:<\/p>\n<p>        print(f&quot;Error: {err}&quot;)<\/p>\n<p>    finally:<\/p>\n<p>        cursor.close()<\/p>\n<p>        conn.close()<\/p>\n<p>query_sqlite()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u63d2\u5165\u64cd\u4f5c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def insert_sqlite():<\/p>\n<p>    try:<\/p>\n<p>        conn = sqlite3.connect(&#39;yourdatabase.db&#39;)<\/p>\n<p>        cursor = conn.cursor()<\/p>\n<p>        cursor.execute(&quot;INSERT INTO yourtable (column1, column2) VALUES (?, ?)&quot;, (&#39;value1&#39;, &#39;value2&#39;))<\/p>\n<p>        conn.commit()<\/p>\n<p>    except sqlite3.Error as err:<\/p>\n<p>        print(f&quot;Error: {err}&quot;)<\/p>\n<p>    finally:<\/p>\n<p>        cursor.close()<\/p>\n<p>        conn.close()<\/p>\n<p>insert_sqlite()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u5f02\u5e38\u548c\u5173\u95ed\u8fde\u63a5<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\uff0c\u5904\u7406\u5f02\u5e38\u548c\u5173\u95ed\u8fde\u63a5\u662f\u5fc5\u8981\u7684\u6b65\u9aa4\uff0c\u4ee5\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u548c\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u5904\u7406\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\uff0c\u5982\u8fde\u63a5\u5931\u8d25\u3001\u67e5\u8be2\u5931\u8d25\u7b49\u3002\u4f7f\u7528 <code>try-except<\/code> \u5757\u53ef\u4ee5\u6709\u6548\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    conn = mysql.connector.connect(<\/p>\n<p>        host=&#39;localhost&#39;,<\/p>\n<p>        user=&#39;yourusername&#39;,<\/p>\n<p>        password=&#39;yourpassword&#39;,<\/p>\n<p>        database=&#39;yourdatabase&#39;<\/p>\n<p>    )<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&quot;SELECT * FROM yourtable&quot;)<\/p>\n<p>    rows = cursor.fetchall()<\/p>\n<p>    for row in rows:<\/p>\n<p>        print(row)<\/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>    conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5173\u95ed\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u6570\u636e\u5e93\u64cd\u4f5c\u540e\uff0c\u5173\u95ed\u8fde\u63a5\u662f\u5fc5\u8981\u7684\uff0c\u4ee5\u91ca\u653e\u8d44\u6e90\u5e76\u907f\u514d\u8fde\u63a5\u6cc4\u6f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">finally:<\/p>\n<p>    cursor.close()<\/p>\n<p>    conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528 ORM \u6846\u67b6<\/p>\n<\/p>\n<p><p>\u4f7f\u7528 ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u6846\u67b6\uff0c\u5982 SQLAlchemy\uff0c\u53ef\u4ee5\u7b80\u5316\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u5e76\u63d0\u4f9b\u66f4\u9ad8\u5c42\u6b21\u7684\u62bd\u8c61\u3002<\/p>\n<\/p>\n<p><h3>SQLAlchemy<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5 SQLAlchemy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install sqlalchemy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fde\u63a5\u5230\u6570\u636e\u5e93\u5e76\u6267\u884c\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine, Column, Integer, String, Sequence<\/p>\n<p>from sqlalchemy.ext.declarative import declarative_base<\/p>\n<p>from sqlalchemy.orm import sessionmaker<\/p>\n<p>Base = declarative_base()<\/p>\n<p>class User(Base):<\/p>\n<p>    __tablename__ = &#39;users&#39;<\/p>\n<p>    id = Column(Integer, Sequence(&#39;user_id_seq&#39;), primary_key=True)<\/p>\n<p>    name = Column(String(50))<\/p>\n<p>    fullname = Column(String(50))<\/p>\n<p>    nickname = Column(String(50))<\/p>\n<p>engine = create_engine(&#39;sqlite:\/\/\/example.db&#39;)<\/p>\n<p>Base.metadata.create_all(engine)<\/p>\n<p>Session = sessionmaker(bind=engine)<\/p>\n<p>session = Session()<\/p>\n<p>new_user = User(name=&#39;John&#39;, fullname=&#39;John Doe&#39;, nickname=&#39;johnny&#39;)<\/p>\n<p>session.add(new_user)<\/p>\n<p>session.commit()<\/p>\n<p>for instance in session.query(User).order_by(User.id):<\/p>\n<p>    print(instance.name, instance.fullname)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u6dfb\u52a0\u6570\u636e\u5e93\u8fde\u63a5\u662f Python \u5e94\u7528\u7a0b\u5e8f\u4e2d\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u9009\u62e9\u9002\u5f53\u7684\u6570\u636e\u5e93\u9a71\u52a8\u3001\u914d\u7f6e\u8fde\u63a5\u53c2\u6570\u3001\u5efa\u7acb\u8fde\u63a5\u548c\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u662f\u5173\u952e\u6b65\u9aa4\u3002\u5904\u7406\u5f02\u5e38\u548c\u5173\u95ed\u8fde\u63a5\u662f\u4fdd\u8bc1\u7a0b\u5e8f\u5065\u58ee\u6027\u7684\u5fc5\u8981\u6b65\u9aa4\u3002\u6b64\u5916\uff0c\u4f7f\u7528 ORM \u6846\u67b6\u53ef\u4ee5\u7b80\u5316\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002\u65e0\u8bba\u662f MySQL\u3001PostgreSQL \u8fd8\u662f SQLite\uff0cPython \u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u5e93\u6765\u652f\u6301\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u9009\u62e9\u591a\u79cd\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff0c\u5982SQLite\u3001MySQL Connector\u3001Psycopg2\uff08\u7528\u4e8ePostgreSQL\uff09\u7b49\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u901a\u5e38\u53d6\u51b3\u4e8e\u60a8\u6b63\u5728\u4f7f\u7528\u7684\u6570\u636e\u5e93\u7c7b\u578b\u3001\u9879\u76ee\u7684\u89c4\u6a21\u4ee5\u53ca\u5bf9\u6027\u80fd\u7684\u8981\u6c42\u3002SQLite\u9002\u5408\u5c0f\u578b\u5e94\u7528\uff0c\u800cMySQL\u548cPostgreSQL\u66f4\u9002\u5408\u5927\u578b\u5e94\u7528\u3002\u786e\u4fdd\u9009\u62e9\u7684\u5e93\u5177\u6709\u826f\u597d\u7684\u6587\u6863\u548c\u793e\u533a\u652f\u6301\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u8fde\u63a5\u6570\u636e\u5e93\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8fde\u63a5\u6570\u636e\u5e93\u901a\u5e38\u5305\u62ec\u51e0\u4e2a\u6b65\u9aa4\uff1a\u9996\u5148\uff0c\u5b89\u88c5\u6240\u9700\u7684\u6570\u636e\u5e93\u9a71\u52a8\u7a0b\u5e8f\uff1b\u5176\u6b21\uff0c\u5bfc\u5165\u76f8\u5e94\u7684\u5e93\uff1b\u63a5\u7740\uff0c\u4f7f\u7528\u8fde\u63a5\u5b57\u7b26\u4e32\u5efa\u7acb\u8fde\u63a5\uff1b\u6700\u540e\uff0c\u521b\u5efa\u6e38\u6807\u5bf9\u8c61\u4ee5\u6267\u884cSQL\u67e5\u8be2\u3002\u786e\u4fdd\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u5173\u95ed\u8fde\u63a5\uff0c\u4ee5\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u6570\u636e\u5e93\u8fde\u63a5\u4e2d\u7684\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u4f8b\u5982\u8fde\u63a5\u5931\u8d25\u6216\u67e5\u8be2\u9519\u8bef\u3002\u4f7f\u7528try-except\u8bed\u53e5\u53ef\u4ee5\u6709\u6548\u6355\u6349\u8fd9\u4e9b\u5f02\u5e38\uff0c\u5e76\u91c7\u53d6\u9002\u5f53\u7684\u63aa\u65bd\uff0c\u5982\u91cd\u8bd5\u8fde\u63a5\u6216\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08with\u8bed\u53e5\uff09\u53ef\u4ee5\u786e\u4fdd\u5728\u53d1\u751f\u5f02\u5e38\u65f6\u81ea\u52a8\u5173\u95ed\u8fde\u63a5\uff0c\u51cf\u5c11\u8d44\u6e90\u6cc4\u6f0f\u7684\u98ce\u9669\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u6dfb\u52a0\u6570\u636e\u5e93\u8fde\u63a5\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u9002\u5f53\u7684\u6570\u636e\u5e93\u9a71\u52a8\u3001\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u53c2\u6570\u3001\u5efa\u7acb\u8fde\u63a5\u548c\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1139895,"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\/1139884"}],"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=1139884"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139884\/revisions"}],"predecessor-version":[{"id":1139896,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139884\/revisions\/1139896"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1139895"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1139884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1139884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1139884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}