{"id":1129702,"date":"2025-01-08T20:31:54","date_gmt":"2025-01-08T12:31:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1129702.html"},"modified":"2025-01-08T20:31:57","modified_gmt":"2025-01-08T12:31:57","slug":"python%e5%a6%82%e4%bd%95%e5%b1%95%e7%a4%ba%e7%ae%a1%e7%90%86%e7%b3%bb%e7%bb%9f%e9%87%8c%e9%9d%a2%e7%9a%84%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1129702.html","title":{"rendered":"python\u5982\u4f55\u5c55\u793a\u7ba1\u7406\u7cfb\u7edf\u91cc\u9762\u7684\u6570\u636e\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100028\/89825fb7-72ad-4212-a6e9-44aeabcd341e.webp\" alt=\"python\u5982\u4f55\u5c55\u793a\u7ba1\u7406\u7cfb\u7edf\u91cc\u9762\u7684\u6570\u636e\u5e93\" \/><\/p>\n<p><p> <strong>Python\u5728\u5c55\u793a\u548c\u7ba1\u7406\u7cfb\u7edf\u6570\u636e\u5e93\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5de5\u5177\u548c\u6280\u672f\uff0c\u5982Django\u3001Flask\u3001SQLAlchemy\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fde\u63a5\u5230\u6570\u636e\u5e93\u3001\u6267\u884c\u67e5\u8be2\u3001\u5c55\u793a\u6570\u636e\u4ee5\u53ca\u8fdb\u884c\u5404\u79cd\u7ba1\u7406\u64cd\u4f5c\u3002<\/strong>\u672c\u6587\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5229\u7528Python\u6765\u5c55\u793a\u548c\u7ba1\u7406\u7cfb\u7edf\u4e2d\u7684\u6570\u636e\u5e93\uff0c\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Django\u548cSQLAlchemy\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Django\u5c55\u793a\u548c\u7ba1\u7406\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><h4>1. Django\u6846\u67b6\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>Django\u662f\u4e00\u4e2a\u9ad8\u7ea7Python Web\u6846\u67b6\uff0c\u65e8\u5728\u5feb\u901f\u5f00\u53d1\u548c\u7b80\u4fbf\u7684\u8bbe\u8ba1\u3002\u5b83\u5e26\u6709\u4e00\u4e2a\u5185\u7f6e\u7684ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\uff0c\u4f7f\u5f97\u6570\u636e\u5e93\u64cd\u4f5c\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002Django\u5185\u7f6e\u7684Admin\u754c\u9762\u8ba9\u5c55\u793a\u548c\u7ba1\u7406\u6570\u636e\u5e93\u53d8\u5f97\u975e\u5e38\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h4>2. \u5b89\u88c5\u548c\u914d\u7f6eDjango<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Django\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install django<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6bd5\u540e\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Django\u9879\u76ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">django-admin startproject myproject<\/p>\n<p>cd myproject<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python manage.py startapp myapp<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u914d\u7f6e\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u5728<code>settings.py<\/code>\u4e2d\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cDjango\u4f7f\u7528SQLite\u6570\u636e\u5e93\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.sqlite3&#39;,<\/p>\n<p>        &#39;NAME&#39;: BASE_DIR \/ &quot;db.sqlite3&quot;,<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u8fde\u63a5\u5230\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\u5e93\uff0c\u5982PostgreSQL\u6216MySQL\uff0c\u53ef\u4ee5\u4fee\u6539<code>ENGINE<\/code>\u548c\u5176\u4ed6\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h4>4. \u5b9a\u4e49\u6a21\u578b<\/h4>\n<\/p>\n<p><p>\u5728<code>myapp\/models.py<\/code>\u4e2d\u5b9a\u4e49\u4f60\u7684\u6570\u636e\u5e93\u6a21\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.db import models<\/p>\n<p>class Product(models.Model):<\/p>\n<p>    name = models.CharField(max_length=100)<\/p>\n<p>    price = models.DecimalField(max_digits=10, decimal_places=2)<\/p>\n<p>    description = models.TextField()<\/p>\n<p>    def __str__(self):<\/p>\n<p>        return self.name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u521b\u5efa\u548c\u5e94\u7528\u8fc1\u79fb<\/h4>\n<\/p>\n<p><p>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u521b\u5efa\u548c\u5e94\u7528\u6570\u636e\u5e93\u8fc1\u79fb\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python manage.py makemigrations<\/p>\n<p>python manage.py migrate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6. \u521b\u5efa\u8d85\u7ea7\u7528\u6237<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u8bbf\u95eeDjango\u7684Admin\u754c\u9762\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u8d85\u7ea7\u7528\u6237\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python manage.py createsuperuser<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6309\u7167\u63d0\u793a\u8f93\u5165\u7528\u6237\u540d\u3001\u90ae\u7bb1\u548c\u5bc6\u7801\u3002<\/p>\n<\/p>\n<p><h4>7. \u6ce8\u518c\u6a21\u578b\u5230Admin\u754c\u9762<\/h4>\n<\/p>\n<p><p>\u5728<code>myapp\/admin.py<\/code>\u4e2d\u6ce8\u518c\u4f60\u7684\u6a21\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.contrib import admin<\/p>\n<p>from .models import Product<\/p>\n<p>admin.site.register(Product)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>8. \u8fd0\u884c\u670d\u52a1\u5668\u5e76\u8bbf\u95eeAdmin\u754c\u9762<\/h4>\n<\/p>\n<p><p>\u8fd0\u884c\u5f00\u53d1\u670d\u52a1\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python manage.py runserver<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee<code>http:\/\/127.0.0.1:8000\/admin<\/code>\uff0c\u4f7f\u7528\u521a\u624d\u521b\u5efa\u7684\u8d85\u7ea7\u7528\u6237\u767b\u5f55\u3002\u4f60\u5c06\u770b\u5230\u4e00\u4e2a\u76f4\u89c2\u7684\u754c\u9762\u6765\u5c55\u793a\u548c\u7ba1\u7406\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528SQLAlchemy\u5c55\u793a\u548c\u7ba1\u7406\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><h4>1. SQLAlchemy\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>SQLAlchemy\u662f\u4e00\u4e2aPython SQL\u5de5\u5177\u5305\u548c\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\u5668\uff0c\u63d0\u4f9b\u4e86\u6570\u636e\u5e93\u9ad8\u5c42\u6b21\u548c\u4f4e\u5c42\u6b21\u7684\u8bbf\u95ee\u3002\u5b83\u9002\u7528\u4e8e\u90a3\u4e9b\u9700\u8981\u66f4\u591a\u63a7\u5236\u548c\u7075\u6d3b\u6027\u7684\u5f00\u53d1\u8005\u3002<\/p>\n<\/p>\n<p><h4>2. \u5b89\u88c5SQLAlchemy<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5SQLAlchemy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install sqlalchemy<\/p>\n<p>pip install pymysql  # \u5982\u679c\u4f7f\u7528MySQL\u6570\u636e\u5e93<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Python\u6587\u4ef6\uff0c\u4f8b\u5982<code>app.py<\/code>\uff0c\u5e76\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\uff1a<\/p>\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 = &quot;mysql+pymysql:\/\/username:password@localhost\/dbname&quot;<\/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>4. \u5b9a\u4e49\u6a21\u578b<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4f60\u7684\u6570\u636e\u5e93\u6a21\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import Column, Integer, String, DECIMAL<\/p>\n<p>class Product(Base):<\/p>\n<p>    __tablename__ = &#39;products&#39;<\/p>\n<p>    id = Column(Integer, primary_key=True, index=True)<\/p>\n<p>    name = Column(String(100), index=True)<\/p>\n<p>    price = Column(DECIMAL(10, 2))<\/p>\n<p>    description = Column(String(200))<\/p>\n<p>    def __repr__(self):<\/p>\n<p>        return f&quot;&lt;Product(name={self.name}, price={self.price})&gt;&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u521b\u5efa\u6570\u636e\u5e93\u8868<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u6570\u636e\u5e93\u8868\uff1a<\/p>\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>6. \u6570\u636e\u5e93\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy.orm import Session<\/p>\n<h2><strong>\u521b\u5efa\u4f1a\u8bdd<\/strong><\/h2>\n<p>db = SessionLocal()<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>new_product = Product(name=&quot;Sample Product&quot;, price=19.99, description=&quot;This is a sample product.&quot;)<\/p>\n<p>db.add(new_product)<\/p>\n<p>db.commit()<\/p>\n<h2><strong>\u67e5\u8be2\u6570\u636e<\/strong><\/h2>\n<p>products = db.query(Product).all()<\/p>\n<p>for product in products:<\/p>\n<p>    print(product)<\/p>\n<h2><strong>\u66f4\u65b0\u6570\u636e<\/strong><\/h2>\n<p>product_to_update = db.query(Product).filter(Product.name == &quot;Sample Product&quot;).first()<\/p>\n<p>product_to_update.price = 29.99<\/p>\n<p>db.commit()<\/p>\n<h2><strong>\u5220\u9664\u6570\u636e<\/strong><\/h2>\n<p>product_to_delete = db.query(Product).filter(Product.name == &quot;Sample Product&quot;).first()<\/p>\n<p>db.delete(product_to_delete)<\/p>\n<p>db.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Flask\u5c55\u793a\u548c\u7ba1\u7406\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><h4>1. Flask\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Python Web\u6846\u67b6\uff0c\u9002\u5408\u5c0f\u578b\u9879\u76ee\u548c\u5fae\u670d\u52a1\u3002\u4e0eDjango\u4e0d\u540c\uff0cFlask\u6ca1\u6709\u5185\u7f6e\u7684ORM\uff0c\u4f46\u53ef\u4ee5\u4e0eSQLAlchemy\u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>2. \u5b89\u88c5\u548c\u914d\u7f6eFlask<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Flask\u548cFlask-SQLAlchemy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install flask<\/p>\n<p>pip install flask_sqlalchemy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u914d\u7f6eFlask\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Flask\u5e94\u7528\uff0c\u4f8b\u5982<code>app.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask<\/p>\n<p>from flask_sqlalchemy import SQLAlchemy<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>app.config[&#39;SQLALCHEMY_DATABASE_URI&#39;] = &#39;mysql+pymysql:\/\/username:password@localhost\/dbname&#39;<\/p>\n<p>db = SQLAlchemy(app)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u5b9a\u4e49\u6a21\u578b<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4f60\u7684\u6570\u636e\u5e93\u6a21\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Product(db.Model):<\/p>\n<p>    id = db.Column(db.Integer, primary_key=True)<\/p>\n<p>    name = db.Column(db.String(100), nullable=False)<\/p>\n<p>    price = db.Column(db.Numeric(10, 2), nullable=False)<\/p>\n<p>    description = db.Column(db.String(200), nullable=False)<\/p>\n<p>    def __repr__(self):<\/p>\n<p>        return f&quot;&lt;Product(name={self.name}, price={self.price})&gt;&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u521b\u5efa\u6570\u636e\u5e93\u8868<\/h4>\n<\/p>\n<p><p>\u5728Flask shell\u4e2d\u521b\u5efa\u6570\u636e\u5e93\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">flask shell<\/p>\n<p>&gt;&gt;&gt; from app import db<\/p>\n<p>&gt;&gt;&gt; db.create_all()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6. \u8def\u7531\u548c\u89c6\u56fe\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e9b\u8def\u7531\u548c\u89c6\u56fe\u51fd\u6570\u6765\u5c55\u793a\u548c\u7ba1\u7406\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import render_template, request, redirect, url_for<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def index():<\/p>\n<p>    products = Product.query.all()<\/p>\n<p>    return render_template(&#39;index.html&#39;, products=products)<\/p>\n<p>@app.route(&#39;\/add&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def add_product():<\/p>\n<p>    name = request.form[&#39;name&#39;]<\/p>\n<p>    price = request.form[&#39;price&#39;]<\/p>\n<p>    description = request.form[&#39;description&#39;]<\/p>\n<p>    new_product = Product(name=name, price=price, description=description)<\/p>\n<p>    db.session.add(new_product)<\/p>\n<p>    db.session.commit()<\/p>\n<p>    return redirect(url_for(&#39;index&#39;))<\/p>\n<p>@app.route(&#39;\/delete\/&lt;int:id&gt;&#39;)<\/p>\n<p>def delete_product(id):<\/p>\n<p>    product = Product.query.get(id)<\/p>\n<p>    db.session.delete(product)<\/p>\n<p>    db.session.commit()<\/p>\n<p>    return redirect(url_for(&#39;index&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u63a2\u8ba8\u4e86\u5982\u4f55\u4f7f\u7528Python\u5c55\u793a\u548c\u7ba1\u7406\u7cfb\u7edf\u4e2d\u7684\u6570\u636e\u5e93\u3002<strong>Django<\/strong>\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u7684Admin\u754c\u9762\uff0c\u9002\u5408\u5feb\u901f\u5f00\u53d1\u548c\u90e8\u7f72\uff1b<strong>SQLAlchemy<\/strong>\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\u548c\u63a7\u5236\uff0c\u9002\u5408\u590d\u6742\u7684\u6570\u636e\u5e93\u64cd\u4f5c\uff1b<strong>Flask<\/strong>\u5219\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Web\u6846\u67b6\uff0c\u53ef\u4ee5\u4e0eSQLAlchemy\u7ed3\u5408\u4f7f\u7528\uff0c\u9002\u5408\u5c0f\u578b\u9879\u76ee\u548c\u5fae\u670d\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u4f60\u9009\u62e9\u54ea\u79cd\u5de5\u5177\uff0c\u5173\u952e\u5728\u4e8e\u7406\u89e3\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\uff0c\u5e76\u6839\u636e\u9879\u76ee\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u5de5\u5177\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u4f7f\u7528Python\u5c55\u793a\u548c\u7ba1\u7406\u7cfb\u7edf\u4e2d\u7684\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8fde\u63a5\u5230\u6211\u7684\u6570\u636e\u5e93\uff1f<\/strong><br \/>\u4e3a\u4e86\u5c55\u793a\u7ba1\u7406\u7cfb\u7edf\u4e2d\u7684\u6570\u636e\u5e93\uff0c\u60a8\u9700\u8981\u4f7f\u7528\u9002\u5408\u60a8\u6570\u636e\u5e93\u7684Python\u5e93\u3002\u4f8b\u5982\uff0c\u4f7f\u7528MySQL\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>mysql-connector-python<\/code>\u6216<code>SQLAlchemy<\/code>\u5e93\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u6240\u9700\u7684\u5e93\u3002\u7136\u540e\uff0c\u4f7f\u7528\u8fde\u63a5\u5b57\u7b26\u4e32\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff0c\u63d0\u4f9b\u5fc5\u8981\u7684\u51ed\u636e\uff0c\u5982\u4e3b\u673a\u540d\u3001\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u67e5\u8be2\u548c\u5c55\u793a\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\uff1f<\/strong><br \/>\u4e00\u65e6\u6210\u529f\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528SQL\u67e5\u8be2\u6765\u68c0\u7d22\u6570\u636e\u3002\u5229\u7528<code>cursor.execute()<\/code>\u65b9\u6cd5\u6267\u884cSQL\u8bed\u53e5\uff0c\u5e76\u7528<code>fetchall()<\/code>\u6216<code>fetchone()<\/code>\u65b9\u6cd5\u83b7\u53d6\u7ed3\u679c\u3002\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u9002\u5408\u5c55\u793a\u7684\u683c\u5f0f\uff0c\u6bd4\u5982\u5217\u8868\u6216\u5b57\u5178\uff0c\u63a5\u7740\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u5c06\u6570\u636e\u8f6c\u5316\u4e3a\u6570\u636e\u6846\uff0c\u65b9\u4fbf\u8fdb\u4e00\u6b65\u5904\u7406\u548c\u53ef\u89c6\u5316\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u548c\u5c55\u793a\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u53ef\u89c6\u5316\uff1f<\/strong><br \/>\u5728\u5904\u7406\u6570\u636e\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>matplotlib<\/code>\u6216<code>seaborn<\/code>\u7b49\u5e93\u8fdb\u884c\u53ef\u89c6\u5316\u3002\u5c06\u4ece\u6570\u636e\u5e93\u4e2d\u63d0\u53d6\u7684\u6570\u636e\u4f20\u5165\u8fd9\u4e9b\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u60a8\u53ef\u4ee5\u751f\u6210\u5404\u79cd\u56fe\u8868\uff0c\u5982\u6761\u5f62\u56fe\u3001\u6298\u7ebf\u56fe\u6216\u997c\u56fe\uff0c\u4ee5\u76f4\u89c2\u5c55\u793a\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u80fd\u591f\u6e05\u6670\u5448\u73b0\u6570\u636e\uff0c\u8fd8\u80fd\u5e2e\u52a9\u8bc6\u522b\u8d8b\u52bf\u548c\u6a21\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5728\u5c55\u793a\u548c\u7ba1\u7406\u7cfb\u7edf\u6570\u636e\u5e93\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5de5\u5177\u548c\u6280\u672f\uff0c\u5982Django\u3001Flask\u3001SQLAlchemy [&hellip;]","protected":false},"author":3,"featured_media":1129714,"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\/1129702"}],"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=1129702"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1129702\/revisions"}],"predecessor-version":[{"id":1129717,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1129702\/revisions\/1129717"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1129714"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1129702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1129702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1129702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}