{"id":1074056,"date":"2025-01-08T11:31:12","date_gmt":"2025-01-08T03:31:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1074056.html"},"modified":"2025-01-08T11:31:14","modified_gmt":"2025-01-08T03:31:14","slug":"%e5%a6%82%e4%bd%95%e5%ae%89%e8%a3%85python%e7%9a%84%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bf%9e%e6%8e%a5-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1074056.html","title":{"rendered":"\u5982\u4f55\u5b89\u88c5python\u7684\u6570\u636e\u5e93\u8fde\u63a5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103805\/ef2b6f4b-7c21-4fc1-8ed7-e07b028bc200.webp\" alt=\"\u5982\u4f55\u5b89\u88c5python\u7684\u6570\u636e\u5e93\u8fde\u63a5\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u5b89\u88c5Python\u7684\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/p>\n<\/p>\n<p><p>\u5b89\u88c5Python\u7684\u6570\u636e\u5e93\u8fde\u63a5\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u6765\u8fdb\u884c\uff1a<strong>\u9009\u62e9\u9002\u5408\u7684\u6570\u636e\u5e93\u9a71\u52a8\u3001\u4f7f\u7528Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u8fdb\u884c\u5b89\u88c5\u3001\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u53c2\u6570\u3001\u7f16\u5199\u4ee3\u7801\u6d4b\u8bd5\u8fde\u63a5<\/strong>\u3002\u5176\u4e2d\uff0c\u9009\u62e9\u9002\u5408\u7684\u6570\u636e\u5e93\u9a71\u52a8\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u4e0d\u540c\u7684\u6570\u636e\u5e93\u9700\u8981\u4e0d\u540c\u7684\u9a71\u52a8\u7a0b\u5e8f\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b89\u88c5\u548c\u914d\u7f6ePython\u7684\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u4ee5\u786e\u4fdd\u4f60\u80fd\u591f\u987a\u5229\u4e0e\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u9009\u62e9\u9002\u5408\u7684\u6570\u636e\u5e93\u9a71\u52a8<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u9002\u5408\u7684\u6570\u636e\u5e93\u9a71\u52a8\u662f\u5b89\u88c5Python\u6570\u636e\u5e93\u8fde\u63a5\u7684\u7b2c\u4e00\u6b65\u3002Python\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\uff0c\u5305\u62ecMySQL\u3001PostgreSQL\u3001SQLite\u3001Microsoft SQL Server\u7b49\uff0c\u6bcf\u79cd\u6570\u636e\u5e93\u90fd\u6709\u5176\u4e13\u95e8\u7684\u9a71\u52a8\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>1. MySQL<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4f7f\u7528MySQL\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u9009\u62e9<code>mysql-connector-python<\/code>\u6216<code>PyMySQL<\/code>\u4f5c\u4e3a\u9a71\u52a8\u7a0b\u5e8f\u3002<code>mysql-connector-python<\/code>\u662fMySQL\u5b98\u65b9\u63d0\u4f9b\u7684\u9a71\u52a8\u7a0b\u5e8f\uff0c\u529f\u80fd\u5f3a\u5927\u4e14\u7a33\u5b9a\u3002<\/p>\n<\/p>\n<p><h4>2. PostgreSQL<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8ePostgreSQL\u6570\u636e\u5e93\uff0c<code>psycopg2<\/code>\u662f\u6700\u5e38\u7528\u7684\u9a71\u52a8\u7a0b\u5e8f\u3002\u5b83\u662f\u4e00\u4e2a\u6210\u719f\u4e14\u9ad8\u6548\u7684PostgreSQL\u9002\u914d\u5668\uff0c\u80fd\u591f\u5f88\u597d\u5730\u652f\u6301Python\u7684\u591a\u7ebf\u7a0b\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h4>3. SQLite<\/h4>\n<\/p>\n<p><p>SQLite\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u5e93\uff0cPython\u5185\u7f6e\u4e86\u5bf9SQLite\u7684\u652f\u6301\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\u9a71\u52a8\u7a0b\u5e8f\u3002\u4f60\u53ea\u9700\u5bfc\u5165<code>sqlite3<\/code>\u6a21\u5757\u5373\u53ef\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>4. Microsoft SQL Server<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4f7f\u7528Microsoft SQL Server\uff0c\u53ef\u4ee5\u9009\u62e9<code>pyodbc<\/code>\u6216<code>pymssql<\/code>\u3002<code>pyodbc<\/code>\u662f\u4e00\u4e2a\u901a\u7528\u7684ODBC\u63a5\u53e3\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\uff0c\u800c<code>pymssql<\/code>\u662f\u4e13\u95e8\u4e3aSQL Server\u8bbe\u8ba1\u7684\u9a71\u52a8\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528pip\u5b89\u88c5\u9a71\u52a8\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u5b89\u88c5\u6570\u636e\u5e93\u9a71\u52a8\u7a0b\u5e8f\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u662f\u4f7f\u7528Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u3002\u4e0b\u9762\u662f\u5b89\u88c5\u5e38\u7528\u6570\u636e\u5e93\u9a71\u52a8\u7a0b\u5e8f\u7684\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5MySQL\u9a71\u52a8\u7a0b\u5e8f<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install mysql-connector-python<\/p>\n<h2><strong>\u6216\u8005<\/strong><\/h2>\n<p>pip install PyMySQL<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5b89\u88c5PostgreSQL\u9a71\u52a8\u7a0b\u5e8f<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install psycopg2<\/p>\n<h2><strong>\u6216\u8005<\/strong><\/h2>\n<p>pip install psycopg2-binary<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5b89\u88c5SQLite\u9a71\u52a8\u7a0b\u5e8f<\/h4>\n<\/p>\n<p><p>SQLite\u9a71\u52a8\u7a0b\u5e8f\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u5355\u72ec\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h4>4. \u5b89\u88c5Microsoft SQL Server\u9a71\u52a8\u7a0b\u5e8f<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyodbc<\/p>\n<h2><strong>\u6216\u8005<\/strong><\/h2>\n<p>pip install pymssql<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u5b89\u88c5\u9a71\u52a8\u7a0b\u5e8f\u540e\uff0c\u63a5\u4e0b\u6765\u9700\u8981\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u53c2\u6570\u3002\u901a\u5e38\u5305\u62ec\u6570\u636e\u5e93\u4e3b\u673a\u5730\u5740\u3001\u7aef\u53e3\u53f7\u3001\u6570\u636e\u5e93\u540d\u3001\u7528\u6237\u540d\u548c\u5bc6\u7801\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. MySQL<\/h4>\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;127.0.0.1&#39;,<\/p>\n<p>    &#39;database&#39;: &#39;yourdatabase&#39;<\/p>\n<p>}<\/p>\n<p>conn = mysql.connector.connect(config)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. PostgreSQL<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import psycopg2<\/p>\n<p>conn = psycopg2.connect(<\/p>\n<p>    dbname=&#39;yourdatabase&#39;,<\/p>\n<p>    user=&#39;yourusername&#39;,<\/p>\n<p>    password=&#39;yourpassword&#39;,<\/p>\n<p>    host=&#39;127.0.0.1&#39;,<\/p>\n<p>    port=&#39;5432&#39;<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. SQLite<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>conn = sqlite3.connect(&#39;yourdatabase.db&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. Microsoft SQL Server<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pyodbc<\/p>\n<p>conn = pyodbc.connect(<\/p>\n<p>    &#39;DRIVER={ODBC Driver 17 for SQL Server};&#39;<\/p>\n<p>    &#39;SERVER=yourserver;&#39;<\/p>\n<p>    &#39;DATABASE=yourdatabase;&#39;<\/p>\n<p>    &#39;UID=yourusername;&#39;<\/p>\n<p>    &#39;PWD=yourpassword&#39;<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7f16\u5199\u4ee3\u7801\u6d4b\u8bd5\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>\u914d\u7f6e\u597d\u6570\u636e\u5e93\u8fde\u63a5\u53c2\u6570\u540e\uff0c\u53ef\u4ee5\u7f16\u5199\u4ee3\u7801\u6d4b\u8bd5\u8fde\u63a5\u662f\u5426\u6210\u529f\u3002<\/p>\n<\/p>\n<p><h4>1. MySQL<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">cursor = conn.cursor()<\/p>\n<p>cursor.execute(&#39;SELECT DATABASE()&#39;)<\/p>\n<p>result = cursor.fetchone()<\/p>\n<p>print(&#39;Connected to database:&#39;, result)<\/p>\n<p>cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. PostgreSQL<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">cursor = conn.cursor()<\/p>\n<p>cursor.execute(&#39;SELECT current_database()&#39;)<\/p>\n<p>result = cursor.fetchone()<\/p>\n<p>print(&#39;Connected to database:&#39;, result)<\/p>\n<p>cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. SQLite<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">cursor = conn.cursor()<\/p>\n<p>cursor.execute(&#39;SELECT sqlite_version()&#39;)<\/p>\n<p>result = cursor.fetchone()<\/p>\n<p>print(&#39;SQLite version:&#39;, result)<\/p>\n<p>cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. Microsoft SQL Server<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">cursor = conn.cursor()<\/p>\n<p>cursor.execute(&#39;SELECT @@VERSION&#39;)<\/p>\n<p>result = cursor.fetchone()<\/p>\n<p>print(&#39;SQL Server version:&#39;, result)<\/p>\n<p>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>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6570\u636e\u5e93\u8fde\u63a5\u53ef\u80fd\u4f1a\u51fa\u73b0\u5f02\u5e38\u60c5\u51b5\uff0c\u4f8b\u5982\u7f51\u7edc\u6545\u969c\u3001\u6570\u636e\u5e93\u670d\u52a1\u4e0d\u53ef\u7528\u7b49\u3002\u56e0\u6b64\uff0c\u5728\u7f16\u5199\u6570\u636e\u5e93\u8fde\u63a5\u4ee3\u7801\u65f6\uff0c\u5efa\u8bae\u6dfb\u52a0\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    conn = mysql.connector.connect(config)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&#39;SELECT DATABASE()&#39;)<\/p>\n<p>    result = cursor.fetchone()<\/p>\n<p>    print(&#39;Connected to database:&#39;, result)<\/p>\n<p>except mysql.connector.Error as err:<\/p>\n<p>    print(&#39;Error:&#39;, err)<\/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><h3>\u516d\u3001\u4f7f\u7528ORM\u6846\u67b6<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u7b80\u5316\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u6846\u67b6\uff0c\u4f8b\u5982SQLAlchemy\u3001Django ORM\u7b49\u3002ORM\u6846\u67b6\u80fd\u591f\u5c06\u6570\u636e\u5e93\u8868\u6620\u5c04\u4e3aPython\u5bf9\u8c61\uff0c\u4f7f\u5f97\u6570\u636e\u5e93\u64cd\u4f5c\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5SQLAlchemy<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install SQLAlchemy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u914d\u7f6eSQLAlchemy<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine<\/p>\n<p>from sqlalchemy.ext.declarative import declarative_base<\/p>\n<p>from sqlalchemy.orm import sessionmaker<\/p>\n<p>DATABASE_URL = &#39;mysql+mysqlconnector:\/\/yourusername:yourpassword@127.0.0.1\/yourdatabase&#39;<\/p>\n<p>engine = create_engine(DATABASE_URL)<\/p>\n<p>SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)<\/p>\n<p>Base = declarative_base()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5b9a\u4e49\u6a21\u578b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import Column, Integer, String<\/p>\n<p>class User(Base):<\/p>\n<p>    __tablename__ = &#39;users&#39;<\/p>\n<p>    id = Column(Integer, primary_key=True, index=True)<\/p>\n<p>    name = Column(String, index=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u521b\u5efa\u6570\u636e\u5e93\u8868<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">Base.metadata.create_all(bind=engine)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u64cd\u4f5c\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">db = SessionLocal()<\/p>\n<p>new_user = User(name=&#39;John Doe&#39;)<\/p>\n<p>db.add(new_user)<\/p>\n<p>db.commit()<\/p>\n<p>db.refresh(new_user)<\/p>\n<p>print(&#39;Added user:&#39;, new_user.name)<\/p>\n<p>db.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f18\u5316\u6570\u636e\u5e93\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\uff0c\u4f18\u5316\u6570\u636e\u5e93\u8fde\u63a5\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u6765\u63d0\u9ad8\u8fde\u63a5\u6548\u7387\uff0c\u51cf\u5c11\u8fde\u63a5\u521b\u5efa\u548c\u9500\u6bc1\u7684\u5f00\u9500\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528SQLAlchemy\u8fde\u63a5\u6c60<\/h4>\n<\/p>\n<p><p>SQLAlchemy\u5185\u7f6e\u4e86\u8fde\u63a5\u6c60\u529f\u80fd\uff0c\u914d\u7f6e\u8fde\u63a5\u6c60\u53c2\u6570\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u5e93\u8fde\u63a5\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">DATABASE_URL = &#39;mysql+mysqlconnector:\/\/yourusername:yourpassword@127.0.0.1\/yourdatabase&#39;<\/p>\n<p>engine = create_engine(<\/p>\n<p>    DATABASE_URL,<\/p>\n<p>    pool_size=10,<\/p>\n<p>    max_overflow=20,<\/p>\n<p>    pool_timeout=30,<\/p>\n<p>    pool_recycle=3600<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u7b2c\u4e09\u65b9\u8fde\u63a5\u6c60\u5e93<\/h4>\n<\/p>\n<p><p>\u9664\u4e86SQLAlchemy\u7684\u5185\u7f6e\u8fde\u63a5\u6c60\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u8fde\u63a5\u6c60\u5e93\uff0c\u4f8b\u5982DBUtils\u3002DBUtils\u662f\u4e00\u4e2a\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u7ba1\u7406\u5de5\u5177\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\u9a71\u52a8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install DBUtils<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">from DBUtils.PooledDB import PooledDB<\/p>\n<p>import mysql.connector<\/p>\n<p>pool = PooledDB(<\/p>\n<p>    creator=mysql.connector,<\/p>\n<p>    maxconnections=10,<\/p>\n<p>    mincached=2,<\/p>\n<p>    maxcached=5,<\/p>\n<p>    maxshared=3,<\/p>\n<p>    blocking=True,<\/p>\n<p>    setsession=[],<\/p>\n<p>    ping=0,<\/p>\n<p>    user=&#39;yourusername&#39;,<\/p>\n<p>    password=&#39;yourpassword&#39;,<\/p>\n<p>    host=&#39;127.0.0.1&#39;,<\/p>\n<p>    database=&#39;yourdatabase&#39;<\/p>\n<p>)<\/p>\n<p>conn = pool.connection()<\/p>\n<p>cursor = conn.cursor()<\/p>\n<p>cursor.execute(&#39;SELECT DATABASE()&#39;)<\/p>\n<p>result = cursor.fetchone()<\/p>\n<p>print(&#39;Connected to database:&#39;, result)<\/p>\n<p>cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5b89\u88c5Python\u7684\u6570\u636e\u5e93\u8fde\u63a5\u53ef\u4ee5\u901a\u8fc7\u9009\u62e9\u9002\u5408\u7684\u6570\u636e\u5e93\u9a71\u52a8\u3001\u4f7f\u7528pip\u5b89\u88c5\u9a71\u52a8\u7a0b\u5e8f\u3001\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u53c2\u6570\u3001\u7f16\u5199\u4ee3\u7801\u6d4b\u8bd5\u8fde\u63a5\u7b49\u6b65\u9aa4\u8fdb\u884c\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u8fd8\u9700\u8981\u5904\u7406\u5f02\u5e38\u60c5\u51b5\uff0c\u5e76\u53ef\u4ee5\u4f7f\u7528ORM\u6846\u67b6\u548c\u8fde\u63a5\u6c60\u4f18\u5316\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u4f60\u987a\u5229\u5b89\u88c5\u548c\u914d\u7f6ePython\u7684\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff1f<\/strong><br \/>\u5728\u5b89\u88c5Python\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u4e4b\u524d\uff0c\u4e86\u89e3\u4e0d\u540c\u6570\u636e\u5e93\u7c7b\u578b\uff08\u5982MySQL\u3001PostgreSQL\u3001SQLite\u7b49\uff09\u6240\u9700\u7684\u8fde\u63a5\u5e93\u975e\u5e38\u91cd\u8981\u3002\u5e38\u89c1\u7684\u5e93\u5305\u62ec<code>mysql-connector-python<\/code>\u3001<code>psycopg2<\/code>\u548c<code>sqlite3<\/code>\u3002\u6839\u636e\u4f60\u7684\u9879\u76ee\u9700\u6c42\u548c\u6570\u636e\u5e93\u7c7b\u578b\u9009\u62e9\u6700\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<p><strong>\u5b89\u88c5\u8fd9\u4e9b\u5e93\u9700\u8981\u54ea\u4e9b\u524d\u7f6e\u6761\u4ef6\uff1f<\/strong><br \/>\u5728\u5b89\u88c5\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u4e4b\u524d\uff0c\u786e\u4fdd\u5df2\u5728\u7cfb\u7edf\u4e2d\u5b89\u88c5Python\u548cpip\u3002\u5bf9\u4e8e\u67d0\u4e9b\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff0c\u53ef\u80fd\u8fd8\u9700\u8981\u5b89\u88c5\u6570\u636e\u5e93\u672c\u8eab\u6216\u76f8\u5173\u7684\u9a71\u52a8\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528MySQL\u65f6\uff0c\u4f60\u9700\u8981\u786e\u4fddMySQL\u670d\u52a1\u5668\u5df2\u7ecf\u5b89\u88c5\u5e76\u6b63\u5e38\u8fd0\u884c\u3002\u6b64\u5916\uff0c\u5efa\u8bae\u4f7f\u7528\u865a\u62df\u73af\u5883\uff08\u5982venv\u6216conda\uff09\u6765\u9694\u79bb\u9879\u76ee\u4f9d\u8d56\uff0c\u907f\u514d\u7248\u672c\u51b2\u7a81\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4ee3\u7801\u4e2d\u4f7f\u7528\u5df2\u5b89\u88c5\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff1f<\/strong><br \/>\u5b89\u88c5\u5b8c\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165\u5e93\u6765\u5728Python\u4ee3\u7801\u4e2d\u4f7f\u7528\u5b83\u4eec\u3002\u4ee5<code>mysql-connector-python<\/code>\u4e3a\u4f8b\uff0c\u8fde\u63a5\u5230\u6570\u636e\u5e93\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\u5bfc\u5165\u5e93\u3001\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\u3001\u6267\u884cSQL\u8bed\u53e5\u4ee5\u53ca\u5173\u95ed\u8fde\u63a5\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import mysql.connector\n\n# \u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\nconnection = 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\n# \u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61\ncursor = connection.cursor()\n\n# \u6267\u884c\u67e5\u8be2\ncursor.execute(&quot;SELECT * FROM your_table&quot;)\n\n# \u83b7\u53d6\u7ed3\u679c\nresults = cursor.fetchall()\nfor row in results:\n    print(row)\n\n# \u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5\ncursor.close()\nconnection.close()\n<\/code><\/pre>\n<p>\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u8f7b\u677e\u5730\u4e0e\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\uff0c\u8fdb\u884c\u6570\u636e\u7684\u589e\u5220\u6539\u67e5\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u5b89\u88c5Python\u7684\u6570\u636e\u5e93\u8fde\u63a5 \u5b89\u88c5Python\u7684\u6570\u636e\u5e93\u8fde\u63a5\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u6765\u8fdb\u884c\uff1a\u9009\u62e9\u9002\u5408\u7684\u6570\u636e\u5e93\u9a71\u52a8\u3001\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1074064,"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\/1074056"}],"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=1074056"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074056\/revisions"}],"predecessor-version":[{"id":1074065,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074056\/revisions\/1074065"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1074064"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1074056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1074056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1074056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}