{"id":1087826,"date":"2025-01-08T13:38:40","date_gmt":"2025-01-08T05:38:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1087826.html"},"modified":"2025-01-08T13:38:42","modified_gmt":"2025-01-08T05:38:42","slug":"python-%e5%a6%82%e4%bd%95%e5%88%b6%e4%bd%9c%e6%95%b0%e6%8d%ae%e5%ba%93%e7%a8%8b%e5%ba%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1087826.html","title":{"rendered":"python \u5982\u4f55\u5236\u4f5c\u6570\u636e\u5e93\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200544\/ba21307b-c800-40bb-9252-31e974099452.webp\" alt=\"python \u5982\u4f55\u5236\u4f5c\u6570\u636e\u5e93\u7a0b\u5e8f\" \/><\/p>\n<p><p> <strong>\u5236\u4f5c\u6570\u636e\u5e93\u7a0b\u5e8f\u6d89\u53ca\u7684\u6838\u5fc3\u8981\u70b9\u5305\u62ec\u9009\u62e9\u6570\u636e\u5e93\u3001\u5b89\u88c5\u76f8\u5173\u5e93\u3001\u8fde\u63a5\u6570\u636e\u5e93\u3001\u521b\u5efa\u8868\u3001\u63d2\u5165\u6570\u636e\u548c\u67e5\u8be2\u6570\u636e\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u5236\u4f5c\u4e00\u4e2a\u6570\u636e\u5e93\u7a0b\u5e8f\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u9009\u62e9\u6570\u636e\u5e93\u7c7b\u578b<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u4e00\u4e2a\u6570\u636e\u5e93\u7c7b\u578b\u662f\u5236\u4f5c\u6570\u636e\u5e93\u7a0b\u5e8f\u7684\u7b2c\u4e00\u6b65\u3002\u5e38\u89c1\u7684\u6570\u636e\u5e93\u7c7b\u578b\u6709\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff08\u5982MySQL\u3001PostgreSQL\u3001SQLite\uff09\u548c\u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff08\u5982MongoDB\u3001Redis\uff09\u3002\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u672c\u6587\u5c06\u4e3b\u8981\u8bb2\u8ff0\u5982\u4f55\u4f7f\u7528SQLite\uff0c\u56e0\u4e3a\u5b83\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff0c\u5e76\u4e14Python\u5185\u7f6e\u652f\u6301\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5b89\u88c5\u76f8\u5173\u5e93<\/p>\n<\/p>\n<p><p>\u867d\u7136Python\u5185\u7f6e\u652f\u6301SQLite\uff0c\u4f46\u5982\u679c\u9009\u62e9\u5176\u4ed6\u6570\u636e\u5e93\u7c7b\u578b\uff0c\u4f60\u9700\u8981\u5b89\u88c5\u76f8\u5e94\u7684\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u6570\u636e\u5e93\u7c7b\u578b\u53ca\u5176\u76f8\u5e94\u7684Python\u5e93\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>MySQL<\/strong>\uff1a<code>mysql-connector-python<\/code> \u6216 <code>PyMySQL<\/code><\/li>\n<li><strong>PostgreSQL<\/strong>\uff1a<code>psycopg2<\/code><\/li>\n<li><strong>MongoDB<\/strong>\uff1a<code>pymongo<\/code><\/li>\n<\/ul>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install mysql-connector-python<\/p>\n<p>pip install psycopg2<\/p>\n<p>pip install pymongo<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u8fde\u63a5\u6570\u636e\u5e93<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u8fde\u63a5\u5230\u6570\u636e\u5e93\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528Python\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93\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>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = conn.cursor()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u521b\u5efa\u8868<\/p>\n<\/p>\n<p><p>\u8fde\u63a5\u5230\u6570\u636e\u5e93\u540e\uff0c\u4f60\u9700\u8981\u521b\u5efa\u8868\u6765\u5b58\u50a8\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528SQL\u8bed\u53e5\u5728SQLite\u4e2d\u521b\u5efa\u8868\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u8868<\/p>\n<p>cursor.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS users<\/p>\n<p>                  (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)&#39;&#39;&#39;)<\/p>\n<h2><strong>\u63d0\u4ea4\u66f4\u6539<\/strong><\/h2>\n<p>conn.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u521b\u5efa\u8868\u65f6\uff0c\u4f60\u9700\u8981\u6307\u5b9a\u6bcf\u4e2a\u5217\u7684\u540d\u79f0\u548c\u6570\u636e\u7c7b\u578b\u3002\u5e38\u89c1\u7684\u6570\u636e\u7c7b\u578b\u5305\u62ec INTEGER\u3001TEXT\u3001REAL \u548c BLOB\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e94\u3001\u63d2\u5165\u6570\u636e<\/p>\n<\/p>\n<p><p>\u521b\u5efa\u8868\u540e\uff0c\u4f60\u53ef\u4ee5\u63d2\u5165\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528SQL\u8bed\u53e5\u63d2\u5165\u6570\u636e\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63d2\u5165\u6570\u636e<\/p>\n<p>cursor.execute(&quot;INSERT INTO users (name, age) VALUES (?, ?)&quot;, (&#39;Alice&#39;, 30))<\/p>\n<p>cursor.execute(&quot;INSERT INTO users (name, age) VALUES (?, ?)&quot;, (&#39;Bob&#39;, 25))<\/p>\n<h2><strong>\u63d0\u4ea4\u66f4\u6539<\/strong><\/h2>\n<p>conn.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u63d2\u5165\u6570\u636e\u65f6\uff0c\u4f60\u9700\u8981\u4f7f\u7528\u5360\u4f4d\u7b26\u6765\u9632\u6b62SQL\u6ce8\u5165\u653b\u51fb\u3002\u5360\u4f4d\u7b26\u5728\u4e0d\u540c\u7684\u6570\u636e\u5e93\u4e2d\u53ef\u80fd\u6709\u6240\u4e0d\u540c\uff0c\u4f8b\u5982 SQLite \u4f7f\u7528\u95ee\u53f7\uff08?\uff09\uff0c\u800c PostgreSQL \u4f7f\u7528\u767e\u5206\u53f7\uff08%s\uff09\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u516d\u3001\u67e5\u8be2\u6570\u636e<\/p>\n<\/p>\n<p><p>\u63d2\u5165\u6570\u636e\u540e\uff0c\u4f60\u53ef\u4ee5\u67e5\u8be2\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528SQL\u8bed\u53e5\u67e5\u8be2\u6570\u636e\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u67e5\u8be2\u6570\u636e<\/p>\n<p>cursor.execute(&quot;SELECT * FROM users&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c<\/strong><\/h2>\n<p>rows = cursor.fetchall()<\/p>\n<h2><strong>\u6253\u5370\u67e5\u8be2\u7ed3\u679c<\/strong><\/h2>\n<p>for row in rows:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u67e5\u8be2\u6570\u636e\u65f6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5404\u79cdSQL\u8bed\u53e5\u6765\u7b5b\u9009\u3001\u6392\u5e8f\u548c\u8fc7\u6ee4\u6570\u636e\u3002fetchall() \u65b9\u6cd5\u8fd4\u56de\u6240\u6709\u67e5\u8be2\u7ed3\u679c\uff0c\u800c fetchone() \u65b9\u6cd5\u53ea\u8fd4\u56de\u7b2c\u4e00\u6761\u8bb0\u5f55\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e03\u3001\u66f4\u65b0\u548c\u5220\u9664\u6570\u636e<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u63d2\u5165\u548c\u67e5\u8be2\u6570\u636e\uff0c\u4f60\u8fd8\u53ef\u4ee5\u66f4\u65b0\u548c\u5220\u9664\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528SQL\u8bed\u53e5\u66f4\u65b0\u548c\u5220\u9664\u6570\u636e\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u66f4\u65b0\u6570\u636e<\/p>\n<p>cursor.execute(&quot;UPDATE users SET age = ? WHERE name = ?&quot;, (35, &#39;Alice&#39;))<\/p>\n<h2><strong>\u5220\u9664\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&quot;DELETE FROM users WHERE name = ?&quot;, (&#39;Bob&#39;,))<\/p>\n<h2><strong>\u63d0\u4ea4\u66f4\u6539<\/strong><\/h2>\n<p>conn.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u66f4\u65b0\u6570\u636e\u65f6\uff0c\u4f60\u9700\u8981\u6307\u5b9a\u8981\u66f4\u65b0\u7684\u5217\u548c\u6761\u4ef6\u3002\u5220\u9664\u6570\u636e\u65f6\uff0c\u4f60\u9700\u8981\u6307\u5b9a\u8981\u5220\u9664\u7684\u8bb0\u5f55\u7684\u6761\u4ef6\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u516b\u3001\u5173\u95ed\u8fde\u63a5<\/p>\n<\/p>\n<p><p>\u5b8c\u6210\u6570\u636e\u5e93\u64cd\u4f5c\u540e\uff0c\u4f60\u9700\u8981\u5173\u95ed\u8fde\u63a5\u4ee5\u91ca\u653e\u8d44\u6e90\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5173\u95ed\u8fde\u63a5\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5173\u95ed\u6e38\u6807<\/p>\n<p>cursor.close()<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5173\u95ed\u8fde\u63a5\u65f6\uff0c\u4f60\u9700\u8981\u5148\u5173\u95ed\u6e38\u6807\u5bf9\u8c61\uff0c\u7136\u540e\u5173\u95ed\u8fde\u63a5\u5bf9\u8c61\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u5904\u7406\u5f02\u5e38<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6570\u636e\u5e93\u64cd\u4f5c\u53ef\u80fd\u4f1a\u51fa\u73b0\u5404\u79cd\u5f02\u5e38\uff0c\u56e0\u6b64\u4f60\u9700\u8981\u6dfb\u52a0\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5904\u7406\u5f02\u5e38\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u8fde\u63a5\u5230\u6570\u636e\u5e93<\/p>\n<p>    conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    # \u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c<\/p>\n<p>    cursor.execute(&quot;SELECT * FROM users&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;\u6570\u636e\u5e93\u9519\u8bef: {e}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    if cursor:<\/p>\n<p>        cursor.close()<\/p>\n<p>    if conn:<\/p>\n<p>        conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5904\u7406\u5f02\u5e38\u65f6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 try&#8230;except&#8230;finally \u8bed\u53e5\u3002\u5728 finally \u5757\u4e2d\uff0c\u786e\u4fdd\u6e38\u6807\u548c\u8fde\u63a5\u5bf9\u8c61\u88ab\u6b63\u786e\u5173\u95ed\uff0c\u5373\u4f7f\u53d1\u751f\u5f02\u5e38\u4e5f\u4e0d\u4f8b\u5916\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5341\u3001\u5b8c\u6574\u793a\u4f8b<\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u5c06\u6240\u6709\u6b65\u9aa4\u6574\u5408\u5728\u4e00\u8d77\uff0c\u7ed9\u51fa\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>def connect_db(db_name):<\/p>\n<p>    try:<\/p>\n<p>        conn = sqlite3.connect(db_name)<\/p>\n<p>        cursor = conn.cursor()<\/p>\n<p>        return conn, cursor<\/p>\n<p>    except sqlite3.Error as e:<\/p>\n<p>        print(f&quot;\u6570\u636e\u5e93\u8fde\u63a5\u9519\u8bef: {e}&quot;)<\/p>\n<p>        return None, None<\/p>\n<p>def create_table(cursor):<\/p>\n<p>    try:<\/p>\n<p>        cursor.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS users<\/p>\n<p>                          (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)&#39;&#39;&#39;)<\/p>\n<p>    except sqlite3.Error as e:<\/p>\n<p>        print(f&quot;\u521b\u5efa\u8868\u9519\u8bef: {e}&quot;)<\/p>\n<p>def insert_data(cursor, conn, name, age):<\/p>\n<p>    try:<\/p>\n<p>        cursor.execute(&quot;INSERT INTO users (name, age) VALUES (?, ?)&quot;, (name, age))<\/p>\n<p>        conn.commit()<\/p>\n<p>    except sqlite3.Error as e:<\/p>\n<p>        print(f&quot;\u63d2\u5165\u6570\u636e\u9519\u8bef: {e}&quot;)<\/p>\n<p>def query_data(cursor):<\/p>\n<p>    try:<\/p>\n<p>        cursor.execute(&quot;SELECT * FROM users&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;\u67e5\u8be2\u6570\u636e\u9519\u8bef: {e}&quot;)<\/p>\n<p>def update_data(cursor, conn, name, age):<\/p>\n<p>    try:<\/p>\n<p>        cursor.execute(&quot;UPDATE users SET age = ? WHERE name = ?&quot;, (age, name))<\/p>\n<p>        conn.commit()<\/p>\n<p>    except sqlite3.Error as e:<\/p>\n<p>        print(f&quot;\u66f4\u65b0\u6570\u636e\u9519\u8bef: {e}&quot;)<\/p>\n<p>def delete_data(cursor, conn, name):<\/p>\n<p>    try:<\/p>\n<p>        cursor.execute(&quot;DELETE FROM users WHERE name = ?&quot;, (name,))<\/p>\n<p>        conn.commit()<\/p>\n<p>    except sqlite3.Error as e:<\/p>\n<p>        print(f&quot;\u5220\u9664\u6570\u636e\u9519\u8bef: {e}&quot;)<\/p>\n<p>def close_connection(cursor, conn):<\/p>\n<p>    if cursor:<\/p>\n<p>        cursor.close()<\/p>\n<p>    if conn:<\/p>\n<p>        conn.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>    conn, cursor = connect_db(&#39;example.db&#39;)<\/p>\n<p>    if conn and cursor:<\/p>\n<p>        create_table(cursor)<\/p>\n<p>        insert_data(cursor, conn, &#39;Alice&#39;, 30)<\/p>\n<p>        insert_data(cursor, conn, &#39;Bob&#39;, 25)<\/p>\n<p>        query_data(cursor)<\/p>\n<p>        update_data(cursor, conn, &#39;Alice&#39;, 35)<\/p>\n<p>        delete_data(cursor, conn, &#39;Bob&#39;)<\/p>\n<p>        query_data(cursor)<\/p>\n<p>        close_connection(cursor, conn)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u4e2a\u5b8c\u6574\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Python\u548cSQLite\u521b\u5efa\u3001\u63d2\u5165\u3001\u67e5\u8be2\u3001\u66f4\u65b0\u548c\u5220\u9664\u6570\u636e\u3002\u6211\u4eec\u8fd8\u6dfb\u52a0\u4e86\u5f02\u5e38\u5904\u7406\u4ee3\u7801\uff0c\u4ee5\u786e\u4fdd\u5728\u53d1\u751f\u9519\u8bef\u65f6\u80fd\u591f\u6b63\u786e\u5173\u95ed\u8fde\u63a5\u3002<\/strong><\/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\u7ba1\u7406\u7cfb\u7edf\u6765\u5236\u4f5cPython\u6570\u636e\u5e93\u7a0b\u5e8f\uff1f<\/strong><br \/>\u5728\u5236\u4f5cPython\u6570\u636e\u5e93\u7a0b\u5e8f\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\u81f3\u5173\u91cd\u8981\u3002\u5e38\u89c1\u7684\u9009\u9879\u5305\u62ecSQLite\u3001MySQL\u3001PostgreSQL\u548cMongoDB\u7b49\u3002SQLite\u9002\u5408\u5c0f\u578b\u5e94\u7528\uff0c\u56e0\u5176\u8f7b\u91cf\u7ea7\u548c\u6613\u4e8e\u914d\u7f6e\uff1bMySQL\u548cPostgreSQL\u9002\u5408\u9700\u8981\u590d\u6742\u67e5\u8be2\u548c\u9ad8\u5e76\u53d1\u7684\u5e94\u7528\uff1bMongoDB\u5219\u9002\u5408\u5904\u7406\u975e\u7ed3\u6784\u5316\u6570\u636e\u3002\u6839\u636e\u9879\u76ee\u7684\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\u6765\u51b3\u5b9a\u6700\u5408\u9002\u7684\u6570\u636e\u5e93\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u4e0e\u6570\u636e\u5e93\u8fdb\u884c\u8fde\u63a5\u548c\u64cd\u4f5c\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u5b9e\u73b0\u4e0e\u6570\u636e\u5e93\u7684\u8fde\u63a5\u548c\u64cd\u4f5c\u3002\u5bf9\u4e8e\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff0c\u5e38\u7528\u7684\u5e93\u6709<code>sqlite3<\/code>\u3001<code>MySQL Connector<\/code>\u548c<code>psycopg2<\/code>\uff08\u7528\u4e8ePostgreSQL\uff09\u3002\u4f7f\u7528\u8fd9\u4e9b\u5e93\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5148\u5efa\u7acb\u8fde\u63a5\uff0c\u6267\u884cSQL\u8bed\u53e5\u6765\u8fdb\u884c\u6570\u636e\u7684\u589e\u3001\u5220\u3001\u6539\u3001\u67e5\u64cd\u4f5c\u3002\u6bcf\u79cd\u5e93\u90fd\u6709\u5176\u7279\u5b9a\u7684\u8fde\u63a5\u65b9\u5f0f\u548c\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5177\u4f53\u53ef\u4ee5\u53c2\u8003\u76f8\u5173\u6587\u6863\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u6570\u636e\u5e93\u7a0b\u5e8f\u4e2d\u7684\u5f02\u5e38\u548c\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u6570\u636e\u5e93\u7f16\u7a0b\u4e2d\uff0c\u5904\u7406\u5f02\u5e38\u548c\u9519\u8bef\u662f\u786e\u4fdd\u7a0b\u5e8f\u7a33\u5b9a\u6027\u7684\u91cd\u8981\u73af\u8282\u3002\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u53ef\u4ee5\u6709\u6548\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002\u4f8b\u5982\uff0c\u5728\u6267\u884cSQL\u8bed\u53e5\u65f6\uff0c\u53ef\u4ee5\u6355\u83b7<code>OperationalError<\/code>\u6216<code>IntegrityError<\/code>\u7b49\u5f02\u5e38\uff0c\u8fdb\u800c\u91c7\u53d6\u76f8\u5e94\u63aa\u65bd\uff0c\u5982\u91cd\u8bd5\u3001\u8bb0\u5f55\u9519\u8bef\u65e5\u5fd7\u6216\u56de\u6eda\u4e8b\u52a1\u3002\u786e\u4fdd\u5728\u7a0b\u5e8f\u4e2d\u52a0\u5165\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u903b\u8f91\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5236\u4f5c\u6570\u636e\u5e93\u7a0b\u5e8f\u6d89\u53ca\u7684\u6838\u5fc3\u8981\u70b9\u5305\u62ec\u9009\u62e9\u6570\u636e\u5e93\u3001\u5b89\u88c5\u76f8\u5173\u5e93\u3001\u8fde\u63a5\u6570\u636e\u5e93\u3001\u521b\u5efa\u8868\u3001\u63d2\u5165\u6570\u636e\u548c\u67e5\u8be2\u6570\u636e\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6 [&hellip;]","protected":false},"author":3,"featured_media":1087831,"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\/1087826"}],"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=1087826"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1087826\/revisions"}],"predecessor-version":[{"id":1087832,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1087826\/revisions\/1087832"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1087831"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1087826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1087826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1087826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}