{"id":1018657,"date":"2024-12-27T12:44:46","date_gmt":"2024-12-27T04:44:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1018657.html"},"modified":"2024-12-27T12:44:48","modified_gmt":"2024-12-27T04:44:48","slug":"python%e4%b8%8emysql%e5%a6%82%e4%bd%95%e8%bf%9e%e6%8e%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1018657.html","title":{"rendered":"python\u4e0emysql\u5982\u4f55\u8fde\u63a5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25161312\/c46d2abb-3cf9-43c6-b291-9152daa2f90a.webp\" alt=\"python\u4e0emysql\u5982\u4f55\u8fde\u63a5\" \/><\/p>\n<p><p> Python\u4e0eMySQL\u7684\u8fde\u63a5\u53ef\u4ee5\u901a\u8fc7<strong>\u4f7f\u7528MySQL Connector\u3001\u5229\u7528SQLAlchemy\u5e93\u3001\u6216\u901a\u8fc7PyMySQL\u5e93<\/strong>\u6765\u5b9e\u73b0\u3002\u5176\u4e2d\uff0c<strong>MySQL Connector<\/strong>\u662f\u7531MySQL\u5b98\u65b9\u63d0\u4f9b\u7684Python\u5e93\uff0c\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u3002\u5b83\u5141\u8bb8Python\u7a0b\u5e8f\u4e0eMySQL\u6570\u636e\u5e93\u8fdb\u884c\u901a\u4fe1\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528MySQL Connector\u8fde\u63a5Python\u4e0eMySQL\u6570\u636e\u5e93\u7684\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><p>MySQL Connector \u662f\u4e00\u4e2a\u7eafPython\u5b9e\u73b0\u7684\u5e93\uff0c\u65e0\u9700\u5b89\u88c5\u5176\u4ed6\u7ec4\u4ef6\u5373\u53ef\u4f7f\u7528\u3002\u4f7f\u7528\u5b83\u53ef\u4ee5\u6267\u884c\u5404\u79cd\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u5982\u521b\u5efa\u6570\u636e\u5e93\u548c\u8868\u3001\u63d2\u5165\u3001\u66f4\u65b0\u3001\u5220\u9664\u548c\u67e5\u8be2\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528MySQL Connector\u7684\u4e00\u4e9b\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5MySQL Connector<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528MySQL Connector\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5728\u547d\u4ee4\u884c\u4e2d\u5b89\u88c5\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\u5373\u53ef\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u8be5\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5efa\u7acb\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528MySQL Connector\u4e4b\u524d\uff0c\u5fc5\u987b\u5148\u4e0e\u6570\u636e\u5e93\u5efa\u7acb\u8fde\u63a5\u3002MySQL Connector\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>connect<\/code>\u65b9\u6cd5\uff0c\u7528\u4e8e\u521b\u5efa\u4e0e\u6570\u636e\u5e93\u7684\u8fde\u63a5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<h2><strong>\u521b\u5efa\u8fde\u63a5<\/strong><\/h2>\n<p>conn = mysql.connector.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<h2><strong>\u68c0\u67e5\u8fde\u63a5\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if conn.is_connected():<\/p>\n<p>    print(&quot;Successfully connected to the database&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Connection f<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c<code>host<\/code>\u3001<code>user<\/code>\u3001<code>password<\/code>\u548c<code>database<\/code>\u53c2\u6570\u9700\u8981\u6839\u636e\u5b9e\u9645\u7684\u6570\u636e\u5e93\u914d\u7f6e\u8fdb\u884c\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6267\u884cSQL\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u5efa\u7acb\u8fde\u63a5\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fde\u63a5\u5bf9\u8c61\u7684<code>cursor<\/code>\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61\uff0c\u901a\u8fc7\u6e38\u6807\u5bf9\u8c61\u6267\u884cSQL\u67e5\u8be2\u3002\u4ee5\u4e0b\u662f\u6267\u884cSQL\u8bed\u53e5\u7684\u57fa\u672c\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>cursor.execute(&quot;CREATE TABLE IF NOT EXISTS employees (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), salary INT)&quot;)<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&quot;INSERT INTO employees (name, salary) VALUES (%s, %s)&quot;, (&#39;John Doe&#39;, 50000))<\/p>\n<h2><strong>\u63d0\u4ea4\u4e8b\u52a1<\/strong><\/h2>\n<p>conn.commit()<\/p>\n<h2><strong>\u67e5\u8be2\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT * FROM employees&quot;)<\/p>\n<p>result = cursor.fetchall()<\/p>\n<p>for row in result:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>execute<\/code>\u65b9\u6cd5\u7528\u4e8e\u6267\u884cSQL\u8bed\u53e5\uff0c<code>commit<\/code>\u65b9\u6cd5\u7528\u4e8e\u63d0\u4ea4\u4e8b\u52a1\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u6301\u4e45\u5316\u3002\u4f7f\u7528<code>fetchall<\/code>\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u67e5\u8be2\u7684\u6240\u6709\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5173\u95ed\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u6240\u6709\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u540e\uff0c\u5e94\u8be5\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5\u4ee5\u91ca\u653e\u8d44\u6e90\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\uff0c\u5982\u8fde\u63a5\u5931\u8d25\u3001SQL\u8bed\u6cd5\u9519\u8bef\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u5757\u6765\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    conn = mysql.connector.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>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&quot;SELECT * FROM employees&quot;)<\/p>\n<p>    result = cursor.fetchall()<\/p>\n<p>    for row in result:<\/p>\n<p>        print(row)<\/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>        cursor.close()<\/p>\n<p>        conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>mysql.connector.Error<\/code>\u7528\u4e8e\u6355\u83b7MySQL\u76f8\u5173\u7684\u5f02\u5e38\uff0c\u5e76\u5728\u51fa\u73b0\u9519\u8bef\u65f6\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5229\u7528SQLAlchemy\u5e93\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>SQLAlchemy\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684Python SQL\u5de5\u5177\u5305\u548c\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff08ORM\uff09\u5e93\u3002\u4f7f\u7528SQLAlchemy\u4e0d\u4ec5\u53ef\u4ee5\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93\uff0c\u8fd8\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u7ba1\u7406\u6570\u636e\u5e93\u6a21\u5f0f\u548c\u6267\u884c\u590d\u6742\u67e5\u8be2\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5SQLAlchemy\u548cPyMySQL<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528SQLAlchemy\u4e0eMySQL\u8fde\u63a5\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5b89\u88c5PyMySQL\u4f5c\u4e3a\u5e95\u5c42\u9a71\u52a8\u7a0b\u5e8f\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install sqlalchemy pymysql<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7SQLAlchemy\u7684<code>create_engine<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93\u8fde\u63a5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine<\/p>\n<h2><strong>\u521b\u5efa\u5f15\u64ce<\/strong><\/h2>\n<p>engine = create_engine(&#39;mysql+pymysql:\/\/yourusername:yourpassword@localhost\/yourdatabase&#39;)<\/p>\n<h2><strong>\u8fde\u63a5\u6d4b\u8bd5<\/strong><\/h2>\n<p>with engine.connect() as connection:<\/p>\n<p>    result = connection.execute(&quot;SELECT 1&quot;)<\/p>\n<p>    print(result.fetchone())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4ee3\u7801\u4e2d\uff0c<code>create_engine<\/code>\u51fd\u6570\u7684\u53c2\u6570\u662f\u4e00\u4e2a\u6570\u636e\u5e93URL\uff0c\u5176\u4e2d\u5305\u62ec\u7528\u6237\u540d\u3001\u5bc6\u7801\u3001\u4e3b\u673a\u5730\u5740\u548c\u6570\u636e\u5e93\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><h4>3. \u4f7f\u7528ORM\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>SQLAlchemy\u7684ORM\u5141\u8bb8\u5c06Python\u7c7b\u6620\u5c04\u5230\u6570\u636e\u5e93\u8868\uff0c\u4ece\u800c\u53ef\u4ee5\u4f7f\u7528Python\u5bf9\u8c61\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy.ext.declarative import declarative_base<\/p>\n<p>from sqlalchemy import Column, Integer, String<\/p>\n<p>Base = declarative_base()<\/p>\n<h2><strong>\u5b9a\u4e49\u6620\u5c04\u7c7b<\/strong><\/h2>\n<p>class Employee(Base):<\/p>\n<p>    __tablename__ = &#39;employees&#39;<\/p>\n<p>    id = Column(Integer, primary_key=True, autoincrement=True)<\/p>\n<p>    name = Column(String(255))<\/p>\n<p>    salary = Column(Integer)<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>Base.metadata.create_all(engine)<\/p>\n<h2><strong>\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>from sqlalchemy.orm import sessionmaker<\/p>\n<p>Session = sessionmaker(bind=engine)<\/p>\n<p>session = Session()<\/p>\n<p>new_employee = Employee(name=&#39;Jane Doe&#39;, salary=60000)<\/p>\n<p>session.add(new_employee)<\/p>\n<p>session.commit()<\/p>\n<h2><strong>\u67e5\u8be2\u6570\u636e<\/strong><\/h2>\n<p>employees = session.query(Employee).all()<\/p>\n<p>for employee in employees:<\/p>\n<p>    print(employee.name, employee.salary)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>declarative_base<\/code>\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u57fa\u7c7b\uff0c\u7528\u4e8e\u5b9a\u4e49\u6620\u5c04\u7c7b\u3002<code>Employee<\/code>\u7c7b\u8868\u793a\u6570\u636e\u5e93\u4e2d\u7684<code>employees<\/code>\u8868\u3002\u901a\u8fc7\u4f7f\u7528SQLAlchemy\u7684ORM\uff0c\u80fd\u591f\u66f4\u7b80\u6d01\u5730\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u901a\u8fc7PyMySQL\u5e93\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>PyMySQL\u662f\u4e00\u4e2a\u7eafPython\u5b9e\u73b0\u7684MySQL\u5ba2\u6237\u7aef\u5e93\uff0c\u652f\u6301Python 3\u3002\u5b83\u662fMySQLdb\u7684\u66ff\u4ee3\u54c1\uff0c\u9002\u7528\u4e8e\u90a3\u4e9b\u65e0\u6cd5\u5b89\u88c5MySQLdb\u7684\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5PyMySQL<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5PyMySQL\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pymysql<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528PyMySQL\u8fde\u63a5\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528PyMySQL\u8fde\u63a5MySQL\u6570\u636e\u5e93\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<h2><strong>\u521b\u5efa\u8fde\u63a5<\/strong><\/h2>\n<p>conn = 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<h2><strong>\u521b\u5efa\u6e38\u6807<\/strong><\/h2>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884c\u67e5\u8be2<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT * FROM employees&quot;)<\/p>\n<p>result = cursor.fetchall()<\/p>\n<p>for row in result:<\/p>\n<p>    print(row)<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>PyMySQL\u7684\u7528\u6cd5\u4e0eMySQL Connector\u975e\u5e38\u76f8\u4f3c\uff0c\u901a\u8fc7\u521b\u5efa\u8fde\u63a5\u3001\u83b7\u53d6\u6e38\u6807\u3001\u6267\u884cSQL\u8bed\u53e5\u3001\u83b7\u53d6\u7ed3\u679c\u548c\u5173\u95ed\u8fde\u63a5\u6765\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u4e0eMySQL\u7684\u8fde\u63a5\u65b9\u5f0f\u591a\u79cd\u591a\u6837\uff0c\u4e0d\u540c\u7684\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002<strong>MySQL Connector<\/strong>\u662f\u5b98\u65b9\u63a8\u8350\u7684\u5e93\uff0c\u529f\u80fd\u5168\u9762\u4e14\u6613\u4e8e\u4f7f\u7528\u3002<strong>SQLAlchemy<\/strong>\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u6570\u636e\u5e93\u62bd\u8c61\u548cORM\u529f\u80fd\uff0c\u9002\u5408\u9700\u8981\u590d\u6742\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u9879\u76ee\u3002<strong>PyMySQL<\/strong>\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u5e93\uff0c\u9002\u5408\u7b80\u5355\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u65b9\u6cd5\u53d6\u51b3\u4e8e\u9879\u76ee\u7684\u9700\u6c42\u548c\u5f00\u53d1\u8005\u7684\u504f\u597d\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5efa\u8bae\u6839\u636e\u6570\u636e\u5e93\u7684\u89c4\u6a21\u3001\u8bbf\u95ee\u9891\u7387\u3001\u590d\u6742\u6027\u548c\u9879\u76ee\u7684\u5177\u4f53\u9700\u6c42\u6765\u9009\u62e9\u5408\u9002\u7684\u5e93\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5MySQL\u8fde\u63a5\u5e93\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u8fde\u63a5MySQL\uff0c\u60a8\u9700\u8981\u5b89\u88c5\u4e00\u4e2aMySQL\u8fde\u63a5\u5e93\uff0c\u6700\u5e38\u7528\u7684\u662f<code>mysql-connector-python<\/code>\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u5b89\u88c5\uff1a  <\/p>\n<pre><code class=\"language-bash\">pip install mysql-connector-python\n<\/code><\/pre>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5c31\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8be5\u5e93\u5e76\u4f7f\u7528\u5b83\u8fdb\u884c\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n<p><strong>\u8fde\u63a5MySQL\u65f6\u9700\u8981\u63d0\u4f9b\u54ea\u4e9b\u4fe1\u606f\uff1f<\/strong><br \/>\u5728\u8fde\u63a5MySQL\u6570\u636e\u5e93\u65f6\uff0c\u60a8\u9700\u8981\u63d0\u4f9b\u4ee5\u4e0b\u4fe1\u606f\uff1a  <\/p>\n<ul>\n<li><strong>\u4e3b\u673a\u540d<\/strong>\uff08\u901a\u5e38\u662f<code>localhost<\/code>\u6216\u8fdc\u7a0b\u670d\u52a1\u5668\u7684IP\u5730\u5740\uff09  <\/li>\n<li><strong>\u7528\u6237\u540d<\/strong>\uff08\u7528\u4e8e\u767b\u5f55MySQL\u7684\u7528\u6237\uff09  <\/li>\n<li><strong>\u5bc6\u7801<\/strong>\uff08\u5bf9\u5e94\u4e8e\u7528\u6237\u540d\u7684\u5bc6\u7801\uff09  <\/li>\n<li><strong>\u6570\u636e\u5e93\u540d<\/strong>\uff08\u8981\u8fde\u63a5\u7684\u5177\u4f53\u6570\u636e\u5e93\u540d\u79f0\uff09<br \/>\u786e\u4fdd\u8fd9\u4e9b\u4fe1\u606f\u6b63\u786e\u65e0\u8bef\uff0c\u624d\u80fd\u6210\u529f\u5efa\u7acb\u8fde\u63a5\u3002<\/li>\n<\/ul>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u6267\u884cMySQL\u67e5\u8be2\uff1f<\/strong><br \/>\u5728\u6210\u529f\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\u6267\u884c\u67e5\u8be2\uff1a  <\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61\uff1a<code>cursor = connection.cursor()<\/code>  <\/li>\n<li>\u4f7f\u7528\u6e38\u6807\u5bf9\u8c61\u6267\u884cSQL\u8bed\u53e5\uff1a<code>cursor.execute(&quot;SELECT * FROM table_name&quot;)<\/code>  <\/li>\n<li>\u83b7\u53d6\u7ed3\u679c\uff1a\u4f7f\u7528<code>cursor.fetchall()<\/code>\u6216<code>cursor.fetchone()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c\u3002  <\/li>\n<li>\u6700\u540e\uff0c\u8bb0\u5f97\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5\uff1a<code>cursor.close()<\/code> \u548c <code>connection.close()<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u8d44\u6e90\u5f97\u5230\u91ca\u653e\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"Python\u4e0eMySQL\u7684\u8fde\u63a5\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528MySQL Connector\u3001\u5229\u7528SQLAlchemy\u5e93\u3001\u6216\u901a\u8fc7P [&hellip;]","protected":false},"author":3,"featured_media":1018660,"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\/1018657"}],"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=1018657"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1018657\/revisions"}],"predecessor-version":[{"id":1018662,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1018657\/revisions\/1018662"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1018660"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1018657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1018657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1018657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}