{"id":1109931,"date":"2025-01-08T17:14:57","date_gmt":"2025-01-08T09:14:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1109931.html"},"modified":"2025-01-08T17:15:01","modified_gmt":"2025-01-08T09:15:01","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96sql%e7%9a%84%e6%89%a7%e8%a1%8c%e7%8a%b6%e6%80%81%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1109931.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6sql\u7684\u6267\u884c\u72b6\u6001\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073011\/a0afea57-641b-4d2f-9360-dbc37c36a06d.webp\" alt=\"python\u5982\u4f55\u83b7\u53d6sql\u7684\u6267\u884c\u72b6\u6001\u7801\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u6570\u636e\u5e93\u9a71\u52a8\u5e93\u83b7\u53d6SQL\u6267\u884c\u7684\u72b6\u6001\u7801\uff0c\u4f8b\u5982\u4f7f\u7528<code>pyodbc<\/code>\u3001<code>pymysql<\/code>\u3001<code>psycopg2<\/code>\u7b49\u3002\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u68c0\u67e5\u5f02\u5e38\u3001\u4f7f\u7528\u6e38\u6807\uff08cursor\uff09\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u8be6\u7ec6\u89e3\u91ca\u8fd9\u4e00\u70b9\uff0c\u672c\u6587\u5c06\u4ecb\u7ecd\u4e0d\u540c\u6570\u636e\u5e93\u9a71\u52a8\u5e93\u5982\u4f55\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\uff0c\u5e76\u83b7\u53d6SQL\u6267\u884c\u7684\u72b6\u6001\u7801\u3002\u6211\u4eec\u5c06\u4ece\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u63a2\u8ba8\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>pyodbc<\/code>\u83b7\u53d6SQL\u6267\u884c\u72b6\u6001\u7801<br \/>\u4e8c\u3001\u4f7f\u7528<code>pymysql<\/code>\u83b7\u53d6SQL\u6267\u884c\u72b6\u6001\u7801<br \/>\u4e09\u3001\u4f7f\u7528<code>psycopg2<\/code>\u83b7\u53d6SQL\u6267\u884c\u72b6\u6001\u7801<br \/>\u56db\u3001\u5f02\u5e38\u5904\u7406\u548c\u72b6\u6001\u7801\u89e3\u6790<br \/>\u4e94\u3001\u5b9e\u8df5\u6848\u4f8b\u548c\u4ee3\u7801\u793a\u4f8b<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528<code>pyodbc<\/code>\u83b7\u53d6SQL\u6267\u884c\u72b6\u6001\u7801<\/h2>\n<\/p>\n<p><p><code>pyodbc<\/code>\u662f\u4e00\u4e2aPython\u5e93\uff0c\u5b83\u5141\u8bb8\u4f60\u901a\u8fc7ODBC\u8fde\u63a5\u6765\u8bbf\u95ee\u6570\u636e\u5e93\u3002\u5728\u4f7f\u7528<code>pyodbc<\/code>\u65f6\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u6765\u83b7\u53d6SQL\u6267\u884c\u7684\u72b6\u6001\u7801\u3002<\/p>\n<\/p>\n<p><h3>1.1 \u5b89\u88c5<code>pyodbc<\/code><\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5<code>pyodbc<\/code>\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyodbc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.2 \u8fde\u63a5\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>pyodbc<\/code>\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff0c\u5e76\u6267\u884cSQL\u67e5\u8be2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyodbc<\/p>\n<h2><strong>\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>connection = pyodbc.connect(&#39;DRIVER={SQL Server};SERVER=server_name;DATABASE=db_name;UID=user;PWD=password&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = connection.cursor()<\/p>\n<p>try:<\/p>\n<p>    # \u6267\u884cSQL\u67e5\u8be2<\/p>\n<p>    cursor.execute(&quot;SELECT * FROM table_name&quot;)<\/p>\n<p>    rows = cursor.fetchall()<\/p>\n<p>    # \u5904\u7406\u67e5\u8be2\u7ed3\u679c<\/p>\n<p>    for row in rows:<\/p>\n<p>        print(row)<\/p>\n<p>except pyodbc.Error as ex:<\/p>\n<p>    # \u83b7\u53d6\u9519\u8bef\u4fe1\u606f\u548c\u72b6\u6001\u7801<\/p>\n<p>    sqlstate = ex.args[0]<\/p>\n<p>    print(f&quot;SQLSTATE: {sqlstate}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    # \u5173\u95ed\u8fde\u63a5<\/p>\n<p>    cursor.close()<\/p>\n<p>    connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5f53SQL\u67e5\u8be2\u6267\u884c\u5931\u8d25\u65f6\uff0c\u4f1a\u6355\u83b7<code>pyodbc.Error<\/code>\u5f02\u5e38\uff0c\u5e76\u83b7\u53d6SQL\u72b6\u6001\u7801\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528<code>pymysql<\/code>\u83b7\u53d6SQL\u6267\u884c\u72b6\u6001\u7801<\/h2>\n<\/p>\n<p><p><code>pymysql<\/code>\u662f\u4e00\u4e2a\u7eafPython\u5b9e\u73b0\u7684MySQL\u6570\u636e\u5e93\u63a5\u53e3\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u6765\u83b7\u53d6SQL\u6267\u884c\u7684\u72b6\u6001\u7801\u3002<\/p>\n<\/p>\n<p><h3>2.1 \u5b89\u88c5<code>pymysql<\/code><\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5<code>pymysql<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pymysql<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.2 \u8fde\u63a5\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>pymysql<\/code>\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff0c\u5e76\u6267\u884cSQL\u67e5\u8be2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<h2><strong>\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>connection = pymysql.connect(host=&#39;localhost&#39;, user=&#39;user&#39;, password=&#39;password&#39;, database=&#39;db_name&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = connection.cursor()<\/p>\n<p>try:<\/p>\n<p>    # \u6267\u884cSQL\u67e5\u8be2<\/p>\n<p>    cursor.execute(&quot;SELECT * FROM table_name&quot;)<\/p>\n<p>    rows = cursor.fetchall()<\/p>\n<p>    # \u5904\u7406\u67e5\u8be2\u7ed3\u679c<\/p>\n<p>    for row in rows:<\/p>\n<p>        print(row)<\/p>\n<p>except pymysql.MySQLError as ex:<\/p>\n<p>    # \u83b7\u53d6\u9519\u8bef\u4fe1\u606f\u548c\u72b6\u6001\u7801<\/p>\n<p>    error_code = ex.args[0]<\/p>\n<p>    print(f&quot;Error Code: {error_code}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    # \u5173\u95ed\u8fde\u63a5<\/p>\n<p>    cursor.close()<\/p>\n<p>    connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5f53SQL\u67e5\u8be2\u6267\u884c\u5931\u8d25\u65f6\uff0c\u4f1a\u6355\u83b7<code>pymysql.MySQLError<\/code>\u5f02\u5e38\uff0c\u5e76\u83b7\u53d6\u9519\u8bef\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528<code>psycopg2<\/code>\u83b7\u53d6SQL\u6267\u884c\u72b6\u6001\u7801<\/h2>\n<\/p>\n<p><p><code>psycopg2<\/code>\u662f\u4e00\u4e2aPython\u5e93\uff0c\u7528\u4e8e\u4e0ePostgreSQL\u6570\u636e\u5e93\u4ea4\u4e92\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u6765\u83b7\u53d6SQL\u6267\u884c\u7684\u72b6\u6001\u7801\u3002<\/p>\n<\/p>\n<p><h3>3.1 \u5b89\u88c5<code>psycopg2<\/code><\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5<code>psycopg2<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install psycopg2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2 \u8fde\u63a5\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>psycopg2<\/code>\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff0c\u5e76\u6267\u884cSQL\u67e5\u8be2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import psycopg2<\/p>\n<h2><strong>\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>connection = psycopg2.connect(host=&#39;localhost&#39;, user=&#39;user&#39;, password=&#39;password&#39;, dbname=&#39;db_name&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = connection.cursor()<\/p>\n<p>try:<\/p>\n<p>    # \u6267\u884cSQL\u67e5\u8be2<\/p>\n<p>    cursor.execute(&quot;SELECT * FROM table_name&quot;)<\/p>\n<p>    rows = cursor.fetchall()<\/p>\n<p>    # \u5904\u7406\u67e5\u8be2\u7ed3\u679c<\/p>\n<p>    for row in rows:<\/p>\n<p>        print(row)<\/p>\n<p>except psycopg2.DatabaseError as ex:<\/p>\n<p>    # \u83b7\u53d6\u9519\u8bef\u4fe1\u606f\u548c\u72b6\u6001\u7801<\/p>\n<p>    sqlstate = ex.pgcode<\/p>\n<p>    print(f&quot;SQLSTATE: {sqlstate}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    # \u5173\u95ed\u8fde\u63a5<\/p>\n<p>    cursor.close()<\/p>\n<p>    connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5f53SQL\u67e5\u8be2\u6267\u884c\u5931\u8d25\u65f6\uff0c\u4f1a\u6355\u83b7<code>psycopg2.DatabaseError<\/code>\u5f02\u5e38\uff0c\u5e76\u83b7\u53d6SQL\u72b6\u6001\u7801\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u5f02\u5e38\u5904\u7406\u548c\u72b6\u6001\u7801\u89e3\u6790<\/h2>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u6355\u83b7\u4e0d\u540c\u5e93\u7684\u5f02\u5e38\u6765\u83b7\u53d6SQL\u6267\u884c\u7684\u72b6\u6001\u7801\u3002\u8fd9\u4e9b\u72b6\u6001\u7801\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3SQL\u67e5\u8be2\u5931\u8d25\u7684\u5177\u4f53\u539f\u56e0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684SQL\u72b6\u6001\u7801\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>08003<\/code>\uff1a\u8fde\u63a5\u4e0d\u5b58\u5728<\/li>\n<li><code>08006<\/code>\uff1a\u8fde\u63a5\u5931\u8d25<\/li>\n<li><code>40001<\/code>\uff1a\u4e8b\u52a1\u56de\u6eda<\/li>\n<li><code>42P01<\/code>\uff1a\u672a\u5b9a\u4e49\u7684\u8868<\/li>\n<li><code>23505<\/code>\uff1a\u552f\u4e00\u7ea6\u675f\u8fdd\u89c4<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u89e3\u6790\u8fd9\u4e9b\u72b6\u6001\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406SQL\u67e5\u8be2\u5931\u8d25\u7684\u60c5\u51b5\uff0c\u5e76\u91c7\u53d6\u76f8\u5e94\u7684\u63aa\u65bd\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5b9e\u8df5\u6848\u4f8b\u548c\u4ee3\u7801\u793a\u4f8b<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u83b7\u53d6SQL\u6267\u884c\u7684\u72b6\u6001\u7801\uff0c\u6211\u4eec\u5c06\u5c55\u793a\u4e00\u4e2a\u5b8c\u6574\u7684\u5b9e\u8df5\u6848\u4f8b\uff0c\u4f7f\u7528<code>pyodbc<\/code>\u3001<code>pymysql<\/code>\u548c<code>psycopg2<\/code>\u5206\u522b\u8fde\u63a5\u5230SQL Server\u3001MySQL\u548cPostgreSQL\u6570\u636e\u5e93\uff0c\u5e76\u6267\u884cSQL\u67e5\u8be2\u3002<\/p>\n<\/p>\n<p><h3>5.1 \u4f7f\u7528<code>pyodbc<\/code>\u8fde\u63a5SQL Server<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pyodbc<\/p>\n<p>def execute_sql_with_pyodbc():<\/p>\n<p>    connection = pyodbc.connect(&#39;DRIVER={SQL Server};SERVER=server_name;DATABASE=db_name;UID=user;PWD=password&#39;)<\/p>\n<p>    cursor = connection.cursor()<\/p>\n<p>    try:<\/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 pyodbc.Error as ex:<\/p>\n<p>        sqlstate = ex.args[0]<\/p>\n<p>        print(f&quot;SQLSTATE: {sqlstate}&quot;)<\/p>\n<p>    finally:<\/p>\n<p>        cursor.close()<\/p>\n<p>        connection.close()<\/p>\n<p>execute_sql_with_pyodbc()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.2 \u4f7f\u7528<code>pymysql<\/code>\u8fde\u63a5MySQL<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>def execute_sql_with_pymysql():<\/p>\n<p>    connection = pymysql.connect(host=&#39;localhost&#39;, user=&#39;user&#39;, password=&#39;password&#39;, database=&#39;db_name&#39;)<\/p>\n<p>    cursor = connection.cursor()<\/p>\n<p>    try:<\/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 pymysql.MySQLError as ex:<\/p>\n<p>        error_code = ex.args[0]<\/p>\n<p>        print(f&quot;Error Code: {error_code}&quot;)<\/p>\n<p>    finally:<\/p>\n<p>        cursor.close()<\/p>\n<p>        connection.close()<\/p>\n<p>execute_sql_with_pymysql()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.3 \u4f7f\u7528<code>psycopg2<\/code>\u8fde\u63a5PostgreSQL<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import psycopg2<\/p>\n<p>def execute_sql_with_psycopg2():<\/p>\n<p>    connection = psycopg2.connect(host=&#39;localhost&#39;, user=&#39;user&#39;, password=&#39;password&#39;, dbname=&#39;db_name&#39;)<\/p>\n<p>    cursor = connection.cursor()<\/p>\n<p>    try:<\/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 psycopg2.DatabaseError as ex:<\/p>\n<p>        sqlstate = ex.pgcode<\/p>\n<p>        print(f&quot;SQLSTATE: {sqlstate}&quot;)<\/p>\n<p>    finally:<\/p>\n<p>        cursor.close()<\/p>\n<p>        connection.close()<\/p>\n<p>execute_sql_with_psycopg2()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u5b9e\u8df5\u6848\u4f8b\u548c\u4ee3\u7801\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u6e05\u6670\u5730\u770b\u5230\u5982\u4f55\u4f7f\u7528\u4e0d\u540c\u7684Python\u5e93\u8fde\u63a5\u5230\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u5e93\uff0c\u5e76\u83b7\u53d6SQL\u6267\u884c\u7684\u72b6\u6001\u7801\u3002\u8fd9\u4e9b\u72b6\u6001\u7801\u5bf9\u4e8e\u8c03\u8bd5\u548c\u9519\u8bef\u5904\u7406\u975e\u5e38\u91cd\u8981\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u5b9a\u4f4d\u548c\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u8d77\u6765\uff0c\u901a\u8fc7\u4f7f\u7528<code>pyodbc<\/code>\u3001<code>pymysql<\/code>\u548c<code>psycopg2<\/code>\u7b49\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u4e0e\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\uff0c\u5e76\u83b7\u53d6SQL\u6267\u884c\u7684\u72b6\u6001\u7801\u3002\u4e86\u89e3\u8fd9\u4e9b\u72b6\u6001\u7801\u5bf9\u4e8e\u6570\u636e\u5e93\u64cd\u4f5c\u548c\u9519\u8bef\u5904\u7406\u81f3\u5173\u91cd\u8981\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6267\u884cSQL\u67e5\u8be2\u5e76\u83b7\u53d6\u72b6\u6001\u7801\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u5e93\u9002\u914d\u5668\uff08\u5982<code>sqlite3<\/code>\u3001<code>psycopg2<\/code>\u3001<code>pyodbc<\/code>\u7b49\uff09\u6765\u6267\u884cSQL\u67e5\u8be2\u5e76\u83b7\u53d6\u72b6\u6001\u7801\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u6267\u884cSQL\u8bed\u53e5\u540e\uff0c\u9002\u914d\u5668\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7ed3\u679c\u5bf9\u8c61\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u8be5\u5bf9\u8c61\u7684\u5c5e\u6027\u6216\u4f7f\u7528\u7279\u5b9a\u7684\u65b9\u6cd5\u6765\u83b7\u53d6\u72b6\u6001\u7801\u3002\u4f8b\u5982\uff0c\u5728\u4f7f\u7528<code>sqlite3<\/code>\u65f6\uff0c\u6267\u884c<code>cursor.execute(sql)<\/code>\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>cursor.rowcount<\/code>\u6765\u83b7\u53d6\u53d7\u5f71\u54cd\u7684\u884c\u6570\uff0c\u7ed3\u5408\u5f02\u5e38\u5904\u7406\u53ef\u4ee5\u5224\u65ad\u6267\u884c\u72b6\u6001\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406SQL\u6267\u884c\u9519\u8bef\uff1f<\/strong><br \/>\u6267\u884cSQL\u8bed\u53e5\u65f6\uff0c\u53ef\u80fd\u4f1a\u53d1\u751f\u5404\u79cd\u9519\u8bef\uff0c\u4f8b\u5982\u8bed\u6cd5\u9519\u8bef\u6216\u8fde\u63a5\u95ee\u9898\u3002\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u53ef\u4ee5\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\u3002\u901a\u8fc7\u6355\u83b7\u7279\u5b9a\u7684\u5f02\u5e38\uff08\u5982<code>sqlite3.Error<\/code>\u6216<code>psycopg2.DatabaseError<\/code>\uff09\uff0c\u60a8\u53ef\u4ee5\u83b7\u53d6\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u5e76\u6839\u636e\u9700\u8981\u91c7\u53d6\u76f8\u5e94\u7684\u63aa\u65bd\uff0c\u6bd4\u5982\u91cd\u65b0\u5c1d\u8bd5\u6267\u884c\u6216\u8bb0\u5f55\u65e5\u5fd7\u3002<\/p>\n<p><strong>\u4e0d\u540c\u7684\u6570\u636e\u5e93\u9002\u914d\u5668\u83b7\u53d6SQL\u6267\u884c\u72b6\u6001\u7801\u7684\u65b9\u5f0f\u6709\u4f55\u4e0d\u540c\uff1f<\/strong><br \/>\u6bcf\u79cd\u6570\u636e\u5e93\u9002\u914d\u5668\u7684\u5b9e\u73b0\u7ec6\u8282\u53ef\u80fd\u4e0d\u540c\uff0c\u4f46\u5927\u591a\u6570\u90fd\u4f1a\u63d0\u4f9b\u83b7\u53d6\u6267\u884c\u72b6\u6001\u7684\u529f\u80fd\u3002\u4ee5<code>psycopg2<\/code>\u4e3a\u4f8b\uff0c\u6267\u884c\u5b8cSQL\u8bed\u53e5\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>cursor.statusmessage<\/code>\u83b7\u53d6\u6267\u884c\u7ed3\u679c\u7684\u72b6\u6001\u4fe1\u606f\uff1b\u800c\u5728<code>pyodbc<\/code>\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cursor.rowcount<\/code>\u83b7\u53d6\u53d7\u5f71\u54cd\u7684\u884c\u6570\u3002\u5efa\u8bae\u67e5\u9605\u5404\u9002\u914d\u5668\u7684\u5b98\u65b9\u6587\u6863\uff0c\u4ee5\u4e86\u89e3\u5982\u4f55\u83b7\u53d6\u72b6\u6001\u7801\u548c\u6267\u884c\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u6570\u636e\u5e93\u9a71\u52a8\u5e93\u83b7\u53d6SQL\u6267\u884c\u7684\u72b6\u6001\u7801\uff0c\u4f8b\u5982\u4f7f\u7528pyodbc\u3001pymysql\u3001psycopg2 [&hellip;]","protected":false},"author":3,"featured_media":1109948,"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\/1109931"}],"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=1109931"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109931\/revisions"}],"predecessor-version":[{"id":1109951,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109931\/revisions\/1109951"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1109948"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1109931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1109931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1109931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}