{"id":1074989,"date":"2025-01-08T11:39:46","date_gmt":"2025-01-08T03:39:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1074989.html"},"modified":"2025-01-08T11:39:49","modified_gmt":"2025-01-08T03:39:49","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%95%b0%e6%8d%ae-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1074989.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6\u6570\u636e\u5e93\u7684\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104510\/2335bdb9-2b14-4dee-b359-304c56af067f.webp\" alt=\"python\u5982\u4f55\u83b7\u53d6\u6570\u636e\u5e93\u7684\u6570\u636e\" \/><\/p>\n<p><p> <strong>Python \u83b7\u53d6\u6570\u636e\u5e93\u6570\u636e\u7684\u65b9\u6cd5<\/strong>\u5305\u62ec\uff1a\u4f7f\u7528SQL\u8bed\u53e5\u3001\u901a\u8fc7ORM\u6846\u67b6\u3001\u5229\u7528\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528SQL\u8bed\u53e5\uff0c\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5\u6570\u636e\u5e93\u9a71\u52a8\u3001\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\u3001\u6267\u884cSQL\u67e5\u8be2\u3001\u5904\u7406\u7ed3\u679c\u96c6\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u83b7\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528SQL\u8bed\u53e5\u83b7\u53d6\u6570\u636e<\/h2>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5\u6570\u636e\u5e93\u9a71\u52a8<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u8fde\u63a5\u6570\u636e\u5e93\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u76f8\u5e94\u7684\u6570\u636e\u5e93\u9a71\u52a8\u3002\u4e0d\u540c\u7684\u6570\u636e\u5e93\u6709\u4e0d\u540c\u7684\u9a71\u52a8\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li>MySQL: <code>mysql-connector-python<\/code> \u6216 <code>PyMySQL<\/code><\/li>\n<li>PostgreSQL: <code>psycopg2<\/code><\/li>\n<li>SQLite: \u5185\u7f6e\u652f\u6301\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5<\/li>\n<\/ul>\n<p><p>\u5b89\u88c5\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install mysql-connector-python<\/p>\n<p>pip install psycopg2<\/p>\n<p>pip install pymysql<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\u662f\u83b7\u53d6\u6570\u636e\u7684\u7b2c\u4e00\u6b65\u3002\u4ee5\u4e0b\u662f\u8fde\u63a5\u4e0d\u540c\u6570\u636e\u5e93\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>MySQL<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/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><\/code><\/pre>\n<\/p>\n<p><h4>PostgreSQL<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import psycopg2<\/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><\/code><\/pre>\n<\/p>\n<p><h4>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><h3>3\u3001\u6267\u884cSQL\u67e5\u8be2<\/h3>\n<\/p>\n<p><p>\u8fde\u63a5\u6210\u529f\u540e\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u5b83\u6765\u6267\u884cSQL\u67e5\u8be2\u8bed\u53e5\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor = conn.cursor()<\/p>\n<p>cursor.execute(&quot;SELECT * FROM your_table&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u5904\u7406\u7ed3\u679c\u96c6<\/h3>\n<\/p>\n<p><p>\u6267\u884c\u67e5\u8be2\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6e38\u6807\u5bf9\u8c61\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c\uff0c\u5e76\u8fdb\u884c\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">results = cursor.fetchall()<\/p>\n<p>for row in results:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5\u3001\u5173\u95ed\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u5e94\u5173\u95ed\u6e38\u6807\u548c\u6570\u636e\u5e93\u8fde\u63a5\uff0c\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><h2>\u4e8c\u3001\u4f7f\u7528ORM\u6846\u67b6\u83b7\u53d6\u6570\u636e<\/h2>\n<\/p>\n<p><p>ORM\uff08Object-Relational Mapping\uff09\u6846\u67b6\u53ef\u4ee5\u7b80\u5316\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u4e8e\u7ef4\u62a4\u3002\u5e38\u7528\u7684Python ORM\u6846\u67b6\u5305\u62ecSQLAlchemy\u548cDjango ORM\u3002<\/p>\n<\/p>\n<p><h3>1\u3001SQLAlchemy<\/h3>\n<\/p>\n<p><h4>\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>\u4f7f\u7528SQLAlchemy\u8fde\u63a5\u6570\u636e\u5e93\u5e76\u83b7\u53d6\u6570\u636e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine, Table, MetaData<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u5e93\u5f15\u64ce<\/strong><\/h2>\n<p>engine = create_engine(&#39;mysql+mysqlconnector:\/\/yourusername:yourpassword@localhost\/yourdatabase&#39;)<\/p>\n<h2><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = engine.connect()<\/p>\n<h2><strong>\u53cd\u5c04\u6570\u636e\u5e93\u8868<\/strong><\/h2>\n<p>metadata = MetaData()<\/p>\n<p>your_table = Table(&#39;your_table&#39;, metadata, autoload_with=engine)<\/p>\n<h2><strong>\u67e5\u8be2\u6570\u636e<\/strong><\/h2>\n<p>query = your_table.select()<\/p>\n<p>results = conn.execute(query).fetchall()<\/p>\n<h2><strong>\u5904\u7406\u7ed3\u679c<\/strong><\/h2>\n<p>for row in results:<\/p>\n<p>    print(row)<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001Django ORM<\/h3>\n<\/p>\n<p><h4>\u5b89\u88c5Django<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install django<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efaDjango\u9879\u76ee\u5e76\u914d\u7f6e\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">django-admin startproject myproject<\/p>\n<p>cd myproject<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728 <code>myproject\/settings.py<\/code> \u6587\u4ef6\u4e2d\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">DATABASES = {<\/p>\n<p>    &#39;default&#39;: {<\/p>\n<p>        &#39;ENGINE&#39;: &#39;django.db.backends.mysql&#39;,<\/p>\n<p>        &#39;NAME&#39;: &#39;yourdatabase&#39;,<\/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;PORT&#39;: &#39;3306&#39;,<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efaDjango\u6a21\u578b\u5e76\u83b7\u53d6\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728 <code>myproject\/models.py<\/code> \u6587\u4ef6\u4e2d\u5b9a\u4e49\u6a21\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.db import models<\/p>\n<p>class YourModel(models.Model):<\/p>\n<p>    field1 = models.CharField(max_length=100)<\/p>\n<p>    field2 = models.IntegerField()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728 <code>myproject\/views.py<\/code> \u6587\u4ef6\u4e2d\u83b7\u53d6\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from .models import YourModel<\/p>\n<p>def get_data():<\/p>\n<p>    results = YourModel.objects.all()<\/p>\n<p>    for row in results:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u5229\u7528\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u83b7\u53d6\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u76f4\u63a5\u4f7f\u7528SQL\u8bed\u53e5\u548cORM\u6846\u67b6\u5916\uff0c\u8fd8\u53ef\u4ee5\u5229\u7528\u4e00\u4e9b\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u6765\u7b80\u5316\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528pandas\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u5e76\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5pandas\u548c\u6570\u636e\u5e93\u9a71\u52a8<\/h3>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p>pip install mysql-connector-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528pandas\u8bfb\u53d6\u6570\u636e<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import mysql.connector<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\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<h2><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>query = &quot;SELECT * FROM your_table&quot;<\/p>\n<p>df = pd.read_sql(query, conn)<\/p>\n<h2><strong>\u5904\u7406\u6570\u636e<\/strong><\/h2>\n<p>print(df)<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u4f7f\u7528Python\u4ece\u5404\u79cd\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u636e\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\uff0c\u8fd8\u80fd\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u3002\u65e0\u8bba\u662f\u76f4\u63a5\u4f7f\u7528SQL\u8bed\u53e5\u3001\u901a\u8fc7ORM\u6846\u67b6\uff0c\u8fd8\u662f\u5229\u7528\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff0c\u90fd\u80fd\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u6570\u636e\u83b7\u53d6\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u9519\u8bef\u5904\u7406\u548c\u4f18\u5316<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u5e93\u64cd\u4f5c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\u548c\u6027\u80fd\u95ee\u9898\uff0c\u9700\u8981\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u8fde\u63a5\u5931\u8d25\u3001SQL\u8bed\u6cd5\u9519\u8bef\u3001\u6570\u636e\u7c7b\u578b\u4e0d\u5339\u914d\u7b49\u95ee\u9898\u3002\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u6765\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/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;SELECT * FROM your_table&quot;)<\/p>\n<p>    results = cursor.fetchall()<\/p>\n<p>    for row in results:<\/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><h3>2\u3001\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6570\u636e\u91cf\u65f6\uff0c\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u6027\u80fd\u53ef\u80fd\u6210\u4e3a\u74f6\u9888\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u8fdb\u884c\u4f18\u5316\uff1a<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u7d22\u5f15<\/h4>\n<\/p>\n<p><p>\u4e3a\u67e5\u8be2\u9891\u7e41\u7684\u5b57\u6bb5\u6dfb\u52a0\u7d22\u5f15\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-sql\">CREATE INDEX idx_field1 ON your_table(field1);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5206\u9875\u67e5\u8be2<\/h4>\n<\/p>\n<p><p>\u5728\u67e5\u8be2\u5927\u6570\u636e\u91cf\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5206\u9875\u67e5\u8be2\uff0c\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u8fc7\u591a\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.execute(&quot;SELECT * FROM your_table LIMIT 100 OFFSET 0&quot;)<\/p>\n<p>results = cursor.fetchall()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6279\u91cf\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u63d2\u5165\u6216\u66f4\u65b0\u5927\u91cf\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6279\u91cf\u5904\u7406\uff0c\u51cf\u5c11\u6570\u636e\u5e93\u64cd\u4f5c\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [<\/p>\n<p>    (&#39;value1&#39;, 1),<\/p>\n<p>    (&#39;value2&#39;, 2),<\/p>\n<p>    # ...<\/p>\n<p>]<\/p>\n<p>cursor.executemany(&quot;INSERT INTO your_table (field1, field2) VALUES (%s, %s)&quot;, data)<\/p>\n<p>conn.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u83b7\u53d6\u6570\u636e\u5e93\u6570\u636e\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528SQL\u8bed\u53e5\u3001\u901a\u8fc7ORM\u6846\u67b6\u3001\u5229\u7528\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u6b64\u5916\uff0c\u8fd8\u4ecb\u7ecd\u4e86\u9519\u8bef\u5904\u7406\u548c\u6027\u80fd\u4f18\u5316\u7684\u76f8\u5173\u6280\u5de7\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u9ad8\u6548\u6027\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u5728Python\u5f00\u53d1\u4e2d\u8f7b\u677e\u5e94\u5bf9\u5404\u79cd\u6570\u636e\u5e93\u64cd\u4f5c\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff1f<\/strong><br \/>\u8981\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff0c\u60a8\u9700\u8981\u4f7f\u7528\u9002\u5f53\u7684\u6570\u636e\u5e93\u9a71\u52a8\u7a0b\u5e8f\u3002\u5e38\u7528\u7684\u6709SQLite\u3001MySQL\u548cPostgreSQL\u7b49\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>sqlite3<\/code>\u6a21\u5757\u8fde\u63a5SQLite\u6570\u636e\u5e93\uff0c\u6216\u8005\u4f7f\u7528<code>mysql-connector-python<\/code>\u548c<code>psycopg2<\/code>\u6a21\u5757\u5206\u522b\u8fde\u63a5MySQL\u548cPostgreSQL\u3002\u8fde\u63a5\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u6e38\u6807\u5bf9\u8c61\u6765\u6267\u884cSQL\u67e5\u8be2\u5e76\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u6267\u884cSQL\u67e5\u8be2\u5e76\u83b7\u53d6\u7ed3\u679c\uff1f<\/strong><br \/>\u6267\u884cSQL\u67e5\u8be2\u65f6\uff0c\u60a8\u9700\u8981\u4f7f\u7528\u6e38\u6807\u5bf9\u8c61\u7684<code>execute<\/code>\u65b9\u6cd5\u6765\u8fd0\u884cSQL\u8bed\u53e5\u3002\u67e5\u8be2\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>fetchall()<\/code>\u6216<code>fetchone()<\/code>\u65b9\u6cd5\u83b7\u53d6\u7ed3\u679c\u96c6\u3002<code>fetchall()<\/code>\u4f1a\u8fd4\u56de\u6240\u6709\u7ed3\u679c\uff0c\u800c<code>fetchone()<\/code>\u5219\u8fd4\u56de\u5355\u4e2a\u7ed3\u679c\u3002\u786e\u4fdd\u5728\u6267\u884c\u67e5\u8be2\u540e\u4f7f\u7528<code>commit()<\/code>\u65b9\u6cd5\u63d0\u4ea4\u66f4\u6539\uff08\u5bf9\u4e8eINSERT\u3001UPDATE\u3001DELETE\u7b49\u64cd\u4f5c\uff09\u5e76\u5728\u5b8c\u6210\u540e\u5173\u95ed\u8fde\u63a5\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u83b7\u53d6\u6570\u636e\u5e93\u6570\u636e\u65f6\u5982\u4f55\u5904\u7406\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u5904\u7406\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0c\u4f7f\u7528<code>try-except<\/code>\u5757\u662f\u4e2a\u597d\u4e60\u60ef\uff0c\u4ee5\u6355\u83b7\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u3002\u4f8b\u5982\uff0c\u8fde\u63a5\u5931\u8d25\u3001\u67e5\u8be2\u8bed\u6cd5\u9519\u8bef\u6216\u6570\u636e\u672a\u627e\u5230\u7b49\u60c5\u51b5\u3002\u901a\u8fc7\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\uff0c\u60a8\u53ef\u4ee5\u8f93\u51fa\u53cb\u597d\u7684\u9519\u8bef\u4fe1\u606f\u6216\u8fdb\u884c\u5176\u4ed6\u5904\u7406\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u83b7\u53d6\u6570\u636e\u5e93\u6570\u636e\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528SQL\u8bed\u53e5\u3001\u901a\u8fc7ORM\u6846\u67b6\u3001\u5229\u7528\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528S [&hellip;]","protected":false},"author":3,"featured_media":1074998,"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\/1074989"}],"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=1074989"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074989\/revisions"}],"predecessor-version":[{"id":1075004,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074989\/revisions\/1075004"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1074998"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1074989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1074989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1074989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}