{"id":1136775,"date":"2025-01-08T21:39:53","date_gmt":"2025-01-08T13:39:53","guid":{"rendered":""},"modified":"2025-01-08T21:39:57","modified_gmt":"2025-01-08T13:39:57","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96%e4%b8%80%e8%a1%8c%e6%95%b0%e6%8d%ae%e5%ba%93%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1136775.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6\u4e00\u884c\u6570\u636e\u5e93\u4e2d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100803\/1ff5ac2a-026e-4301-ab7c-a9683c8b8a0f.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6\u4e00\u884c\u6570\u636e\u5e93\u4e2d\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u8bfb\u53d6\u4e00\u884c\u6570\u636e\u5e93\u4e2d<\/strong>  <\/p>\n<\/p>\n<p><p><strong>Python\u8bfb\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u884c\u6570\u636e\u4e3b\u8981\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528SQLAlchemy\u3001\u4f7f\u7528PyMySQL\u3001\u4f7f\u7528SQLite\u5e93\u3001\u4f7f\u7528Pandas\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528SQLAlchemy\u7684\u65b9\u5f0f\u662f\u8f83\u4e3a\u63a8\u8350\u7684\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\u7c7b\u578b\uff0c\u8fd8\u80fd\u63d0\u4f9bORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u529f\u80fd\uff0c\u4f7f\u5f97\u6570\u636e\u5e93\u64cd\u4f5c\u66f4\u4e3a\u4fbf\u6377\u548c\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528SQLAlchemy<\/h3>\n<\/p>\n<p><p>SQLAlchemy \u662f\u4e00\u4e2a\u5f3a\u5927\u7684 Python SQL \u5de5\u5177\u5305\u548c ORM \u6846\u67b6\u3002\u5b83\u63d0\u4f9b\u4e86\u5b8c\u6574\u7684\u6570\u636e\u5e93\u8bbf\u95ee\u529f\u80fd\uff0c\u5e76\u4e14\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\uff0c\u5305\u62ecMySQL\u3001PostgreSQL\u3001SQLite\u7b49\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 SQLAlchemy \u8bfb\u53d6\u4e00\u884c\u6570\u636e\u5e93\u6570\u636e\u7684\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5SQLAlchemy<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5 SQLAlchemy\uff0c\u53ef\u4ee5\u4f7f\u7528 pip \u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install sqlalchemy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8fde\u63a5\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93\u5f15\u64ce\uff0c\u5e76\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u5e93\u5f15\u64ce\uff0c\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93<\/strong><\/h2>\n<p>engine = create_engine(&#39;mysql+pymysql:\/\/username:password@host\/dbname&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5b9a\u4e49\u8868\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 SQLAlchemy \u7684 ORM \u529f\u80fd\uff0c\u5b9a\u4e49\u6570\u636e\u5e93\u4e2d\u7684\u8868\u7ed3\u6784\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<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<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u67e5\u8be2\u4e00\u884c\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 SQLAlchemy \u7684\u4f1a\u8bdd\u529f\u80fd\uff0c\u67e5\u8be2\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u884c\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy.orm import sessionmaker<\/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>\u67e5\u8be2\u4e00\u884c\u6570\u636e<\/strong><\/h2>\n<p>user = session.query(User).first()<\/p>\n<h2><strong>\u8f93\u51fa\u67e5\u8be2\u7ed3\u679c<\/strong><\/h2>\n<p>print(f&quot;ID: {user.id}, Name: {user.name}, Age: {user.age}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528PyMySQL<\/h3>\n<\/p>\n<p><p>PyMySQL \u662f\u4e00\u4e2a\u7eaf Python \u5b9e\u73b0\u7684 MySQL \u5ba2\u6237\u7aef\u3002\u5b83\u5141\u8bb8\u4f60\u901a\u8fc7 Python \u4ee3\u7801\u4e0e MySQL \u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 PyMySQL \u8bfb\u53d6\u4e00\u884c\u6570\u636e\u5e93\u6570\u636e\u7684\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5PyMySQL<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5 PyMySQL\uff0c\u53ef\u4ee5\u4f7f\u7528 pip \u8fdb\u884c\u5b89\u88c5\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. \u8fde\u63a5\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 PyMySQL \u8fde\u63a5\u5230 MySQL \u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<h2><strong>\u8fde\u63a5\u5230\u6570\u636e\u5e93<\/strong><\/h2>\n<p>connection = pymysql.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;username&#39;,<\/p>\n<p>    password=&#39;password&#39;,<\/p>\n<p>    database=&#39;dbname&#39;<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u67e5\u8be2\u4e00\u884c\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 PyMySQL \u7684\u6e38\u6807\u529f\u80fd\uff0c\u67e5\u8be2\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u884c\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with connection.cursor() as cursor:<\/p>\n<p>        # \u6267\u884cSQL\u67e5\u8be2<\/p>\n<p>        sql = &quot;SELECT id, name, age FROM users LIMIT 1&quot;<\/p>\n<p>        cursor.execute(sql)<\/p>\n<p>        # \u83b7\u53d6\u67e5\u8be2\u7ed3\u679c<\/p>\n<p>        result = cursor.fetchone()<\/p>\n<p>        print(f&quot;ID: {result[0]}, Name: {result[1]}, Age: {result[2]}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528SQLite\u5e93<\/h3>\n<\/p>\n<p><p>SQLite \u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u5d4c\u5165\u5f0f\u6570\u636e\u5e93\uff0c\u5b83\u968f Python \u4e00\u8d77\u63d0\u4f9b\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 SQLite \u8bfb\u53d6\u4e00\u884c\u6570\u636e\u5e93\u6570\u636e\u7684\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1. \u8fde\u63a5\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 SQLite3 \u5e93\u8fde\u63a5\u5230 SQLite \u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u5230\u6570\u636e\u5e93<\/strong><\/h2>\n<p>connection = sqlite3.connect(&#39;database.db&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u67e5\u8be2\u4e00\u884c\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 SQLite3 \u7684\u6e38\u6807\u529f\u80fd\uff0c\u67e5\u8be2\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u884c\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    cursor = connection.cursor()<\/p>\n<p>    # \u6267\u884cSQL\u67e5\u8be2<\/p>\n<p>    sql = &quot;SELECT id, name, age FROM users LIMIT 1&quot;<\/p>\n<p>    cursor.execute(sql)<\/p>\n<p>    # \u83b7\u53d6\u67e5\u8be2\u7ed3\u679c<\/p>\n<p>    result = cursor.fetchone()<\/p>\n<p>    print(f&quot;ID: {result[0]}, Name: {result[1]}, Age: {result[2]}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas \u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 Pandas \u8bfb\u53d6\u4e00\u884c\u6570\u636e\u5e93\u6570\u636e\u7684\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Pandas<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5 Pandas\uff0c\u53ef\u4ee5\u4f7f\u7528 pip \u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8fde\u63a5\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 Pandas \u8fde\u63a5\u5230\u6570\u636e\u5e93\uff0c\u5e76\u8bfb\u53d6\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u5230\u6570\u636e\u5e93<\/strong><\/h2>\n<p>connection = sqlite3.connect(&#39;database.db&#39;)<\/p>\n<h2><strong>\u4f7f\u7528Pandas\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>df = pd.read_sql_query(&quot;SELECT id, name, age FROM users LIMIT 1&quot;, connection)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u5f0f\uff0cPython \u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u884c\u6570\u636e\u3002<strong>\u4f7f\u7528SQLAlchemy\u3001\u4f7f\u7528PyMySQL\u3001\u4f7f\u7528SQLite\u5e93\u3001\u4f7f\u7528Pandas\u5e93<\/strong>\uff0c\u5404\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<strong>\u63a8\u8350\u4f7f\u7528SQLAlchemy<\/strong>\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\u7c7b\u578b\uff0c\u8fd8\u80fd\u63d0\u4f9bORM\u529f\u80fd\uff0c\u4f7f\u5f97\u6570\u636e\u5e93\u64cd\u4f5c\u66f4\u4e3a\u4fbf\u6377\u548c\u76f4\u89c2\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u7279\u5b9a\u884c\uff1f<\/strong><br \/>\u8981\u8bfb\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u7279\u5b9a\u884c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6570\u636e\u5e93\u63a5\u53e3\u5e93\uff0c\u5982<code>sqlite3<\/code>\u3001<code>MySQL Connector<\/code>\u6216<code>SQLAlchemy<\/code>\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u7136\u540e\u7f16\u5199SQL\u67e5\u8be2\u8bed\u53e5\u6765\u9009\u62e9\u60a8\u60f3\u8981\u7684\u884c\u3002\u6267\u884c\u67e5\u8be2\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>fetchone()<\/code>\u6216<code>fetchall()<\/code>\u65b9\u6cd5\u83b7\u53d6\u7ed3\u679c\u3002<\/p>\n<p><strong>\u6211\u9700\u8981\u5b89\u88c5\u54ea\u4e9b\u5e93\u624d\u80fd\u4f7f\u7528Python\u8bfb\u53d6\u6570\u636e\u5e93\uff1f<\/strong><br \/>\u6839\u636e\u60a8\u4f7f\u7528\u7684\u6570\u636e\u5e93\u7c7b\u578b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5b89\u88c5\u4e0d\u540c\u7684\u5e93\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u4f7f\u7528SQLite\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>sqlite3<\/code>\u6a21\u5757\u3002\u5982\u679c\u60a8\u4f7f\u7528MySQL\uff0c\u5219\u9700\u8981\u5b89\u88c5<code>mysql-connector-python<\/code>\u6216<code>PyMySQL<\/code>\u3002\u5bf9\u4e8ePostgreSQL\uff0c\u53ef\u4ee5\u4f7f\u7528<code>psycopg2<\/code>\u3002\u901a\u8fc7<code>pip install &lt;\u5e93\u540d&gt;<\/code>\u547d\u4ee4\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b89\u88c5\u8fd9\u4e9b\u5e93\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u8bfb\u53d6\u6570\u636e\u5e93\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u8bfb\u53d6\u6570\u636e\u5e93\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u8fde\u63a5\u5931\u8d25\u3001SQL\u8bed\u6cd5\u9519\u8bef\u6216\u6570\u636e\u7c7b\u578b\u4e0d\u5339\u914d\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u95ee\u9898\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u5f02\u5e38\u5e76\u8f93\u51fa\u76f8\u5173\u9519\u8bef\u4fe1\u606f\u3002\u786e\u4fdd\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u5b8c\u6210\u540e\u5173\u95ed\u8fde\u63a5\uff0c\u4ee5\u91ca\u653e\u8d44\u6e90\u5e76\u907f\u514d\u6f5c\u5728\u7684\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u8bfb\u53d6\u4e00\u884c\u6570\u636e\u5e93\u4e2d Python\u8bfb\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u884c\u6570\u636e\u4e3b\u8981\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528SQLAlche [&hellip;]","protected":false},"author":3,"featured_media":1136782,"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\/1136775"}],"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=1136775"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136775\/revisions"}],"predecessor-version":[{"id":1136785,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136775\/revisions\/1136785"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1136782"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1136775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1136775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1136775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}