{"id":977303,"date":"2024-12-27T06:28:27","date_gmt":"2024-12-26T22:28:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/977303.html"},"modified":"2024-12-27T06:28:29","modified_gmt":"2024-12-26T22:28:29","slug":"python-%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8b%e8%a1%a8%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/977303.html","title":{"rendered":"python \u5982\u4f55\u67e5\u770b\u8868\u7ed3\u6784"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24202618\/dab594ff-aca3-4901-a911-73b34990c9e9.webp\" alt=\"python \u5982\u4f55\u67e5\u770b\u8868\u7ed3\u6784\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u67e5\u770b\u6570\u636e\u5e93\u8868\u7ed3\u6784\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528SQL\u67e5\u8be2\u76f4\u63a5\u83b7\u53d6\u8868\u7ed3\u6784\u3001\u5229\u7528ORM\u6846\u67b6\uff08\u5982SQLAlchemy\uff09\u67e5\u770b\u8868\u7ed3\u6784\u3001\u4ee5\u53ca\u901a\u8fc7\u6570\u636e\u5e93\u7684\u7ba1\u7406\u5de5\u5177\uff08\u5982SQLite3\u3001Pandas\u7b49\uff09\u83b7\u53d6\u8868\u7ed3\u6784\u3002<\/strong>  <\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c\u4f7f\u7528SQL\u67e5\u8be2\u6765\u83b7\u53d6\u8868\u7ed3\u6784\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u6267\u884c\u6570\u636e\u5e93\u7279\u5b9a\u7684\u67e5\u8be2\u8bed\u53e5\uff0c\u53ef\u4ee5\u83b7\u5f97\u8868\u7684\u5217\u540d\u3001\u6570\u636e\u7c7b\u578b\u3001\u952e\u7ea6\u675f\u7b49\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u5728MySQL\u4e2d\u53ef\u4ee5\u4f7f\u7528<code>DESCRIBE table_name;<\/code>\u6765\u83b7\u53d6\u8868\u7ed3\u6784\u3002\u5728SQLite\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>PRAGMA table_info(table_name);<\/code>\u6765\u83b7\u5f97\u8868\u4fe1\u606f\u3002\u4f7f\u7528SQLAlchemy\u7b49ORM\u6846\u67b6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6620\u5c04\u7c7b\u5c5e\u6027\u6765\u67e5\u770b\u8868\u7ed3\u6784\uff0c\u867d\u7136\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u5982\u76f4\u63a5\u67e5\u8be2\u7075\u6d3b\uff0c\u4f46\u66f4\u9002\u5408\u4e0ePython\u4ee3\u7801\u96c6\u6210\u3002\u540c\u65f6\uff0c\u5229\u7528Pandas\u8bfb\u53d6\u6570\u636e\u5e93\u8868\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>DataFrame<\/code>\u7684\u5c5e\u6027\u67e5\u770b\u5217\u4fe1\u606f\uff0c\u9002\u5408\u7528\u4e8e\u6570\u636e\u5206\u6790\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u5728Python\u4e2d\u5982\u4f55\u67e5\u770b\u8868\u7ed3\u6784\u7684\u4e0d\u540c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528SQL\u67e5\u8be2\u67e5\u770b\u8868\u7ed3\u6784<\/p>\n<\/p>\n<p><p>\u76f4\u63a5\u4f7f\u7528SQL\u67e5\u8be2\u8bed\u53e5\u662f\u67e5\u770b\u6570\u636e\u5e93\u8868\u7ed3\u6784\u7684\u6700\u57fa\u7840\u65b9\u6cd5\u3002\u4e0d\u540c\u7684\u6570\u636e\u5e93\u7cfb\u7edf\u652f\u6301\u4e0d\u540c\u7684\u67e5\u8be2\u8bed\u53e5\u6765\u83b7\u53d6\u8868\u7ed3\u6784\u4fe1\u606f\u3002<\/p>\n<\/p>\n<ol>\n<li>MySQL<\/li>\n<\/ol>\n<p><p>\u5728MySQL\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>DESCRIBE<\/code>\u6216<code>SHOW COLUMNS<\/code>\u547d\u4ee4\u6765\u67e5\u770b\u8868\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<p>def get_mysql_table_structure():<\/p>\n<p>    conn = mysql.connector.connect(<\/p>\n<p>        host=&quot;localhost&quot;,<\/p>\n<p>        user=&quot;yourusername&quot;,<\/p>\n<p>        password=&quot;yourpassword&quot;,<\/p>\n<p>        database=&quot;yourdatabase&quot;<\/p>\n<p>    )<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&quot;DESCRIBE your_table_name;&quot;)<\/p>\n<p>    for row in cursor.fetchall():<\/p>\n<p>        print(row)<\/p>\n<p>    cursor.close()<\/p>\n<p>    conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>PostgreSQL<\/li>\n<\/ol>\n<p><p>\u5728PostgreSQL\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7cfb\u7edf\u8868<code>information_schema<\/code>\u6765\u67e5\u8be2\u8868\u7684\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import psycopg2<\/p>\n<p>def get_postgresql_table_structure():<\/p>\n<p>    conn = psycopg2.connect(<\/p>\n<p>        host=&quot;localhost&quot;,<\/p>\n<p>        database=&quot;yourdatabase&quot;,<\/p>\n<p>        user=&quot;yourusername&quot;,<\/p>\n<p>        password=&quot;yourpassword&quot;<\/p>\n<p>    )<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&quot;&quot;&quot;<\/p>\n<p>        SELECT column_name, data_type, character_maximum_length<\/p>\n<p>        FROM information_schema.columns<\/p>\n<p>        WHERE table_name = &#39;your_table_name&#39;;<\/p>\n<p>    &quot;&quot;&quot;)<\/p>\n<p>    for row in cursor.fetchall():<\/p>\n<p>        print(row)<\/p>\n<p>    cursor.close()<\/p>\n<p>    conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>SQLite<\/li>\n<\/ol>\n<p><p>\u5728SQLite\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>PRAGMA table_info<\/code>\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>def get_sqlite_table_structure():<\/p>\n<p>    conn = sqlite3.connect(&#39;yourdatabase.db&#39;)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&quot;PRAGMA table_info(your_table_name);&quot;)<\/p>\n<p>    for row in cursor.fetchall():<\/p>\n<p>        print(row)<\/p>\n<p>    cursor.close()<\/p>\n<p>    conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u4f7f\u7528SQL\u67e5\u8be2\u65f6\uff0c\u786e\u4fdd\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\u548c\u8868\u540d\u6b63\u786e\u65e0\u8bef\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5229\u7528SQLAlchemy\u67e5\u770b\u8868\u7ed3\u6784<\/p>\n<\/p>\n<p><p>SQLAlchemy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python ORM\u6846\u67b6\uff0c\u53ef\u4ee5\u5e2e\u52a9\u7ba1\u7406\u548c\u64cd\u4f5c\u6570\u636e\u5e93\u3002\u901a\u8fc7SQLAlchemy\u7684\u5143\u6570\u636e\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u67e5\u770b\u8868\u7ed3\u6784\u3002<\/p>\n<\/p>\n<ol>\n<li>\u8bbe\u7f6eSQLAlchemy\u5f15\u64ce\u548c\u5143\u6570\u636e<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine, MetaData, Table<\/p>\n<p>def get_sqlalchemy_table_structure():<\/p>\n<p>    engine = create_engine(&#39;mysql+mysqlconnector:\/\/yourusername:yourpassword@localhost\/yourdatabase&#39;)<\/p>\n<p>    metadata = MetaData()<\/p>\n<p>    table = Table(&#39;your_table_name&#39;, metadata, autoload_with=engine)<\/p>\n<p>    for column in table.columns:<\/p>\n<p>        print(column.name, column.type)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5206\u6790SQLAlchemy\u8f93\u51fa<\/li>\n<\/ol>\n<p><p>SQLAlchemy\u7684\u8f93\u51fa\u76f4\u63a5\u63d0\u4f9b\u4e86\u5217\u540d\u548c\u6570\u636e\u7c7b\u578b\u7684\u4fe1\u606f\uff0c\u80fd\u591f\u5f88\u65b9\u4fbf\u5730\u96c6\u6210\u5230Python\u7a0b\u5e8f\u4e2d\u8fdb\u884c\u66f4\u590d\u6742\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528ORM\u6846\u67b6\u7684\u597d\u5904\u662f\u4ee3\u7801\u4e0e\u6570\u636e\u5e93\u7684\u7d27\u5bc6\u7ed3\u5408\uff0c\u80fd\u591f\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6570\u636e\u4ea4\u4e92\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e09\u3001\u901a\u8fc7\u6570\u636e\u5e93\u7ba1\u7406\u5de5\u5177\u67e5\u770b\u8868\u7ed3\u6784<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u4f7f\u7528\u7b80\u5355\u7684\u6570\u636e\u5e93\u7ba1\u7406\u5de5\u5177\u6216\u5e93\uff0c\u4e5f\u53ef\u4ee5\u65b9\u4fbf\u5730\u67e5\u770b\u8868\u7ed3\u6784\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528Pandas\u8bfb\u53d6\u6570\u636e\u5e93\u8868<\/li>\n<\/ol>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5de5\u5177\u5e93\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u8bfb\u53d6\u8868\u6570\u636e\uff0c\u7136\u540e\u67e5\u770b\u8868\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def get_pandas_table_structure():<\/p>\n<p>    conn_str = &#39;mysql+mysqlconnector:\/\/yourusername:yourpassword@localhost\/yourdatabase&#39;<\/p>\n<p>    query = &#39;SELECT * FROM your_table_name LIMIT 0&#39;<\/p>\n<p>    df = pd.read_sql(query, conn_str)<\/p>\n<p>    print(df.columns)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528SQLite3\u6a21\u5757<\/li>\n<\/ol>\n<p><p>SQLite3\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u53ef\u4ee5\u7528\u4e8eSQLite\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>def get_sqlite3_table_structure():<\/p>\n<p>    conn = sqlite3.connect(&#39;yourdatabase.db&#39;)<\/p>\n<p>    cursor = conn.execute(&quot;SELECT * FROM your_table_name&quot;)<\/p>\n<p>    for column in cursor.description:<\/p>\n<p>        print(column[0])<\/p>\n<p>    conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528Pandas\u6216SQLite3\u6a21\u5757\uff0c\u66f4\u9002\u5408\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u4efb\u52a1\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u67e5\u770b\u8868\u7ed3\u6784\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>SQL\u67e5\u8be2\u65b9\u6cd5<\/strong>\u9002\u5408\u9700\u8981\u76f4\u63a5\u83b7\u53d6\u6570\u636e\u5e93\u4fe1\u606f\u7684\u573a\u666f\uff0c\u9002\u7528\u4e8e\u591a\u79cd\u6570\u636e\u5e93\u7cfb\u7edf\u3002<\/li>\n<li><strong>ORM\u6846\u67b6\uff08\u5982SQLAlchemy\uff09<\/strong>\u9002\u5408\u4e0ePython\u5e94\u7528\u7a0b\u5e8f\u7d27\u5bc6\u96c6\u6210\uff0c\u5c24\u5176\u662f\u9700\u8981\u590d\u6742\u6570\u636e\u4ea4\u4e92\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u6570\u636e\u5e93\u7ba1\u7406\u5de5\u5177\uff08\u5982Pandas\u548cSQLite3\uff09<\/strong>\u9002\u5408\u4e8e\u6570\u636e\u5206\u6790\u548c\u5feb\u901f\u5904\u7406\u4efb\u52a1\u3002<\/li>\n<\/ul>\n<p><p>\u5728\u5b9e\u8df5\u4e2d\uff0c\u786e\u4fdd\u6570\u636e\u5e93\u8fde\u63a5\u7684\u5b89\u5168\u6027\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u5efa\u8bae\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u6216\u914d\u7f6e\u6587\u4ef6\u6765\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\uff0c\u4ee5\u907f\u514d\u654f\u611f\u4fe1\u606f\u5728\u4ee3\u7801\u4e2d\u66b4\u9732\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fde\u63a5\u6570\u636e\u5e93\u4ee5\u67e5\u770b\u8868\u7ed3\u6784\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff0c\u5982<code>sqlite3<\/code>\u3001<code>pymysql<\/code>\u6216<code>psycopg2<\/code>\u7b49\uff0c\u6765\u8fde\u63a5\u6570\u636e\u5e93\u3002\u8fde\u63a5\u6210\u529f\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528SQL\u547d\u4ee4<code>DESCRIBE \u8868\u540d<\/code>\u6216<code>SHOW COLUMNS FROM \u8868\u540d<\/code>\uff08\u5177\u4f53\u547d\u4ee4\u53d6\u51b3\u4e8e\u6570\u636e\u5e93\u7c7b\u578b\uff09\u6765\u67e5\u8be2\u8868\u7684\u7ed3\u6784\u3002\u786e\u4fdd\u5728\u6267\u884c\u8fd9\u4e9b\u547d\u4ee4\u4e4b\u524d\uff0c\u5df2\u7ecf\u6210\u529f\u8fde\u63a5\u5230\u6570\u636e\u5e93\u5e76\u9009\u62e9\u4e86\u76f8\u5e94\u7684\u6570\u636e\u5e93\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u7528\u6765\u67e5\u770b\u6570\u636e\u5e93\u8868\u7684\u7ed3\u6784\uff1f<\/strong><br \/>\u5e38\u7528\u7684Python\u5e93\u5305\u62ec<code>pandas<\/code>\u3001<code>SQLAlchemy<\/code>\u3001<code>sqlite3<\/code>\u3001<code>pymysql<\/code>\u548c<code>psycopg2<\/code>\u3002<code>pandas<\/code>\u63d0\u4f9b\u4e86<code>read_sql<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06SQL\u67e5\u8be2\u7ed3\u679c\u8bfb\u5165DataFrame\uff0c\u4ece\u800c\u67e5\u770b\u8868\u7ed3\u6784\u3002\u540c\u65f6\uff0c<code>SQLAlchemy<\/code>\u5141\u8bb8\u901a\u8fc7\u5143\u6570\u636e\u5bf9\u8c61\u83b7\u53d6\u8868\u7684\u8be6\u7ec6\u7ed3\u6784\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528SQLAlchemy\u67e5\u770b\u8868\u7ed3\u6784\u7684\u8be6\u7ec6\u4fe1\u606f\uff1f<\/strong><br \/>\u4f7f\u7528SQLAlchemy\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u6570\u636e\u5e93\u8fde\u63a5\u548c\u5143\u6570\u636e\u5bf9\u8c61\u6765\u67e5\u770b\u8868\u7684\u8be6\u7ec6\u7ed3\u6784\u3002\u9996\u5148\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93\u5f15\u64ce\uff0c\u7136\u540e\u4f7f\u7528<code>MetaData<\/code>\u5bf9\u8c61\u53cd\u5c04\u8868\u7ed3\u6784\u3002\u901a\u8fc7<code>Table<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95ee\u5217\u540d\u3001\u6570\u636e\u7c7b\u578b\u548c\u5176\u4ed6\u7ea6\u675f\u4fe1\u606f\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u9002\u7528\u4e8e\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff0c\u4e5f\u80fd\u5f88\u597d\u5730\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u67e5\u770b\u6570\u636e\u5e93\u8868\u7ed3\u6784\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528SQL\u67e5\u8be2\u76f4\u63a5\u83b7\u53d6\u8868\u7ed3\u6784\u3001\u5229\u7528ORM\u6846\u67b6\uff08\u5982SQLAlche [&hellip;]","protected":false},"author":3,"featured_media":977307,"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\/977303"}],"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=977303"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977303\/revisions"}],"predecessor-version":[{"id":977308,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977303\/revisions\/977308"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/977307"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=977303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=977303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=977303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}