{"id":1054103,"date":"2024-12-31T14:39:31","date_gmt":"2024-12-31T06:39:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1054103.html"},"modified":"2024-12-31T14:39:33","modified_gmt":"2024-12-31T06:39:33","slug":"python%e5%a6%82%e4%bd%95%e6%93%8d%e4%bd%9c%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1054103.html","title":{"rendered":"python\u5982\u4f55\u64cd\u4f5c\u6570\u636e\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/54056658-1883-418f-a117-d97d498762fb.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u64cd\u4f5c\u6570\u636e\u5e93\" \/><\/p>\n<p><p> <strong>Python\u64cd\u4f5c\u6570\u636e\u5e93\u7684\u6838\u5fc3\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff08\u5982SQLite3\u3001MySQL Connector\u3001Psycopg2\u3001SQLAlchemy\u7b49\uff09\u3001\u8fde\u63a5\u6570\u636e\u5e93\u3001\u6267\u884cSQL\u8bed\u53e5\u3001\u5904\u7406\u67e5\u8be2\u7ed3\u679c\u3001\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/strong> \u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u64cd\u4f5c\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u7684\u9009\u62e9<\/p>\n<\/p>\n<p><p>Python\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u6709\u5f88\u591a\uff0c\u5e38\u89c1\u7684\u5305\u62ecSQLite3\u3001MySQL Connector\u3001Psycopg2\u548cSQLAlchemy\u3002SQLite3\u662fPython\u5185\u7f6e\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff0c\u9002\u5408\u5c0f\u578b\u5e94\u7528\uff1bMySQL Connector\u9002\u7528\u4e8e\u8fde\u63a5MySQL\u6570\u636e\u5e93\uff1bPsycopg2\u662fPostgreSQL\u6570\u636e\u5e93\u7684\u8fde\u63a5\u5e93\uff1bSQLAlchemy\u5219\u662f\u4e00\u4e2aORM\u5e93\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\u3002\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u662f\u64cd\u4f5c\u6570\u636e\u5e93\u7684\u7b2c\u4e00\u6b65\u3002<\/p>\n<\/p>\n<p><p><strong>1. SQLite3<\/strong><\/p>\n<\/p>\n<p><p>SQLite3\u662fPython\u5185\u7f6e\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff0c\u4e0d\u9700\u8981\u5b89\u88c5\u989d\u5916\u7684\u6a21\u5757\u3002\u5b83\u9002\u7528\u4e8e\u5c0f\u578b\u5e94\u7528\uff0c\u6570\u636e\u5b58\u50a8\u5728\u4e00\u4e2a\u5355\u4e00\u6587\u4ef6\u4e2d\uff0c\u65b9\u4fbf\u643a\u5e26\u548c\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5SQLite\u6570\u636e\u5e93\uff0c\u5982\u679c\u6570\u636e\u5e93\u4e0d\u5b58\u5728\u5219\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884cSQL\u8bed\u53e5<\/strong><\/h2>\n<p>cursor.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS users<\/p>\n<p>                 (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)&#39;&#39;&#39;)<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&quot;INSERT INTO users (name, age) VALUES (&#39;Alice&#39;, 30)&quot;)<\/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 users&quot;)<\/p>\n<p>print(cursor.fetchall())<\/p>\n<h2><strong>\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/strong><\/h2>\n<p>cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. MySQL Connector<\/strong><\/p>\n<\/p>\n<p><p>MySQL Connector\u662fPython\u8fde\u63a5MySQL\u6570\u636e\u5e93\u7684\u6807\u51c6\u5e93\uff0c\u4f7f\u7528\u524d\u9700\u8981\u5b89\u88c5mysql-connector-python\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<h2><strong>\u8fde\u63a5MySQL\u6570\u636e\u5e93<\/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>\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884cSQL\u8bed\u53e5<\/strong><\/h2>\n<p>cursor.execute(&quot;CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)&quot;)<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&quot;INSERT INTO users (name, age) VALUES (&#39;Alice&#39;, 30)&quot;)<\/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 users&quot;)<\/p>\n<p>print(cursor.fetchall())<\/p>\n<h2><strong>\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/strong><\/h2>\n<p>cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. Psycopg2<\/strong><\/p>\n<\/p>\n<p><p>Psycopg2\u662fPython\u8fde\u63a5PostgreSQL\u6570\u636e\u5e93\u7684\u5e93\uff0c\u4f7f\u7528\u524d\u9700\u8981\u5b89\u88c5psycopg2\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import psycopg2<\/p>\n<h2><strong>\u8fde\u63a5PostgreSQL\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = psycopg2.connect(<\/p>\n<p>    dbname=&quot;yourdatabase&quot;,<\/p>\n<p>    user=&quot;yourusername&quot;,<\/p>\n<p>    password=&quot;yourpassword&quot;,<\/p>\n<p>    host=&quot;localhost&quot;<\/p>\n<p>)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884cSQL\u8bed\u53e5<\/strong><\/h2>\n<p>cursor.execute(&quot;CREATE TABLE IF NOT EXISTS users (id SERIAL PRIMARY KEY, name VARCHAR(255), age INT)&quot;)<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&quot;INSERT INTO users (name, age) VALUES (&#39;Alice&#39;, 30)&quot;)<\/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 users&quot;)<\/p>\n<p>print(cursor.fetchall())<\/p>\n<h2><strong>\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/strong><\/h2>\n<p>cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>4. SQLAlchemy<\/strong><\/p>\n<\/p>\n<p><p>SQLAlchemy\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684ORM\u5e93\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\u3002\u4f7f\u7528\u524d\u9700\u8981\u5b89\u88c5SQLAlchemy\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine, Column, Integer, String<\/p>\n<p>from sqlalchemy.ext.declarative import declarative_base<\/p>\n<p>from sqlalchemy.orm import sessionmaker<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u5e93\u5f15\u64ce<\/strong><\/h2>\n<p>engine = create_engine(&#39;sqlite:\/\/\/example.db&#39;, echo=True)<\/p>\n<h2><strong>\u521b\u5efa\u57fa\u7c7b<\/strong><\/h2>\n<p>Base = declarative_base()<\/p>\n<h2><strong>\u5b9a\u4e49\u6570\u636e\u6a21\u578b<\/strong><\/h2>\n<p>class User(Base):<\/p>\n<p>    __tablename__ = &#39;users&#39;<\/p>\n<p>    id = Column(Integer, primary_key=True)<\/p>\n<p>    name = Column(String)<\/p>\n<p>    age = Column(Integer)<\/p>\n<h2><strong>\u521b\u5efa\u6240\u6709\u8868<\/strong><\/h2>\n<p>Base.metadata.create_all(engine)<\/p>\n<h2><strong>\u521b\u5efa\u4f1a\u8bdd<\/strong><\/h2>\n<p>Session = sessionmaker(bind=engine)<\/p>\n<p>session = Session()<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>new_user = User(name=&#39;Alice&#39;, age=30)<\/p>\n<p>session.add(new_user)<\/p>\n<p>session.commit()<\/p>\n<h2><strong>\u67e5\u8be2\u6570\u636e<\/strong><\/h2>\n<p>users = session.query(User).all()<\/p>\n<p>for user in users:<\/p>\n<p>    print(user.name, user.age)<\/p>\n<h2><strong>\u5173\u95ed\u4f1a\u8bdd<\/strong><\/h2>\n<p>session.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u8fde\u63a5\u6570\u636e\u5e93<\/p>\n<\/p>\n<p><p>\u8fde\u63a5\u6570\u636e\u5e93\u662f\u64cd\u4f5c\u6570\u636e\u5e93\u7684\u7b2c\u4e00\u6b65\uff0c\u4e0d\u540c\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u6709\u4e0d\u540c\u7684\u8fde\u63a5\u65b9\u6cd5\u3002\u65e0\u8bba\u662f\u54ea\u79cd\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff0c\u90fd\u9700\u8981\u63d0\u4f9b\u6570\u636e\u5e93\u7684\u8fde\u63a5\u4fe1\u606f\uff0c\u5305\u62ec\u4e3b\u673a\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u548c\u6570\u636e\u5e93\u540d\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>1. SQLite3<\/strong><\/p>\n<\/p>\n<p><p>SQLite3\u8fde\u63a5\u6570\u636e\u5e93\u65f6\uff0c\u53ea\u9700\u8981\u63d0\u4f9b\u6570\u636e\u5e93\u6587\u4ef6\u7684\u8def\u5f84\u3002\u5982\u679c\u6570\u636e\u5e93\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5c06\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u5e93\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. MySQL Connector<\/strong><\/p>\n<\/p>\n<p><p>MySQL Connector\u8fde\u63a5MySQL\u6570\u636e\u5e93\u65f6\uff0c\u9700\u8981\u63d0\u4f9b\u4e3b\u673a\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u548c\u6570\u636e\u5e93\u540d\u7b49\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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><p><strong>3. Psycopg2<\/strong><\/p>\n<\/p>\n<p><p>Psycopg2\u8fde\u63a5PostgreSQL\u6570\u636e\u5e93\u65f6\uff0c\u9700\u8981\u63d0\u4f9b\u6570\u636e\u5e93\u540d\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u548c\u4e3b\u673a\u7b49\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">conn = psycopg2.connect(<\/p>\n<p>    dbname=&quot;yourdatabase&quot;,<\/p>\n<p>    user=&quot;yourusername&quot;,<\/p>\n<p>    password=&quot;yourpassword&quot;,<\/p>\n<p>    host=&quot;localhost&quot;<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>4. SQLAlchemy<\/strong><\/p>\n<\/p>\n<p><p>SQLAlchemy\u8fde\u63a5\u6570\u636e\u5e93\u65f6\uff0c\u9700\u8981\u63d0\u4f9b\u6570\u636e\u5e93\u7684\u8fde\u63a5\u5b57\u7b26\u4e32\u3002\u8fde\u63a5\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u4e3a\uff1a<code>dialect+driver:\/\/username:password@host:port\/database<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">engine = create_engine(&#39;sqlite:\/\/\/example.db&#39;, echo=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u6267\u884cSQL\u8bed\u53e5<\/p>\n<\/p>\n<p><p>\u8fde\u63a5\u6570\u636e\u5e93\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6e38\u6807\u5bf9\u8c61\u6267\u884cSQL\u8bed\u53e5\u3002\u5e38\u89c1\u7684SQL\u8bed\u53e5\u5305\u62ec\uff1a\u521b\u5efa\u8868\u3001\u63d2\u5165\u6570\u636e\u3001\u66f4\u65b0\u6570\u636e\u3001\u5220\u9664\u6570\u636e\u548c\u67e5\u8be2\u6570\u636e\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u521b\u5efa\u8868<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u8868\u662f\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u7b2c\u4e00\u6b65\uff0c\u8868\u7684\u7ed3\u6784\u5305\u62ec\u5b57\u6bb5\u540d\u548c\u5b57\u6bb5\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS users<\/p>\n<p>                 (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)&#39;&#39;&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u63d2\u5165\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u63d2\u5165\u6570\u636e\u662f\u5c06\u6570\u636e\u5199\u5165\u8868\u4e2d\uff0c\u9700\u8981\u63d0\u4f9b\u5b57\u6bb5\u540d\u548c\u5b57\u6bb5\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.execute(&quot;INSERT INTO users (name, age) VALUES (&#39;Alice&#39;, 30)&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u66f4\u65b0\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u66f4\u65b0\u6570\u636e\u662f\u4fee\u6539\u8868\u4e2d\u7684\u6570\u636e\uff0c\u9700\u8981\u63d0\u4f9b\u8981\u4fee\u6539\u7684\u6570\u636e\u6761\u4ef6\u548c\u65b0\u7684\u6570\u636e\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.execute(&quot;UPDATE users SET age = 31 WHERE name = &#39;Alice&#39;&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>4. \u5220\u9664\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u5220\u9664\u6570\u636e\u662f\u4ece\u8868\u4e2d\u5220\u9664\u6570\u636e\uff0c\u9700\u8981\u63d0\u4f9b\u8981\u5220\u9664\u7684\u6570\u636e\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.execute(&quot;DELETE FROM users WHERE name = &#39;Alice&#39;&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>5. \u67e5\u8be2\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u67e5\u8be2\u6570\u636e\u662f\u4ece\u8868\u4e2d\u83b7\u53d6\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528SELECT\u8bed\u53e5\u6307\u5b9a\u8981\u67e5\u8be2\u7684\u5b57\u6bb5\u548c\u67e5\u8be2\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.execute(&quot;SELECT * FROM users&quot;)<\/p>\n<p>print(cursor.fetchall())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u67e5\u8be2\u7ed3\u679c<\/p>\n<\/p>\n<p><p>\u67e5\u8be2\u6570\u636e\u540e\uff0c\u6e38\u6807\u5bf9\u8c61\u4f1a\u8fd4\u56de\u67e5\u8be2\u7ed3\u679c\u3002\u5e38\u89c1\u7684\u5904\u7406\u67e5\u8be2\u7ed3\u679c\u7684\u65b9\u6cd5\u5305\u62ec\uff1afetchone\u3001fetchall\u548c\u8fed\u4ee3\u6e38\u6807\u5bf9\u8c61\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>1. fetchone<\/strong><\/p>\n<\/p>\n<p><p>fetchone\u65b9\u6cd5\u8fd4\u56de\u67e5\u8be2\u7ed3\u679c\u7684\u7b2c\u4e00\u6761\u8bb0\u5f55\uff0c\u5982\u679c\u6ca1\u6709\u8bb0\u5f55\u8fd4\u56deNone\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = cursor.fetchone()<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. fetchall<\/strong><\/p>\n<\/p>\n<p><p>fetchall\u65b9\u6cd5\u8fd4\u56de\u67e5\u8be2\u7ed3\u679c\u7684\u6240\u6709\u8bb0\u5f55\uff0c\u7ed3\u679c\u662f\u4e00\u4e2a\u5305\u542b\u591a\u6761\u8bb0\u5f55\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">results = cursor.fetchall()<\/p>\n<p>for result in results:<\/p>\n<p>    print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u8fed\u4ee3\u6e38\u6807\u5bf9\u8c61<\/strong><\/p>\n<\/p>\n<p><p>\u6e38\u6807\u5bf9\u8c61\u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u6240\u6709\u7684\u8bb0\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for result in cursor:<\/p>\n<p>    print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/p>\n<\/p>\n<p><p>\u64cd\u4f5c\u6570\u636e\u5e93\u5b8c\u6210\u540e\uff0c\u9700\u8981\u5173\u95ed\u6e38\u6807\u5bf9\u8c61\u548c\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u4ee5\u91ca\u653e\u6570\u636e\u5e93\u8d44\u6e90\u3002<\/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><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u64cd\u4f5c\u6570\u636e\u5e93\u7684\u6838\u5fc3\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u3001\u8fde\u63a5\u6570\u636e\u5e93\u3001\u6267\u884cSQL\u8bed\u53e5\u3001\u5904\u7406\u67e5\u8be2\u7ed3\u679c\u548c\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u3002\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u662f\u64cd\u4f5c\u6570\u636e\u5e93\u7684\u7b2c\u4e00\u6b65\uff0c\u5e38\u89c1\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u5305\u62ecSQLite3\u3001MySQL Connector\u3001Psycopg2\u548cSQLAlchemy\u3002\u8fde\u63a5\u6570\u636e\u5e93\u65f6\u9700\u8981\u63d0\u4f9b\u6570\u636e\u5e93\u7684\u8fde\u63a5\u4fe1\u606f\uff0c\u6267\u884cSQL\u8bed\u53e5\u65f6\u53ef\u4ee5\u4f7f\u7528\u6e38\u6807\u5bf9\u8c61\uff0c\u5904\u7406\u67e5\u8be2\u7ed3\u679c\u65f6\u53ef\u4ee5\u4f7f\u7528fetchone\u3001fetchall\u548c\u8fed\u4ee3\u6e38\u6807\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002\u64cd\u4f5c\u6570\u636e\u5e93\u5b8c\u6210\u540e\uff0c\u9700\u8981\u5173\u95ed\u6e38\u6807\u5bf9\u8c61\u548c\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u4ee5\u91ca\u653e\u6570\u636e\u5e93\u8d44\u6e90\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\u6765\u8fde\u63a5\u5230\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u5e93\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8eMySQL\uff0c\u5e38\u7528\u7684\u5e93\u662f<code>mysql-connector<\/code>\u6216<code>PyMySQL<\/code>\uff1b\u5bf9\u4e8eSQLite\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>sqlite3<\/code>\u5e93\u3002\u8fde\u63a5\u6570\u636e\u5e93\u7684\u6b65\u9aa4\u901a\u5e38\u5305\u62ec\u5bfc\u5165\u5e93\u3001\u521b\u5efa\u8fde\u63a5\u5bf9\u8c61\u3001\u6253\u5f00\u6e38\u6807\u4ee5\u53ca\u6267\u884c\u67e5\u8be2\u3002\u8fde\u63a5\u5b57\u7b26\u4e32\u901a\u5e38\u5305\u542b\u6570\u636e\u5e93\u7684\u5730\u5740\u3001\u7528\u6237\u540d\u548c\u5bc6\u7801\u7b49\u4fe1\u606f\u3002<\/p>\n<p><strong>Python\u652f\u6301\u54ea\u4e9b\u7c7b\u578b\u7684\u6570\u636e\u5e93\u64cd\u4f5c\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u5305\u62ec\u521b\u5efa\u3001\u8bfb\u53d6\u3001\u66f4\u65b0\u548c\u5220\u9664\uff08CRUD\uff09\u64cd\u4f5c\u3002\u901a\u8fc7\u4f7f\u7528SQL\u8bed\u53e5\uff0c\u53ef\u4ee5\u5bf9\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u67e5\u8be2\u3001\u63d2\u5165\u65b0\u8bb0\u5f55\u3001\u66f4\u65b0\u73b0\u6709\u8bb0\u5f55\u548c\u5220\u9664\u4e0d\u9700\u8981\u7684\u8bb0\u5f55\u3002\u6b64\u5916\uff0cPython\u8fd8\u652f\u6301\u4e8b\u52a1\u5904\u7406\uff0c\u786e\u4fdd\u6570\u636e\u64cd\u4f5c\u7684\u539f\u5b50\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u6570\u636e\u5e93\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u5404\u79cd\u9519\u8bef\uff0c\u4f8b\u5982\u8fde\u63a5\u5931\u8d25\u3001SQL\u8bed\u6cd5\u9519\u8bef\u6216\u6570\u636e\u7c7b\u578b\u4e0d\u5339\u914d\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002\u901a\u8fc7\u6355\u83b7\u7279\u5b9a\u7684\u5f02\u5e38\u7c7b\u578b\uff08\u5982<code>sqlite3.Error<\/code>\u6216<code>mysql.connector.Error<\/code>\uff09\uff0c\u53ef\u4ee5\u63d0\u4f9b\u66f4\u53cb\u597d\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u5e76\u91c7\u53d6\u9002\u5f53\u7684\u6062\u590d\u63aa\u65bd\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u64cd\u4f5c\u6570\u636e\u5e93\u7684\u6838\u5fc3\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff08\u5982SQLite3\u3001MySQL Connector\u3001Psy [&hellip;]","protected":false},"author":3,"featured_media":1054106,"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\/1054103"}],"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=1054103"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1054103\/revisions"}],"predecessor-version":[{"id":1054107,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1054103\/revisions\/1054107"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1054106"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1054103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1054103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1054103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}