{"id":1142468,"date":"2025-01-08T22:40:27","date_gmt":"2025-01-08T14:40:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1142468.html"},"modified":"2025-01-08T22:40:29","modified_gmt":"2025-01-08T14:40:29","slug":"%e5%a6%82%e4%bd%95%e5%b0%86python%e4%ba%8c%e7%bb%b4%e5%ad%97%e5%85%b8%e5%ad%98%e5%85%a5%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1142468.html","title":{"rendered":"\u5982\u4f55\u5c06Python\u4e8c\u7ef4\u5b57\u5178\u5b58\u5165\u6570\u636e\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180600\/f9bd7026-7552-44f9-8229-9b7a39528aff.webp\" alt=\"\u5982\u4f55\u5c06Python\u4e8c\u7ef4\u5b57\u5178\u5b58\u5165\u6570\u636e\u5e93\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u5c06Python\u4e8c\u7ef4\u5b57\u5178\u5b58\u5165\u6570\u636e\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5c06Python\u4e8c\u7ef4\u5b57\u5178\u5b58\u5165\u6570\u636e\u5e93\u7684\u5173\u952e\u6b65\u9aa4\u5305\u62ec<strong>\u5b9a\u4e49\u6570\u636e\u5e93\u7ed3\u6784\u3001\u8fde\u63a5\u6570\u636e\u5e93\u3001\u904d\u5386\u5b57\u5178\u5e76\u63d2\u5165\u6570\u636e\u3001\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38<\/strong>\u3002\u5176\u4e2d\uff0c\u5b9a\u4e49\u6570\u636e\u5e93\u7ed3\u6784\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u6570\u636e\u5e93\u8868\u7684\u8bbe\u8ba1\u4f1a\u76f4\u63a5\u5f71\u54cd\u6570\u636e\u7684\u5b58\u50a8\u548c\u67e5\u8be2\u6548\u7387\u3002\u63a5\u4e0b\u6765\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b8c\u6210\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u6570\u636e\u5e93\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u5c06Python\u4e8c\u7ef4\u5b57\u5178\u5b58\u5165\u6570\u636e\u5e93\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b9a\u4e49\u6570\u636e\u5e93\u7684\u7ed3\u6784\u3002\u6570\u636e\u5e93\u7ed3\u6784\u7684\u5b9a\u4e49\u5305\u62ec\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u7c7b\u578b\uff08\u5982MySQL\u3001PostgreSQL\u3001SQLite\u7b49\uff09\u3001\u521b\u5efa\u6570\u636e\u5e93\u548c\u8868\u3002<\/p>\n<\/p>\n<p><h4>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u6570\u636e\u5e93\u7c7b\u578b\u65f6\uff0c\u9700\u8981\u8003\u8651\u6570\u636e\u7684\u5927\u5c0f\u3001\u67e5\u8be2\u9891\u7387\u3001\u5e76\u53d1\u91cf\u7b49\u56e0\u7d20\u3002MySQL\u548cPostgreSQL\u662f\u5e38\u7528\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff0c\u9002\u5408\u5927\u591a\u6570\u573a\u666f\u3002SQLite\u5219\u9002\u5408\u5c0f\u578b\u5e94\u7528\u6216\u5f00\u53d1\u6d4b\u8bd5\u9636\u6bb5\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u6570\u636e\u5e93\u548c\u8868<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u6570\u636e\u5e93\u548c\u8868\u7684\u6b65\u9aa4\u56e0\u6570\u636e\u5e93\u7c7b\u578b\u800c\u5f02\uff0c\u4f46\u57fa\u672c\u8fc7\u7a0b\u76f8\u4f3c\u3002\u4ee5\u4e0b\u4ee5MySQL\u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u5982\u4f55\u521b\u5efa\u6570\u636e\u5e93\u548c\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sql\">CREATE DATABASE mydatabase;<\/p>\n<p>USE mydatabase;<\/p>\n<p>CREATE TABLE mytable (<\/p>\n<p>    id INT AUTO_INCREMENT PRIMARY KEY,<\/p>\n<p>    key1 VARCHAR(255),<\/p>\n<p>    key2 VARCHAR(255),<\/p>\n<p>    value TEXT<\/p>\n<p>);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0SQL\u8bed\u53e5\u4e2d\uff0c<code>id<\/code>\u662f\u8868\u7684\u4e3b\u952e\uff0c<code>key1<\/code>\u548c<code>key2<\/code>\u662f\u5b57\u5178\u7684\u952e\uff0c<code>value<\/code>\u662f\u5b57\u5178\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8fde\u63a5\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff08\u5982<code>mysql-connector-python<\/code>\u3001<code>psycopg2<\/code>\u3001<code>sqlite3<\/code>\uff09\u8fde\u63a5\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5\u6570\u636e\u5e93\u8fde\u63a5\u5e93<\/h4>\n<\/p>\n<p><p>\u4ee5<code>mysql-connector-python<\/code>\u4e3a\u4f8b\uff0c\u5b89\u88c5\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install mysql-connector-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8fde\u63a5\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>mysql-connector-python<\/code>\u8fde\u63a5MySQL\u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<p>def connect_to_database():<\/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;mydatabase&quot;<\/p>\n<p>    )<\/p>\n<p>    return conn<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u904d\u5386\u5b57\u5178\u5e76\u63d2\u5165\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5c06\u4e8c\u7ef4\u5b57\u5178\u7684\u6570\u636e\u63d2\u5165\u6570\u636e\u5e93\u9700\u8981\u904d\u5386\u5b57\u5178\uff0c\u5e76\u6267\u884cINSERT\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4e8c\u7ef4\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6709\u5982\u4e0b\u4e8c\u7ef4\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {<\/p>\n<p>    &quot;key1_1&quot;: {&quot;key2_1&quot;: &quot;value1_1&quot;, &quot;key2_2&quot;: &quot;value1_2&quot;},<\/p>\n<p>    &quot;key1_2&quot;: {&quot;key2_1&quot;: &quot;value2_1&quot;, &quot;key2_2&quot;: &quot;value2_2&quot;}<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u63d2\u5165\u6570\u636e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def insert_data(conn, data):<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    for key1, subdict in data.items():<\/p>\n<p>        for key2, value in subdict.items():<\/p>\n<p>            sql = &quot;INSERT INTO mytable (key1, key2, value) VALUES (%s, %s, %s)&quot;<\/p>\n<p>            val = (key1, key2, value)<\/p>\n<p>            cursor.execute(sql, val)<\/p>\n<p>    conn.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0c\u9700\u8981\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><h4>\u6355\u83b7\u5f02\u5e38<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    conn = connect_to_database()<\/p>\n<p>    insert_data(conn, data)<\/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><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c06Python\u4e8c\u7ef4\u5b57\u5178\u5b58\u5165\u6570\u636e\u5e93\u7684\u6b65\u9aa4\u5305\u62ec<strong>\u5b9a\u4e49\u6570\u636e\u5e93\u7ed3\u6784\u3001\u8fde\u63a5\u6570\u636e\u5e93\u3001\u904d\u5386\u5b57\u5178\u5e76\u63d2\u5165\u6570\u636e\u3001\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38<\/strong>\u3002\u5b9a\u4e49\u6570\u636e\u5e93\u7ed3\u6784\u662f\u5173\u952e\u6b65\u9aa4\uff0c\u76f4\u63a5\u5f71\u54cd\u6570\u636e\u5b58\u50a8\u548c\u67e5\u8be2\u6548\u7387\u3002\u8fde\u63a5\u6570\u636e\u5e93\u9700\u8981\u4f7f\u7528\u76f8\u5e94\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff0c\u904d\u5386\u5b57\u5178\u5e76\u6267\u884cINSERT\u8bed\u53e5\u5c06\u6570\u636e\u63d2\u5165\u6570\u636e\u5e93\uff0c\u540c\u65f6\u9700\u8981\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f18\u5316\u4e0e\u6269\u5c55<\/h3>\n<\/p>\n<p><h4>\u6279\u91cf\u63d2\u5165\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u6570\u636e\u63d2\u5165\u6548\u7387\uff0c\u53ef\u4ee5\u4f7f\u7528\u6279\u91cf\u63d2\u5165\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def insert_data_bulk(conn, data):<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    sql = &quot;INSERT INTO mytable (key1, key2, value) VALUES (%s, %s, %s)&quot;<\/p>\n<p>    val = [(key1, key2, value) for key1, subdict in data.items() for key2, value in subdict.items()]<\/p>\n<p>    cursor.executemany(sql, val)<\/p>\n<p>    conn.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528ORM\u6846\u67b6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528ORM\u6846\u67b6\uff08\u5982SQLAlchemy\uff09\u53ef\u4ee5\u7b80\u5316\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData<\/p>\n<p>from sqlalchemy.orm import sessionmaker<\/p>\n<p>engine = create_engine(&#39;mysql+mysqlconnector:\/\/yourusername:yourpassword@localhost\/mydatabase&#39;)<\/p>\n<p>metadata = MetaData()<\/p>\n<p>mytable = Table(&#39;mytable&#39;, metadata,<\/p>\n<p>    Column(&#39;id&#39;, Integer, primary_key=True),<\/p>\n<p>    Column(&#39;key1&#39;, String(255)),<\/p>\n<p>    Column(&#39;key2&#39;, String(255)),<\/p>\n<p>    Column(&#39;value&#39;, String(255))<\/p>\n<p>)<\/p>\n<p>metadata.create_all(engine)<\/p>\n<p>Session = sessionmaker(bind=engine)<\/p>\n<p>session = Session()<\/p>\n<p>def insert_data_orm(data):<\/p>\n<p>    for key1, subdict in data.items():<\/p>\n<p>        for key2, value in subdict.items():<\/p>\n<p>            ins = mytable.insert().values(key1=key1, key2=key2, value=value)<\/p>\n<p>            conn.execute(ins)<\/p>\n<p>    session.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5c06Python\u4e8c\u7ef4\u5b57\u5178\u5b58\u5165\u6570\u636e\u5e93\u6d89\u53ca\u591a\u4e2a\u6b65\u9aa4\uff0c\u5305\u62ec<strong>\u5b9a\u4e49\u6570\u636e\u5e93\u7ed3\u6784\u3001\u8fde\u63a5\u6570\u636e\u5e93\u3001\u904d\u5386\u5b57\u5178\u5e76\u63d2\u5165\u6570\u636e\u3001\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38<\/strong>\u3002\u901a\u8fc7\u6279\u91cf\u63d2\u5165\u6570\u636e\u548c\u4f7f\u7528ORM\u6846\u67b6\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u548c\u6269\u5c55\u6570\u636e\u5b58\u50a8\u64cd\u4f5c\u3002\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u7c7b\u578b\u548c\u8fde\u63a5\u5e93\uff0c\u8bbe\u8ba1\u5408\u7406\u7684\u6570\u636e\u5e93\u8868\u7ed3\u6784\uff0c\u662f\u786e\u4fdd\u6570\u636e\u5b58\u50a8\u9ad8\u6548\u548c\u53ef\u9760\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u4e2d\u7684\u4e8c\u7ef4\u5b57\u5178\u8f6c\u6362\u4e3a\u9002\u5408\u6570\u636e\u5e93\u5b58\u50a8\u7684\u683c\u5f0f\uff1f<\/strong><br \/>\u5728\u5c06Python\u4e8c\u7ef4\u5b57\u5178\u5b58\u5165\u6570\u636e\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u9002\u5408\u6570\u636e\u5e93\u8868\u7684\u7ed3\u6784\u3002\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u5faa\u73af\u6765\u63d0\u53d6\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5305\u542b\u5143\u7ec4\u6216\u5b57\u5178\u7684\u5217\u8868\u3002\u8fd9\u6837\u7684\u7ed3\u6784\u4fbf\u4e8e\u63d2\u5165\u5230\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<p><strong>\u5728\u5b58\u50a8\u4e8c\u7ef4\u5b57\u5178\u65f6\uff0c\u5e94\u8be5\u9009\u62e9\u54ea\u79cd\u6570\u636e\u5e93\u7cfb\u7edf\uff1f<\/strong><br \/>\u9009\u62e9\u6570\u636e\u5e93\u7cfb\u7edf\u65f6\uff0c\u5e94\u8be5\u8003\u8651\u6570\u636e\u7684\u6027\u8d28\u548c\u9700\u6c42\u3002\u5982\u679c\u6570\u636e\u7ed3\u6784\u76f8\u5bf9\u7b80\u5355\uff0c\u53ef\u4ee5\u9009\u62e9SQLite\u6216MySQL\u7b49\u5173\u7cfb\u578b\u6570\u636e\u5e93\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u66f4\u590d\u6742\u7684\u975e\u7ed3\u6784\u5316\u6570\u636e\uff0cNoSQL\u6570\u636e\u5e93\u5982MongoDB\u53ef\u80fd\u66f4\u9002\u5408\u3002\u8bc4\u4f30\u6570\u636e\u5e93\u7684\u6027\u80fd\u3001\u6269\u5c55\u6027\u548c\u6613\u7528\u6027\u4e5f\u662f\u5341\u5206\u91cd\u8981\u7684\u3002<\/p>\n<p><strong>\u5b58\u5165\u6570\u636e\u5e93\u540e\uff0c\u5982\u4f55\u6709\u6548\u5730\u67e5\u8be2\u548c\u66f4\u65b0\u4e8c\u7ef4\u5b57\u5178\u7684\u6570\u636e\uff1f<\/strong><br \/>\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u50a8\u6570\u636e\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528SQL\u67e5\u8be2\u8bed\u53e5\u6765\u68c0\u7d22\u548c\u66f4\u65b0\u6570\u636e\u3002\u4f7f\u7528JOIN\u64cd\u4f5c\u53ef\u4ee5\u5c06\u76f8\u5173\u8868\u7684\u6570\u636e\u7ed3\u5408\u5728\u4e00\u8d77\uff0c\u5f62\u6210\u66f4\u590d\u6742\u7684\u67e5\u8be2\u3002\u6b64\u5916\uff0c\u4e3a\u4e86\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\uff0c\u53ef\u4ee5\u5728\u5b57\u5178\u7684\u5173\u952e\u5b57\u6bb5\u4e0a\u5efa\u7acb\u7d22\u5f15\u3002\u66f4\u65b0\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528UPDATE\u8bed\u53e5\u5e76\u7ed3\u5408WHERE\u5b50\u53e5\u6765\u786e\u4fdd\u53ea\u4fee\u6539\u7279\u5b9a\u7684\u8bb0\u5f55\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u5c06Python\u4e8c\u7ef4\u5b57\u5178\u5b58\u5165\u6570\u636e\u5e93 \u5c06Python\u4e8c\u7ef4\u5b57\u5178\u5b58\u5165\u6570\u636e\u5e93\u7684\u5173\u952e\u6b65\u9aa4\u5305\u62ec\u5b9a\u4e49\u6570\u636e\u5e93\u7ed3\u6784\u3001\u8fde\u63a5\u6570\u636e\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1142473,"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\/1142468"}],"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=1142468"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1142468\/revisions"}],"predecessor-version":[{"id":1142476,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1142468\/revisions\/1142476"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1142473"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1142468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1142468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1142468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}