{"id":1172027,"date":"2025-01-15T16:43:22","date_gmt":"2025-01-15T08:43:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1172027.html"},"modified":"2025-01-15T16:43:24","modified_gmt":"2025-01-15T08:43:24","slug":"python%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8odbc%e5%ae%9e%e7%8e%b0%e5%89%8d%e7%ab%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1172027.html","title":{"rendered":"python\u5982\u4f55\u4f7f\u7528odbc\u5b9e\u73b0\u524d\u7aef"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26074216\/a2367ad8-afbd-4974-88db-2863e1ed12be.webp\" alt=\"python\u5982\u4f55\u4f7f\u7528odbc\u5b9e\u73b0\u524d\u7aef\" \/><\/p>\n<p><p> <strong>Python\u4f7f\u7528ODBC\u5b9e\u73b0\u524d\u7aef\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u5b89\u88c5\u76f8\u5173\u5e93\u3001\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u3001\u6267\u884cSQL\u67e5\u8be2\u3001\u5904\u7406\u67e5\u8be2\u7ed3\u679c\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>\u5b89\u88c5\u76f8\u5173\u5e93<\/strong>\u662f\u7b2c\u4e00\u6b65\uff0c\u4e14\u975e\u5e38\u91cd\u8981\u3002\u8981\u4f7f\u7528ODBC\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u9700\u8981\u5b89\u88c5<code>pyodbc<\/code>\u5e93\u3002\u5b89\u88c5\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyodbc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5c31\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165<code>pyodbc<\/code>\u5e93\uff0c\u5e76\u4f7f\u7528\u5b83\u6765\u8fde\u63a5\u6570\u636e\u5e93\u548c\u6267\u884cSQL\u67e5\u8be2\u4e86\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><p><strong>\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/p>\n<p>\u8981\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u8fde\u63a5\u5b57\u7b26\u4e32\uff0c\u5b83\u5305\u542b\u6570\u636e\u5e93\u7684\u7c7b\u578b\u3001\u670d\u52a1\u5668\u5730\u5740\u3001\u6570\u636e\u5e93\u540d\u79f0\u3001\u7528\u6237\u540d\u548c\u5bc6\u7801\u7b49\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u8fde\u63a5\u5b57\u7b26\u4e32\uff0c\u7528\u4e8e\u8fde\u63a5SQL Server\u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyodbc<\/p>\n<p>connection_string = (<\/p>\n<p>    &quot;DRIVER={ODBC Driver 17 for SQL Server};&quot;<\/p>\n<p>    &quot;SERVER=server_address;&quot;<\/p>\n<p>    &quot;DATABASE=database_name;&quot;<\/p>\n<p>    &quot;UID=username;&quot;<\/p>\n<p>    &quot;PWD=password&quot;<\/p>\n<p>)<\/p>\n<p>connection = pyodbc.connect(connection_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9700\u8981\u66ff\u6362<code>server_address<\/code>\u3001<code>database_name<\/code>\u3001<code>username<\/code>\u548c<code>password<\/code>\u4e3a\u5b9e\u9645\u7684\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\u3002\u8fde\u63a5\u6210\u529f\u540e\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u8be5\u8fde\u63a5\u5bf9\u8c61\u6765\u6267\u884cSQL\u67e5\u8be2\u548c\u5904\u7406\u67e5\u8be2\u7ed3\u679c\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u76f8\u5173\u5e93<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528ODBC\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5<code>pyodbc<\/code>\u5e93\u3002<code>pyodbc<\/code>\u662f\u4e00\u4e2a\u5f00\u6e90\u7684Python\u5e93\uff0c\u7528\u4e8e\u901a\u8fc7ODBC\u8fde\u63a5\u6570\u636e\u5e93\u3002\u5b83\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\uff0c\u5305\u62ecSQL Server\u3001MySQL\u3001PostgreSQL\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyodbc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5c31\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165<code>pyodbc<\/code>\u5e93\uff0c\u5e76\u4f7f\u7528\u5b83\u6765\u8fde\u63a5\u6570\u636e\u5e93\u548c\u6267\u884cSQL\u67e5\u8be2\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u662f\u4f7f\u7528ODBC\u8fde\u63a5\u6570\u636e\u5e93\u7684\u5173\u952e\u6b65\u9aa4\u3002\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u8fde\u63a5\u5b57\u7b26\u4e32\uff0c\u5b83\u5305\u542b\u6570\u636e\u5e93\u7684\u7c7b\u578b\u3001\u670d\u52a1\u5668\u5730\u5740\u3001\u6570\u636e\u5e93\u540d\u79f0\u3001\u7528\u6237\u540d\u548c\u5bc6\u7801\u7b49\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>1. \u8fde\u63a5SQL Server\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u8fde\u63a5\u5b57\u7b26\u4e32\uff0c\u7528\u4e8e\u8fde\u63a5SQL Server\u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyodbc<\/p>\n<p>connection_string = (<\/p>\n<p>    &quot;DRIVER={ODBC Driver 17 for SQL Server};&quot;<\/p>\n<p>    &quot;SERVER=server_address;&quot;<\/p>\n<p>    &quot;DATABASE=database_name;&quot;<\/p>\n<p>    &quot;UID=username;&quot;<\/p>\n<p>    &quot;PWD=password&quot;<\/p>\n<p>)<\/p>\n<p>connection = pyodbc.connect(connection_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9700\u8981\u66ff\u6362<code>server_address<\/code>\u3001<code>database_name<\/code>\u3001<code>username<\/code>\u548c<code>password<\/code>\u4e3a\u5b9e\u9645\u7684\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>2. \u8fde\u63a5MySQL\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u8fde\u63a5\u5b57\u7b26\u4e32\uff0c\u7528\u4e8e\u8fde\u63a5MySQL\u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">connection_string = (<\/p>\n<p>    &quot;DRIVER={MySQL ODBC 8.0 Driver};&quot;<\/p>\n<p>    &quot;SERVER=server_address;&quot;<\/p>\n<p>    &quot;DATABASE=database_name;&quot;<\/p>\n<p>    &quot;USER=username;&quot;<\/p>\n<p>    &quot;PASSWORD=password&quot;<\/p>\n<p>)<\/p>\n<p>connection = pyodbc.connect(connection_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u9700\u8981\u66ff\u6362<code>server_address<\/code>\u3001<code>database_name<\/code>\u3001<code>username<\/code>\u548c<code>password<\/code>\u4e3a\u5b9e\u9645\u7684\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6267\u884cSQL\u67e5\u8be2<\/h3>\n<\/p>\n<p><p>\u8fde\u63a5\u6570\u636e\u5e93\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fde\u63a5\u5bf9\u8c61\u6765\u6267\u884cSQL\u67e5\u8be2\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u7528\u4e8e\u6267\u884cSQL\u67e5\u8be2\u5e76\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor = connection.cursor()<\/p>\n<p>query = &quot;SELECT * FROM table_name&quot;<\/p>\n<p>cursor.execute(query)<\/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><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9700\u8981\u66ff\u6362<code>table_name<\/code>\u4e3a\u5b9e\u9645\u7684\u8868\u540d\u3002<code>fetchall()<\/code>\u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u6240\u6709\u67e5\u8be2\u7ed3\u679c\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728<code>rows<\/code>\u53d8\u91cf\u4e2d\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u904d\u5386<code>rows<\/code>\u53d8\u91cf\u6765\u5904\u7406\u67e5\u8be2\u7ed3\u679c\u3002<\/p>\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\u662f\u4f7f\u7528ODBC\u8fde\u63a5\u6570\u636e\u5e93\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u5904\u7406\u67e5\u8be2\u7ed3\u679c\uff0c\u5982\u5c06\u67e5\u8be2\u7ed3\u679c\u8f6c\u6362\u4e3aPandas DataFrame\u3001\u5c06\u67e5\u8be2\u7ed3\u679c\u4fdd\u5b58\u5230\u6587\u4ef6\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u5c06\u67e5\u8be2\u7ed3\u679c\u8f6c\u6362\u4e3aPandas DataFrame<\/h4>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c06\u67e5\u8be2\u7ed3\u679c\u8f6c\u6362\u4e3aPandas DataFrame\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame.from_records(rows, columns=[desc[0] for desc in cursor.description])<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>from_records<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u67e5\u8be2\u7ed3\u679c\u8f6c\u6362\u4e3aPandas DataFrame\u3002<code>columns<\/code>\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u5217\u540d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>cursor.description<\/code>\u5c5e\u6027\u83b7\u53d6\u5217\u540d\u3002<\/p>\n<\/p>\n<p><h4>2. \u5c06\u67e5\u8be2\u7ed3\u679c\u4fdd\u5b58\u5230\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u67e5\u8be2\u7ed3\u679c\u4fdd\u5b58\u5230\u6587\u4ef6\uff0c\u5982CSV\u6587\u4ef6\uff0c\u4ee5\u4fbf\u540e\u7eed\u5904\u7406\u548c\u5206\u6790\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c06\u67e5\u8be2\u7ed3\u679c\u4fdd\u5b58\u5230CSV\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df.to_csv(&quot;query_result.csv&quot;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>to_csv<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06Pandas DataFrame\u4fdd\u5b58\u5230CSV\u6587\u4ef6\u3002<code>index=False<\/code>\u53c2\u6570\u7528\u4e8e\u4e0d\u4fdd\u5b58\u884c\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528ODBC\u8fde\u63a5\u6570\u636e\u5e93\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\uff0c\u5982\u8fde\u63a5\u5931\u8d25\u3001SQL\u8bed\u6cd5\u9519\u8bef\u7b49\u3002\u9700\u8981\u8fdb\u884c\u9519\u8bef\u5904\u7406\uff0c\u4ee5\u4fbf\u5728\u9047\u5230\u9519\u8bef\u65f6\u80fd\u591f\u53ca\u65f6\u53d1\u73b0\u548c\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u6f14\u793a\u5982\u4f55\u8fdb\u884c\u9519\u8bef\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    connection = pyodbc.connect(connection_string)<\/p>\n<p>    cursor = connection.cursor()<\/p>\n<p>    cursor.execute(query)<\/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 e:<\/p>\n<p>    print(&quot;Error:&quot;, e)<\/p>\n<p>finally:<\/p>\n<p>    connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>try-except-finally<\/code>\u7ed3\u6784\u8fdb\u884c\u9519\u8bef\u5904\u7406\u3002\u5728<code>try<\/code>\u5757\u4e2d\u6267\u884c\u6570\u636e\u5e93\u8fde\u63a5\u548cSQL\u67e5\u8be2\u64cd\u4f5c\uff0c\u5982\u679c\u9047\u5230\u9519\u8bef\uff0c\u5c06\u6355\u83b7<code>pyodbc.Error<\/code>\u5f02\u5e38\u5e76\u6253\u5370\u9519\u8bef\u4fe1\u606f\u3002\u5728<code>finally<\/code>\u5757\u4e2d\uff0c\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528Flask\u521b\u5efa\u524d\u7aef\u754c\u9762<\/h3>\n<\/p>\n<p><p>Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Web\u6846\u67b6\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efaWeb\u5e94\u7528\u3002\u53ef\u4ee5\u4f7f\u7528Flask\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u524d\u7aef\u754c\u9762\uff0c\u901a\u8fc7ODBC\u8fde\u63a5\u6570\u636e\u5e93\u5e76\u5c55\u793a\u67e5\u8be2\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Flask<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Flask\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Flask<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efaFlask\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2aFlask\u5e94\u7528\uff0c\u5e76\u7f16\u5199\u4ee3\u7801\u8fde\u63a5\u6570\u636e\u5e93\u548c\u5c55\u793a\u67e5\u8be2\u7ed3\u679c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, render_template<\/p>\n<p>import pyodbc<\/p>\n<p>import pandas as pd<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def index():<\/p>\n<p>    connection_string = (<\/p>\n<p>        &quot;DRIVER={ODBC Driver 17 for SQL Server};&quot;<\/p>\n<p>        &quot;SERVER=server_address;&quot;<\/p>\n<p>        &quot;DATABASE=database_name;&quot;<\/p>\n<p>        &quot;UID=username;&quot;<\/p>\n<p>        &quot;PWD=password&quot;<\/p>\n<p>    )<\/p>\n<p>    connection = pyodbc.connect(connection_string)<\/p>\n<p>    cursor = connection.cursor()<\/p>\n<p>    query = &quot;SELECT * FROM table_name&quot;<\/p>\n<p>    cursor.execute(query)<\/p>\n<p>    rows = cursor.fetchall()<\/p>\n<p>    df = pd.DataFrame.from_records(rows, columns=[desc[0] for desc in cursor.description])<\/p>\n<p>    connection.close()<\/p>\n<p>    return render_template(&#39;index.html&#39;, tables=[df.to_html(classes=&#39;data&#39;)], titles=df.columns.values)<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>index<\/code>\u51fd\u6570\u7528\u4e8e\u5904\u7406\u6839\u8def\u5f84\u7684\u8bf7\u6c42\uff0c\u8fde\u63a5\u6570\u636e\u5e93\u5e76\u6267\u884cSQL\u67e5\u8be2\uff0c\u5c06\u67e5\u8be2\u7ed3\u679c\u8f6c\u6362\u4e3aPandas DataFrame\uff0c\u5e76\u4f7f\u7528<code>render_template<\/code>\u51fd\u6570\u6e32\u67d3HTML\u6a21\u677f\u3002<\/p>\n<\/p>\n<p><h4>3. \u521b\u5efaHTML\u6a21\u677f<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2aHTML\u6a21\u677f\u6587\u4ef6<code>templates\/index.html<\/code>\uff0c\u7528\u4e8e\u5c55\u793a\u67e5\u8be2\u7ed3\u679c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8bHTML\u6a21\u677f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;Query Result&lt;\/title&gt;<\/p>\n<p>    &lt;link rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; href=&quot;{{ url_for(&#39;static&#39;, filename=&#39;style.css&#39;) }}&quot;&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Query Result&lt;\/h1&gt;<\/p>\n<p>    {% for table in tables %}<\/p>\n<p>        {{ table|<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a> }}<\/p>\n<p>    {% endfor %}<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0HTML\u6a21\u677f\u4e2d\uff0c\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386<code>tables<\/code>\u53d8\u91cf\uff0c\u5e76\u4f7f\u7528<code>safe<\/code>\u8fc7\u6ee4\u5668\u5c06HTML\u5185\u5bb9\u6e32\u67d3\u5230\u9875\u9762\u3002<\/p>\n<\/p>\n<p><h4>4. \u8fd0\u884cFlask\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u5b8c\u6210\u4e0a\u8ff0\u6b65\u9aa4\u540e\uff0c\u53ef\u4ee5\u8fd0\u884cFlask\u5e94\u7528\uff0c\u5e76\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee<code>http:\/\/localhost:5000<\/code>\u67e5\u770b\u67e5\u8be2\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python app.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee<code>http:\/\/localhost:5000<\/code>\uff0c\u53ef\u4ee5\u770b\u5230\u67e5\u8be2\u7ed3\u679c\u5c55\u793a\u5728\u7f51\u9875\u4e0a\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u6570\u636e\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5c55\u793a\u67e5\u8be2\u7ed3\u679c\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u5982Matplotlib\u3001Seaborn\u7b49\uff0c\u5c06\u67e5\u8be2\u7ed3\u679c\u53ef\u89c6\u5316\u5c55\u793a\u5728\u524d\u7aef\u754c\u9762\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u6570\u636e\u53ef\u89c6\u5316\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Matplotlib\u548cSeaborn\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib seaborn<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u6570\u636e\u53ef\u89c6\u5316\u56fe\u8868<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u53ef\u89c6\u5316\u56fe\u8868\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3a\u56fe\u7247\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u4f7f\u7528Matplotlib\u521b\u5efa\u4e00\u4e2a\u67f1\u72b6\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import seaborn as sns<\/p>\n<p>def create_bar_chart(df):<\/p>\n<p>    plt.figure(figsize=(10, 6))<\/p>\n<p>    sns.barplot(x=&#39;column1&#39;, y=&#39;column2&#39;, data=df)<\/p>\n<p>    plt.title(&#39;Bar Chart&#39;)<\/p>\n<p>    plt.xlabel(&#39;Column 1&#39;)<\/p>\n<p>    plt.ylabel(&#39;Column 2&#39;)<\/p>\n<p>    plt.savefig(&#39;static\/bar_chart.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>create_bar_chart<\/code>\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u67f1\u72b6\u56fe\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3a\u56fe\u7247\u6587\u4ef6<code>static\/bar_chart.png<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3. \u5728Flask\u5e94\u7528\u4e2d\u5c55\u793a\u56fe\u8868<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5728Flask\u5e94\u7528\u4e2d\u8c03\u7528<code>create_bar_chart<\/code>\u51fd\u6570\uff0c\u5e76\u5c06\u56fe\u7247\u5c55\u793a\u5728\u524d\u7aef\u754c\u9762\u3002\u4ee5\u4e0b\u662f\u66f4\u65b0\u540e\u7684Flask\u5e94\u7528\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, render_template<\/p>\n<p>import pyodbc<\/p>\n<p>import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>import seaborn as sns<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>def create_bar_chart(df):<\/p>\n<p>    plt.figure(figsize=(10, 6))<\/p>\n<p>    sns.barplot(x=&#39;column1&#39;, y=&#39;column2&#39;, data=df)<\/p>\n<p>    plt.title(&#39;Bar Chart&#39;)<\/p>\n<p>    plt.xlabel(&#39;Column 1&#39;)<\/p>\n<p>    plt.ylabel(&#39;Column 2&#39;)<\/p>\n<p>    plt.savefig(&#39;static\/bar_chart.png&#39;)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def index():<\/p>\n<p>    connection_string = (<\/p>\n<p>        &quot;DRIVER={ODBC Driver 17 for SQL Server};&quot;<\/p>\n<p>        &quot;SERVER=server_address;&quot;<\/p>\n<p>        &quot;DATABASE=database_name;&quot;<\/p>\n<p>        &quot;UID=username;&quot;<\/p>\n<p>        &quot;PWD=password&quot;<\/p>\n<p>    )<\/p>\n<p>    connection = pyodbc.connect(connection_string)<\/p>\n<p>    cursor = connection.cursor()<\/p>\n<p>    query = &quot;SELECT * FROM table_name&quot;<\/p>\n<p>    cursor.execute(query)<\/p>\n<p>    rows = cursor.fetchall()<\/p>\n<p>    df = pd.DataFrame.from_records(rows, columns=[desc[0] for desc in cursor.description])<\/p>\n<p>    create_bar_chart(df)<\/p>\n<p>    connection.close()<\/p>\n<p>    return render_template(&#39;index.html&#39;, tables=[df.to_html(classes=&#39;data&#39;)], titles=df.columns.values)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u8c03\u7528<code>create_bar_chart<\/code>\u51fd\u6570\u521b\u5efa\u67f1\u72b6\u56fe\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3a\u56fe\u7247\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>4. \u66f4\u65b0HTML\u6a21\u677f<\/h4>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u66f4\u65b0HTML\u6a21\u677f\u6587\u4ef6<code>templates\/index.html<\/code>\uff0c\u5728\u9875\u9762\u4e0a\u5c55\u793a\u67f1\u72b6\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;Query Result&lt;\/title&gt;<\/p>\n<p>    &lt;link rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; href=&quot;{{ url_for(&#39;static&#39;, filename=&#39;style.css&#39;) }}&quot;&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Query Result&lt;\/h1&gt;<\/p>\n<p>    {% for table in tables %}<\/p>\n<p>        {{ table|safe }}<\/p>\n<p>    {% endfor %}<\/p>\n<p>    &lt;h2&gt;Bar Chart&lt;\/h2&gt;<\/p>\n<p>    &lt;img src=&quot;{{ url_for(&#39;static&#39;, filename=&#39;bar_chart.png&#39;) }}&quot; alt=&quot;Bar Chart&quot;&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0HTML\u6a21\u677f\u4e2d\uff0c\u4f7f\u7528<code>img<\/code>\u6807\u7b7e\u5c55\u793a\u67f1\u72b6\u56fe\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u4f7f\u7528Python\u7684ODBC\u63a5\u53e3\u8fde\u63a5\u6570\u636e\u5e93\u3001\u6267\u884cSQL\u67e5\u8be2\u5e76\u5904\u7406\u67e5\u8be2\u7ed3\u679c\u3002\u540c\u65f6\uff0c\u6211\u4eec\u8fd8\u5b66\u4e60\u4e86\u5982\u4f55\u4f7f\u7528Flask\u521b\u5efa\u524d\u7aef\u754c\u9762\u5c55\u793a\u67e5\u8be2\u7ed3\u679c\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u6570\u636e\u53ef\u89c6\u5316\u5e93\u521b\u5efa\u56fe\u8868\u5e76\u5728\u524d\u7aef\u754c\u9762\u5c55\u793a\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u7684ODBC\u63a5\u53e3\uff0c\u5b9e\u73b0\u524d\u7aef\u754c\u9762\u7684\u6570\u636e\u5c55\u793a\u548c\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u914d\u7f6eODBC\u8fde\u63a5\uff1f<\/strong><br \/>\u4e3a\u4e86\u5728Python\u4e2d\u4f7f\u7528ODBC\u8fde\u63a5\uff0c\u60a8\u9700\u8981\u9996\u5148\u5b89\u88c5pyodbc\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install pyodbc<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u9700\u8981\u5b9a\u4e49\u8fde\u63a5\u5b57\u7b26\u4e32\uff0c\u901a\u5e38\u5305\u542b\u6570\u636e\u6e90\u540d\u79f0(DSN)\u3001\u7528\u6237 ID \u548c\u5bc6\u7801\u7b49\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pyodbc  \nconnection = pyodbc.connect(&#39;DSN=your_dsn;UID=user;PWD=password&#39;)  \n<\/code><\/pre>\n<p><strong>\u4f7f\u7528ODBC\u65f6\uff0c\u5982\u4f55\u5904\u7406\u6570\u636e\u5e93\u4e2d\u7684\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u4f7f\u7528ODBC\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0c\u5f02\u5e38\u5904\u7406\u975e\u5e38\u91cd\u8981\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528try-except\u5757\u6765\u6355\u83b7\u548c\u5904\u7406\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">try:  \n    cursor = connection.cursor()  \n    cursor.execute(&quot;SELECT * FROM your_table&quot;)  \nexcept pyodbc.DatabaseError as e:  \n    print(&quot;\u6570\u636e\u5e93\u9519\u8bef:&quot;, e)  \nfinally:  \n    cursor.close()  \n    connection.close()  \n<\/code><\/pre>\n<p>\u8fd9\u6837\u7684\u5904\u7406\u65b9\u5f0f\u53ef\u4ee5\u786e\u4fdd\u5728\u53d1\u751f\u9519\u8bef\u65f6\uff0c\u7a0b\u5e8f\u4e0d\u4f1a\u5d29\u6e83\uff0c\u5e76\u80fd\u591f\u8fdb\u884c\u9002\u5f53\u7684\u9519\u8bef\u62a5\u544a\u3002<\/p>\n<p><strong>ODBC\u8fde\u63a5\u7684\u6027\u80fd\u5982\u4f55\u4f18\u5316\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u9ad8ODBC\u8fde\u63a5\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u8003\u8651\u51e0\u4e2a\u65b9\u9762\u3002\u9996\u5148\uff0c\u4f7f\u7528\u8fde\u63a5\u6c60\u6765\u91cd\u7528\u73b0\u6709\u8fde\u63a5\uff0c\u800c\u4e0d\u662f\u6bcf\u6b21\u8bf7\u6c42\u65f6\u90fd\u65b0\u5efa\u8fde\u63a5\u3002\u5176\u6b21\uff0c\u786e\u4fddSQL\u67e5\u8be2\u7684\u4f18\u5316\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6570\u636e\u4f20\u8f93\u3002\u6700\u540e\uff0c\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4e8b\u52a1\u6765\u6279\u91cf\u5904\u7406\u6570\u636e\u64cd\u4f5c\uff0c\u51cf\u5c11\u4e0e\u6570\u636e\u5e93\u7684\u4ea4\u4e92\u6b21\u6570\uff0c\u4ece\u800c\u63d0\u5347\u6574\u4f53\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4f7f\u7528ODBC\u5b9e\u73b0\u524d\u7aef\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u5b89\u88c5\u76f8\u5173\u5e93\u3001\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u3001\u6267\u884cSQL\u67e5\u8be2\u3001\u5904\u7406\u67e5\u8be2\u7ed3\u679c\u3002 \u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1172031,"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\/1172027"}],"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=1172027"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172027\/revisions"}],"predecessor-version":[{"id":1172032,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172027\/revisions\/1172032"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1172031"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1172027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1172027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1172027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}