{"id":1122917,"date":"2025-01-08T19:26:59","date_gmt":"2025-01-08T11:26:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1122917.html"},"modified":"2025-01-08T19:27:02","modified_gmt":"2025-01-08T11:27:02","slug":"python%e5%a6%82%e4%bd%95%e8%b7%9fmysql%e4%b8%80%e8%b5%b7%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1122917.html","title":{"rendered":"python\u5982\u4f55\u8ddfmysql\u4e00\u8d77\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084557\/3d3b7d80-4960-4a15-af09-72a3c13696fa.webp\" alt=\"python\u5982\u4f55\u8ddfmysql\u4e00\u8d77\u7528\" \/><\/p>\n<p><p> <strong>Python\u4e0eMySQL\u7684\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u901a\u8fc7MySQL Connector\u3001SQLAlchemy\u3001PyMySQL\u7b49\u5e93\u5b9e\u73b0\u9ad8\u6548\u7684\u6570\u636e\u7ba1\u7406\u548c\u64cd\u4f5c\u3001Python\u4e0eMySQL\u7684\u7ed3\u5408\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u80fd\u529b\uff0c\u4f7f\u5f97\u6570\u636e\u5b58\u50a8\u3001\u67e5\u8be2\u3001\u66f4\u65b0\u7b49\u64cd\u4f5c\u66f4\u52a0\u4fbf\u6377\u3002<\/strong> \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8Python\u4e0eMySQL\u7ed3\u5408\u4f7f\u7528\u7684\u6700\u4f73\u5b9e\u8df5\u548c\u65b9\u6cd5\uff0c\u5e2e\u52a9\u4f60\u5728\u9879\u76ee\u4e2d\u66f4\u597d\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Python\u4e0eMySQL\u7684\u8fde\u63a5\u65b9\u5f0f<\/h3>\n<\/p>\n<p><h4>1.1 MySQL Connector<\/h4>\n<\/p>\n<p><p>MySQL Connector\u662f\u5b98\u65b9\u63d0\u4f9b\u7684\u7eafPython\u5e93\uff0c\u7528\u4e8e\u8fde\u63a5MySQL\u6570\u636e\u5e93\u3002\u5b83\u6613\u4e8e\u5b89\u88c5\u548c\u4f7f\u7528\uff0c\u652f\u6301\u6240\u6709MySQL\u529f\u80fd\u3002\u8981\u4f7f\u7528\u5b83\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u8be5\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><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<p>config = {<\/p>\n<p>    &#39;user&#39;: &#39;yourusername&#39;,<\/p>\n<p>    &#39;password&#39;: &#39;yourpassword&#39;,<\/p>\n<p>    &#39;host&#39;: &#39;localhost&#39;,<\/p>\n<p>    &#39;database&#39;: &#39;yourdatabase&#39;<\/p>\n<p>}<\/p>\n<p>try:<\/p>\n<p>    conn = mysql.connector.connect(config)<\/p>\n<p>    print(&quot;Connection successful&quot;)<\/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>        print(&quot;Connection closed&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 PyMySQL<\/h4>\n<\/p>\n<p><p>PyMySQL\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684\u5e93\uff0c\u7528\u4e8e\u8fde\u63a5MySQL\u6570\u636e\u5e93\u3002\u5b83\u4e5f\u662f\u7eafPython\u5b9e\u73b0\uff0c\u5b89\u88c5\u548c\u4f7f\u7528\u90fd\u975e\u5e38\u7b80\u5355\u3002\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pymysql<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528PyMySQL\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>connection = pymysql.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;yourusername&#39;,<\/p>\n<p>    password=&#39;yourpassword&#39;,<\/p>\n<p>    database=&#39;yourdatabase&#39;<\/p>\n<p>)<\/p>\n<p>try:<\/p>\n<p>    with connection.cursor() as cursor:<\/p>\n<p>        # Execute a query<\/p>\n<p>        sql = &quot;SELECT VERSION()&quot;<\/p>\n<p>        cursor.execute(sql)<\/p>\n<p>        result = cursor.fetchone()<\/p>\n<p>        print(f&quot;MySQL version: {result}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.3 SQLAlchemy<\/h4>\n<\/p>\n<p><p>SQLAlchemy\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u5e93\uff0c\u5b83\u4e0d\u4ec5\u652f\u6301MySQL\uff0c\u8fd8\u652f\u6301\u5176\u4ed6\u591a\u79cd\u6570\u636e\u5e93\u3002\u5b89\u88c5SQLAlchemy\u548cMySQL\u9a71\u52a8\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install sqlalchemy pymysql<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528SQLAlchemy\u8fde\u63a5MySQL\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine<\/p>\n<p>engine = create_engine(&#39;mysql+pymysql:\/\/yourusername:yourpassword@localhost\/yourdatabase&#39;)<\/p>\n<p>with engine.connect() as connection:<\/p>\n<p>    result = connection.execute(&quot;SELECT VERSION()&quot;)<\/p>\n<p>    for row in result:<\/p>\n<p>        print(f&quot;MySQL version: {row[0]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6570\u636e\u5e93\u64cd\u4f5c\uff1a\u589e\u3001\u5220\u3001\u6539\u3001\u67e5<\/h3>\n<\/p>\n<p><h4>2.1 \u63d2\u5165\u6570\u636e<\/h4>\n<\/p>\n<p><p><strong>\u63d2\u5165\u6570\u636e\u662f\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\u6700\u5e38\u89c1\u7684\u4efb\u52a1\u4e4b\u4e00\u3002<\/strong> \u4f7f\u7528MySQL Connector\u63d2\u5165\u6570\u636e\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<p>config = {<\/p>\n<p>    &#39;user&#39;: &#39;yourusername&#39;,<\/p>\n<p>    &#39;password&#39;: &#39;yourpassword&#39;,<\/p>\n<p>    &#39;host&#39;: &#39;localhost&#39;,<\/p>\n<p>    &#39;database&#39;: &#39;yourdatabase&#39;<\/p>\n<p>}<\/p>\n<p>conn = mysql.connector.connect(config)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<p>sql = &quot;INSERT INTO employees (name, age, department) VALUES (%s, %s, %s)&quot;<\/p>\n<p>values = (&quot;John Doe&quot;, 28, &quot;HR&quot;)<\/p>\n<p>try:<\/p>\n<p>    cursor.execute(sql, values)<\/p>\n<p>    conn.commit()<\/p>\n<p>    print(f&quot;Inserted {cursor.rowcount} row(s)&quot;)<\/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>    cursor.close()<\/p>\n<p>    conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u67e5\u8be2\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u67e5\u8be2\u6570\u636e\u662f\u4e86\u89e3\u6570\u636e\u5e93\u4e2d\u5b58\u50a8\u4fe1\u606f\u7684\u4e3b\u8981\u65b9\u5f0f\u3002\u4f7f\u7528PyMySQL\u67e5\u8be2\u6570\u636e\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>connection = pymysql.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;yourusername&#39;,<\/p>\n<p>    password=&#39;yourpassword&#39;,<\/p>\n<p>    database=&#39;yourdatabase&#39;<\/p>\n<p>)<\/p>\n<p>try:<\/p>\n<p>    with connection.cursor() as cursor:<\/p>\n<p>        sql = &quot;SELECT * FROM employees&quot;<\/p>\n<p>        cursor.execute(sql)<\/p>\n<p>        results = cursor.fetchall()<\/p>\n<p>        for row in results:<\/p>\n<p>            print(row)<\/p>\n<p>finally:<\/p>\n<p>    connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.3 \u66f4\u65b0\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u66f4\u65b0\u6570\u636e\u7528\u4e8e\u4fee\u6539\u73b0\u6709\u8bb0\u5f55\u3002\u4f7f\u7528SQLAlchemy\u66f4\u65b0\u6570\u636e\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine, Table, MetaData<\/p>\n<p>engine = create_engine(&#39;mysql+pymysql:\/\/yourusername:yourpassword@localhost\/yourdatabase&#39;)<\/p>\n<p>metadata = MetaData()<\/p>\n<p>employees = Table(&#39;employees&#39;, metadata, autoload_with=engine)<\/p>\n<p>with engine.connect() as connection:<\/p>\n<p>    update_stmt = employees.update().where(employees.c.name == &#39;John Doe&#39;).values(age=29)<\/p>\n<p>    connection.execute(update_stmt)<\/p>\n<p>    print(&quot;Update successful&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.4 \u5220\u9664\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5220\u9664\u6570\u636e\u7528\u4e8e\u79fb\u9664\u4e0d\u518d\u9700\u8981\u7684\u8bb0\u5f55\u3002\u4f7f\u7528MySQL Connector\u5220\u9664\u6570\u636e\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<p>config = {<\/p>\n<p>    &#39;user&#39;: &#39;yourusername&#39;,<\/p>\n<p>    &#39;password&#39;: &#39;yourpassword&#39;,<\/p>\n<p>    &#39;host&#39;: &#39;localhost&#39;,<\/p>\n<p>    &#39;database&#39;: &#39;yourdatabase&#39;<\/p>\n<p>}<\/p>\n<p>conn = mysql.connector.connect(config)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<p>sql = &quot;DELETE FROM employees WHERE name = %s&quot;<\/p>\n<p>values = (&quot;John Doe&quot;,)<\/p>\n<p>try:<\/p>\n<p>    cursor.execute(sql, values)<\/p>\n<p>    conn.commit()<\/p>\n<p>    print(f&quot;Deleted {cursor.rowcount} row(s)&quot;)<\/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>    cursor.close()<\/p>\n<p>    conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4e8b\u52a1\u5904\u7406<\/h3>\n<\/p>\n<p><h4>3.1 \u4e8b\u52a1\u7684\u57fa\u672c\u6982\u5ff5<\/h4>\n<\/p>\n<p><p><strong>\u4e8b\u52a1\u662f\u4e00\u7ec4\u903b\u8f91\u4e0a\u7684\u64cd\u4f5c\u5355\u5143<\/strong>\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u8981\u4e48\u5168\u90fd\u6210\u529f\uff0c\u8981\u4e48\u5168\u90fd\u5931\u8d25\u3002\u4e8b\u52a1\u5177\u6709\u56db\u4e2a\u7279\u6027\uff1a\u539f\u5b50\u6027\u3001\u4e00\u81f4\u6027\u3001\u9694\u79bb\u6027\u548c\u6301\u4e45\u6027\uff08ACID\uff09\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u4f7f\u7528\u4e8b\u52a1<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\uff0c\u4e8b\u52a1\u5904\u7406\u662f\u4fdd\u8bc1\u6570\u636e\u4e00\u81f4\u6027\u7684\u91cd\u8981\u624b\u6bb5\u3002\u4f7f\u7528PyMySQL\u8fdb\u884c\u4e8b\u52a1\u5904\u7406\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>connection = pymysql.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;yourusername&#39;,<\/p>\n<p>    password=&#39;yourpassword&#39;,<\/p>\n<p>    database=&#39;yourdatabase&#39;<\/p>\n<p>)<\/p>\n<p>try:<\/p>\n<p>    with connection.cursor() as cursor:<\/p>\n<p>        sql1 = &quot;UPDATE employees SET age = age + 1 WHERE name = &#39;John Doe&#39;&quot;<\/p>\n<p>        sql2 = &quot;INSERT INTO logs (action) VALUES (&#39;Updated age for John Doe&#39;)&quot;<\/p>\n<p>        cursor.execute(sql1)<\/p>\n<p>        cursor.execute(sql2)<\/p>\n<p>        connection.commit()  # \u63d0\u4ea4\u4e8b\u52a1<\/p>\n<p>        print(&quot;Transaction successful&quot;)<\/p>\n<p>except pymysql.MySQLError as err:<\/p>\n<p>    connection.rollback()  # \u56de\u6eda\u4e8b\u52a1<\/p>\n<p>    print(f&quot;Transaction f<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led: {err}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b89\u5168\u6027\u4e0e\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><h4>4.1 \u9632\u6b62SQL\u6ce8\u5165<\/h4>\n<\/p>\n<p><p><strong>SQL\u6ce8\u5165\u662f\u4e00\u79cd\u901a\u8fc7\u63d2\u5165\u6216\u201c\u6ce8\u5165\u201d\u6076\u610fSQL\u67e5\u8be2\u6765\u653b\u51fb\u5e94\u7528\u7a0b\u5e8f\u7684\u6280\u672f<\/strong>\u3002\u4f7f\u7528\u53c2\u6570\u5316\u67e5\u8be2\u53ef\u4ee5\u6709\u6548\u9632\u6b62SQL\u6ce8\u5165\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5728MySQL Connector\u4e2d\u4f7f\u7528\u53c2\u6570\u5316\u67e5\u8be2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<p>config = {<\/p>\n<p>    &#39;user&#39;: &#39;yourusername&#39;,<\/p>\n<p>    &#39;password&#39;: &#39;yourpassword&#39;,<\/p>\n<p>    &#39;host&#39;: &#39;localhost&#39;,<\/p>\n<p>    &#39;database&#39;: &#39;yourdatabase&#39;<\/p>\n<p>}<\/p>\n<p>conn = mysql.connector.connect(config)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<p>sql = &quot;SELECT * FROM employees WHERE name = %s&quot;<\/p>\n<p>name = (&quot;John Doe&quot;,)<\/p>\n<p>cursor.execute(sql, name)<\/p>\n<p>results = cursor.fetchall()<\/p>\n<p>for row in results:<\/p>\n<p>    print(row)<\/p>\n<p>cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u7d22\u5f15\u4f18\u5316<\/h4>\n<\/p>\n<p><p><strong>\u7d22\u5f15\u662f\u52a0\u901f\u6570\u636e\u5e93\u67e5\u8be2\u7684\u4e00\u79cd\u6709\u6548\u624b\u6bb5<\/strong>\u3002\u5728\u521b\u5efa\u7d22\u5f15\u65f6\uff0c\u9700\u8981\u8003\u8651\u7d22\u5f15\u7684\u9009\u62e9\u6027\u3001\u7d22\u5f15\u7684\u7c7b\u578b\u4ee5\u53ca\u5bf9\u5199\u64cd\u4f5c\u7684\u5f71\u54cd\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5728MySQL\u4e2d\u521b\u5efa\u7d22\u5f15\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sql\">CREATE INDEX idx_name ON employees(name);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.3 \u4f7f\u7528\u8fde\u63a5\u6c60<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u8fde\u63a5\u6c60\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u5e93\u8fde\u63a5\u7684\u6548\u7387\uff0c\u907f\u514d\u9891\u7e41\u6253\u5f00\u548c\u5173\u95ed\u8fde\u63a5\u5e26\u6765\u7684\u5f00\u9500\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528SQLAlchemy\u521b\u5efa\u8fde\u63a5\u6c60\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine<\/p>\n<p>engine = create_engine(<\/p>\n<p>    &#39;mysql+pymysql:\/\/yourusername:yourpassword@localhost\/yourdatabase&#39;,<\/p>\n<p>    pool_size=10,<\/p>\n<p>    max_overflow=20<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6570\u636e\u8fc1\u79fb\u4e0e\u5907\u4efd<\/h3>\n<\/p>\n<p><h4>5.1 \u6570\u636e\u8fc1\u79fb<\/h4>\n<\/p>\n<p><p><strong>\u6570\u636e\u8fc1\u79fb\u662f\u5c06\u6570\u636e\u4ece\u4e00\u4e2a\u6570\u636e\u5e93\u8f6c\u79fb\u5230\u53e6\u4e00\u4e2a\u6570\u636e\u5e93\u7684\u8fc7\u7a0b<\/strong>\u3002\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u6570\u636e\u5e93\u5347\u7ea7\u6216\u66f4\u6362\u6570\u636e\u5e93\u7cfb\u7edf\uff0c\u8fd9\u65f6\u5c31\u9700\u8981\u6570\u636e\u8fc1\u79fb\u3002\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u8fc1\u79fb\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>source_connection = pymysql.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;sourceusername&#39;,<\/p>\n<p>    password=&#39;sourcepassword&#39;,<\/p>\n<p>    database=&#39;sourcedatabase&#39;<\/p>\n<p>)<\/p>\n<p>target_connection = pymysql.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;targetusername&#39;,<\/p>\n<p>    password=&#39;targetpassword&#39;,<\/p>\n<p>    database=&#39;targetdatabase&#39;<\/p>\n<p>)<\/p>\n<p>try:<\/p>\n<p>    with source_connection.cursor() as source_cursor, target_connection.cursor() as target_cursor:<\/p>\n<p>        source_cursor.execute(&quot;SELECT * FROM employees&quot;)<\/p>\n<p>        rows = source_cursor.fetchall()<\/p>\n<p>        for row in rows:<\/p>\n<p>            target_cursor.execute(<\/p>\n<p>                &quot;INSERT INTO employees (name, age, department) VALUES (%s, %s, %s)&quot;, <\/p>\n<p>                row<\/p>\n<p>            )<\/p>\n<p>        target_connection.commit()<\/p>\n<p>        print(&quot;Data migration successful&quot;)<\/p>\n<p>except pymysql.MySQLError as err:<\/p>\n<p>    print(f&quot;Data migration failed: {err}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    source_connection.close()<\/p>\n<p>    target_connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2 \u6570\u636e\u5907\u4efd<\/h4>\n<\/p>\n<p><p><strong>\u6570\u636e\u5907\u4efd\u662f\u4fdd\u8bc1\u6570\u636e\u5b89\u5168\u7684\u91cd\u8981\u624b\u6bb5<\/strong>\u3002\u901a\u8fc7\u5b9a\u671f\u5907\u4efd\u6570\u636e\uff0c\u53ef\u4ee5\u5728\u6570\u636e\u4e22\u5931\u65f6\u8fdb\u884c\u6062\u590d\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u5907\u4efd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import time<\/p>\n<p>def backup_database():<\/p>\n<p>    db_user = &#39;yourusername&#39;<\/p>\n<p>    db_password = &#39;yourpassword&#39;<\/p>\n<p>    db_name = &#39;yourdatabase&#39;<\/p>\n<p>    backup_dir = &#39;\/path\/to\/backup&#39;<\/p>\n<p>    if not os.path.exists(backup_dir):<\/p>\n<p>        os.makedirs(backup_dir)<\/p>\n<p>    timestamp = time.strftime(&#39;%Y%m%d%H%M%S&#39;)<\/p>\n<p>    backup_file = os.path.join(backup_dir, f&quot;{db_name}_{timestamp}.sql&quot;)<\/p>\n<p>    command = f&quot;mysqldump -u {db_user} -p{db_password} {db_name} &gt; {backup_file}&quot;<\/p>\n<p>    os.system(command)<\/p>\n<p>    print(f&quot;Backup completed: {backup_file}&quot;)<\/p>\n<p>backup_database()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u4e86Python\u4e0eMySQL\u7ed3\u5408\u4f7f\u7528\u7684\u5404\u79cd\u65b9\u6cd5\u548c\u6700\u4f73\u5b9e\u8df5\u3002\u4ece\u8fde\u63a5\u6570\u636e\u5e93\u3001\u6267\u884c\u57fa\u672c\u7684\u589e\u5220\u6539\u67e5\u64cd\u4f5c\uff0c\u5230\u4e8b\u52a1\u5904\u7406\u3001\u5b89\u5168\u6027\u4e0e\u6027\u80fd\u4f18\u5316\uff0c\u518d\u5230\u6570\u636e\u8fc1\u79fb\u4e0e\u5907\u4efd\uff0c\u6bcf\u4e00\u4e2a\u65b9\u9762\u90fd\u63d0\u4f9b\u4e86\u8be6\u7ec6\u7684\u793a\u4f8b\u4ee3\u7801\u548c\u8bf4\u660e\u3002<strong>\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u9879\u76ee\u4e2d\u66f4\u597d\u5730\u7ba1\u7406\u548c\u64cd\u4f5cMySQL\u6570\u636e\u5e93<\/strong>\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u6570\u636e\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u4f7f\u7528MySQL Connector\u3001PyMySQL\u8fd8\u662fSQLAlchemy\uff0cPython\u90fd\u80fd\u4e3a\u4f60\u63d0\u4f9b\u5f3a\u5927\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u80fd\u529b\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u4e3a\u4f60\u63d0\u4f9b\u5b9e\u7528\u7684\u6307\u5bfc\uff0c\u5e2e\u52a9\u4f60\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u66f4\u597d\u5730\u4f7f\u7528Python\u4e0eMySQL\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fde\u63a5MySQL\u6570\u636e\u5e93\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u8fde\u63a5MySQL\u6570\u636e\u5e93\uff0c\u60a8\u9700\u8981\u5b89\u88c5MySQL Connector\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5\uff1a<code>pip install mysql-connector-python<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fde\u63a5\u6570\u636e\u5e93\uff1a<\/p>\n<pre><code class=\"language-python\">import mysql.connector\n\nconn = mysql.connector.connect(\n    host=&#39;localhost&#39;,\n    user=&#39;your_username&#39;,\n    password=&#39;your_password&#39;,\n    database=&#39;your_database&#39;\n)\n\ncursor = conn.cursor()\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5c06<code>your_username<\/code>\u3001<code>your_password<\/code>\u548c<code>your_database<\/code>\u66ff\u6362\u4e3a\u5b9e\u9645\u7684\u6570\u636e\u5e93\u51ed\u636e\u3002<\/p>\n<p><strong>Python\u4e0eMySQL\u4ea4\u4e92\u65f6\u5e38\u89c1\u7684\u64cd\u4f5c\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728Python\u4e0eMySQL\u4ea4\u4e92\u65f6\uff0c\u5e38\u89c1\u7684\u64cd\u4f5c\u5305\u62ec\u6570\u636e\u7684\u63d2\u5165\u3001\u67e5\u8be2\u3001\u66f4\u65b0\u548c\u5220\u9664\uff08CRUD\u64cd\u4f5c\uff09\u3002\u4f7f\u7528\u6e38\u6807\u5bf9\u8c61\u6267\u884cSQL\u8bed\u53e5\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e9b\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u63d2\u5165\u6570\u636e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">sql = &quot;INSERT INTO your_table (column1, column2) VALUES (%s, %s)&quot;\nvalues = (value1, value2)\ncursor.execute(sql, values)\nconn.commit()\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u4f7f\u7528<code>conn.commit()<\/code>\u6765\u4fdd\u5b58\u66f4\u6539\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u4e2dMySQL\u67e5\u8be2\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u4e0eMySQL\u8fdb\u884c\u4ea4\u4e92\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\uff0c\u6bd4\u5982\u8fde\u63a5\u5931\u8d25\u6216SQL\u8bed\u6cd5\u9519\u8bef\u3002\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u6765\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    cursor.execute(&quot;SELECT * FROM your_table&quot;)\nexcept mysql.connector.Error as err:\n    print(f&quot;Something went wrong: {err}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u8c03\u8bd5\u5e76\u786e\u4fdd\u7a0b\u5e8f\u5728\u9047\u5230\u9519\u8bef\u65f6\u4e0d\u4f1a\u5d29\u6e83\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e0eMySQL\u7684\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u901a\u8fc7MySQL Connector\u3001SQLAlchemy\u3001PyMySQ [&hellip;]","protected":false},"author":3,"featured_media":1122924,"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\/1122917"}],"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=1122917"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1122917\/revisions"}],"predecessor-version":[{"id":1122928,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1122917\/revisions\/1122928"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1122924"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1122917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1122917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1122917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}