{"id":999532,"date":"2024-12-27T09:42:19","date_gmt":"2024-12-27T01:42:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/999532.html"},"modified":"2024-12-27T09:42:21","modified_gmt":"2024-12-27T01:42:21","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8sql%e8%af%ad%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/999532.html","title":{"rendered":"Python\u5982\u4f55\u7528sql\u8bed\u53e5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074512\/c2d62716-9729-4c6f-909a-987ca6df236a.webp\" alt=\"Python\u5982\u4f55\u7528sql\u8bed\u53e5\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u4f7f\u7528SQL\u8bed\u53e5\u4e3b\u8981\u901a\u8fc7\u6570\u636e\u5e93\u63a5\u53e3\u5e93\u6765\u5b9e\u73b0\uff0c\u5982SQLite\u3001MySQL\u3001PostgreSQL\u7b49\u3002\u8fd9\u4e9b\u5e93\u5141\u8bb8\u4f60\u5728Python\u4ee3\u7801\u4e2d\u5d4c\u5165SQL\u8bed\u53e5\u6765\u67e5\u8be2\u548c\u64cd\u4f5c\u6570\u636e\u5e93\u3002<strong>\u901a\u8fc7\u4f7f\u7528Python\u7684\u6570\u636e\u5e93\u5e93\u5982sqlite3\u3001MySQL Connector\u3001SQLAlchemy\u7b49\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u6267\u884cSQL\u8bed\u53e5\u3001\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u3001\u63d0\u9ad8\u6570\u636e\u5904\u7406\u6548\u7387<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528SQL\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528sqlite3\u5e93<\/h3>\n<\/p>\n<p><h4>1. \u8fde\u63a5\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>SQLite\u662fPython\u5185\u7f6e\u7684\u8f7b\u91cf\u7ea7\u6570\u636e\u5e93\uff0c\u9002\u5408\u5feb\u901f\u5f00\u53d1\u548c\u6d4b\u8bd5\u3002\u8981\u4f7f\u7528SQLite\u6570\u636e\u5e93\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165sqlite3\u6a21\u5757\u5e76\u5efa\u7acb\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93\uff08\u5982\u679c\u6570\u636e\u5e93\u4e0d\u5b58\u5728\uff0c\u5219\u81ea\u52a8\u521b\u5efa\uff09<\/strong><\/h2>\n<p>connection = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u6e38\u6807\u5bf9\u8c61\u5e76\u6267\u884cSQL\u8bed\u53e5<\/h4>\n<\/p>\n<p><p>\u6e38\u6807\u5bf9\u8c61\u7528\u4e8e\u6267\u884cSQL\u8bed\u53e5\u5e76\u83b7\u53d6\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor = connection.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>cursor.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS students<\/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 students (name, age) VALUES (&#39;Alice&#39;, 21)&quot;)<\/p>\n<p>connection.commit()  # \u63d0\u4ea4\u4e8b\u52a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u67e5\u8be2\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u6267\u884cSELECT\u8bed\u53e5\u6765\u67e5\u8be2\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.execute(&quot;SELECT * FROM students&quot;)<\/p>\n<p>rows = cursor.fetchall()<\/p>\n<p>for row in rows:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u5173\u95ed\u8fde\u63a5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528MySQL Connector<\/h3>\n<\/p>\n<p><p>MySQL Connector\u662f\u7528\u4e8ePython\u4e0eMySQL\u6570\u636e\u5e93\u901a\u4fe1\u7684\u5b98\u65b9\u5e93\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u5e76\u8fde\u63a5\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5MySQL Connector\u5e93\u3002<\/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>\u7136\u540e\uff0c\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<p>connection = 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>2. \u6267\u884cSQL\u8bed\u53e5<\/h4>\n<\/p>\n<p><p>\u4e0eSQLite\u7c7b\u4f3c\uff0c\u5148\u521b\u5efa\u6e38\u6807\u5bf9\u8c61\uff0c\u7136\u540e\u6267\u884cSQL\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor = connection.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>cursor.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS employees<\/p>\n<p>                  (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), salary FLOAT)&#39;&#39;&#39;)<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&quot;INSERT INTO employees (name, salary) VALUES (&#39;John Doe&#39;, 50000)&quot;)<\/p>\n<p>connection.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u67e5\u8be2\u548c\u5904\u7406\u6570\u636e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.execute(&quot;SELECT * FROM employees&quot;)<\/p>\n<p>result = cursor.fetchall()<\/p>\n<p>for record in result:<\/p>\n<p>    print(record)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u5173\u95ed\u8fde\u63a5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528SQLAlchemy<\/h3>\n<\/p>\n<p><p>SQLAlchemy\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u5e93\uff0c\u5141\u8bb8\u4f7f\u7528Python\u5bf9\u8c61\u6765\u8868\u793a\u6570\u636e\u5e93\u7ed3\u6784\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. \u5efa\u7acb\u8fde\u63a5\u548c\u5b9a\u4e49\u6a21\u578b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine, Column, Integer, String, Float<\/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\u6a21\u578b<\/strong><\/h2>\n<p>class Product(Base):<\/p>\n<p>    __tablename__ = &#39;products&#39;<\/p>\n<p>    id = Column(Integer, primary_key=True)<\/p>\n<p>    name = Column(String)<\/p>\n<p>    price = Column(Float)<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>Base.metadata.create_all(engine)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u521b\u5efa\u4f1a\u8bdd\u5e76\u6267\u884c\u64cd\u4f5c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">Session = sessionmaker(bind=engine)<\/p>\n<p>session = Session()<\/p>\n<h2><strong>\u6dfb\u52a0\u8bb0\u5f55<\/strong><\/h2>\n<p>new_product = Product(name=&#39;Laptop&#39;, price=799.99)<\/p>\n<p>session.add(new_product)<\/p>\n<p>session.commit()<\/p>\n<h2><strong>\u67e5\u8be2\u8bb0\u5f55<\/strong><\/h2>\n<p>for product in session.query(Product).all():<\/p>\n<p>    print(product.name, product.price)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u5173\u95ed\u4f1a\u8bdd<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">session.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u4f7f\u7528SQL\u8bed\u53e5\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u4e3b\u8981\u4f9d\u8d56\u4e8e\u76f8\u5e94\u7684\u6570\u636e\u5e93\u5e93\u3002<strong>\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027<\/strong>\u3002SQLite\u9002\u7528\u4e8e\u8f7b\u91cf\u7ea7\u5e94\u7528\u548c\u6d4b\u8bd5\uff0cMySQL Connector\u9002\u7528\u4e8e\u4e0eMySQL\u6570\u636e\u5e93\u4ea4\u4e92\uff0c\u800cSQLAlchemy\u4f5c\u4e3aORM\u5de5\u5177\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c42\u6b21\u7684\u62bd\u8c61\uff0c\u975e\u5e38\u9002\u5408\u5927\u578b\u9879\u76ee\u548c\u590d\u6742\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u6839\u636e\u9879\u76ee\u9700\u6c42\uff0c\u5408\u7406\u9009\u62e9\u5e76\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2d\u5982\u4f55\u8fde\u63a5\u6570\u636e\u5e93\u4ee5\u6267\u884cSQL\u8bed\u53e5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\u8fde\u63a5\u5230\u6570\u636e\u5e93\u5e76\u6267\u884cSQL\u8bed\u53e5\u3002\u6700\u5e38\u7528\u7684\u5e93\u5305\u62ec<code>sqlite3<\/code>\u3001<code>MySQL Connector<\/code>\u548c<code>SQLAlchemy<\/code>\u3002\u8fde\u63a5\u6570\u636e\u5e93\u65f6\uff0c\u60a8\u9700\u8981\u63d0\u4f9b\u6570\u636e\u5e93\u7684\u7c7b\u578b\u3001\u540d\u79f0\u3001\u7528\u6237\u51ed\u8bc1\u7b49\u4fe1\u606f\u3002\u8fde\u63a5\u6210\u529f\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>cursor<\/code>\u5bf9\u8c61\u6267\u884c\u5404\u79cdSQL\u8bed\u53e5\uff0c\u5982\u67e5\u8be2\u3001\u63d2\u5165\u3001\u66f4\u65b0\u548c\u5220\u9664\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406SQL\u67e5\u8be2\u7ed3\u679c\uff1f<\/strong><br \/>\u6267\u884cSQL\u67e5\u8be2\u540e\uff0c\u901a\u5e38\u4f1a\u5f97\u5230\u4e00\u4e2a\u7ed3\u679c\u96c6\u3002\u53ef\u4ee5\u901a\u8fc7<code>cursor.fetchall()<\/code>\u6216<code>cursor.fetchone()<\/code>\u65b9\u6cd5\u83b7\u53d6\u7ed3\u679c\u3002<code>fetchall()<\/code>\u4f1a\u8fd4\u56de\u6240\u6709\u7ed3\u679c\uff0c\u800c<code>fetchone()<\/code>\u5219\u8fd4\u56de\u7ed3\u679c\u96c6\u4e2d\u7684\u4e0b\u4e00\u884c\u3002\u83b7\u53d6\u5230\u7ed3\u679c\u540e\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u5316\u4e3aPython\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5217\u8868\u6216\u5b57\u5178\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u6267\u884c\u591a\u6761SQL\u8bed\u53e5\uff1f<\/strong><br \/>\u5728Python\u4e2d\u6267\u884c\u591a\u6761SQL\u8bed\u53e5\u53ef\u4ee5\u901a\u8fc7\u5c06\u8bed\u53e5\u5206\u9694\u5f00\u6765\u5b9e\u73b0\u3002\u4f7f\u7528<code>execute()<\/code>\u65b9\u6cd5\u65f6\uff0c\u53ef\u4ee5\u4f20\u5165\u5305\u542b\u591a\u6761\u8bed\u53e5\u7684\u5b57\u7b26\u4e32\u3002\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u540c\u7684\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\u5bf9\u591a\u6761\u8bed\u53e5\u7684\u652f\u6301\u6709\u6240\u4e0d\u540c\u3002\u6709\u65f6\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u4e8b\u52a1\u6765\u786e\u4fdd\u6570\u636e\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u3002\u5728\u6267\u884c\u591a\u6761SQL\u8bed\u53e5\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u4e8b\u52a1\u63a7\u5236\uff0c\u6bd4\u5982<code>commit()<\/code>\u548c<code>rollback()<\/code>\uff0c\u4ee5\u907f\u514d\u6570\u636e\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f7f\u7528SQL\u8bed\u53e5\u4e3b\u8981\u901a\u8fc7\u6570\u636e\u5e93\u63a5\u53e3\u5e93\u6765\u5b9e\u73b0\uff0c\u5982SQLite\u3001MySQL\u3001PostgreSQL\u7b49\u3002 [&hellip;]","protected":false},"author":3,"featured_media":999538,"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\/999532"}],"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=999532"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999532\/revisions"}],"predecessor-version":[{"id":999539,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999532\/revisions\/999539"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/999538"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=999532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=999532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=999532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}