From 792c9ec098a7ad5f6487eaff71e76ee10d3250d1 Mon Sep 17 00:00:00 2001 From: zart2007 Date: Mon, 17 Apr 2023 21:31:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=8E=E5=B8=82=E4=BF=A1=E6=81=AF=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 20 +++++++++++++++----- database.py | 41 ++++++++++++---------------------------- models.py | 23 +++++++++++++++++++--- templates/city_info.html | 30 +++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 37 deletions(-) create mode 100644 templates/city_info.html diff --git a/app.py b/app.py index 6394dcf..ed4934f 100644 --- a/app.py +++ b/app.py @@ -9,20 +9,19 @@ ''' from flask import Flask, render_template - +from models import User, City_Info +from database import * app = Flask(__name__) -app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:root@127.0.0.1/example' - - @app.route("/") def hello(): return "

hello

" +@app.route('/hello/') @app.route('/hello//') -def hello_2(name): +def hello_2(name=None): return render_template('index.html', name=name) @@ -37,5 +36,16 @@ def multi(): return render_template('table.html', table=table) +@app.route("/city_info/") +def city_info(): + filter = ["province", "cityname", 'usernumber', '操作'] + info = City_Info.query.all() + dic = {"filter":filter, "info":info} + return render_template('city_info.html', dic=dic) + + + + if __name__ == '__main__': + init_db() # 初始化数据库 app.run(host="0.0.0.0", port=8080, debug=True) \ No newline at end of file diff --git a/database.py b/database.py index e6bcb8f..d803b54 100644 --- a/database.py +++ b/database.py @@ -8,47 +8,30 @@ 2023/4/17 16:01 zart20 1.0 None ''' -# from flask import Flask -# from flask_sqlalchemy import SQLAlchemy -# -# app = Flask(__name__) -# -# user = "root" -# password = "123123" -# database = 'example' -# uri = "mysql+pymysql://%s:%s@127.0.0.1:3306/%s" % (user, password, database) -# print(uri) -# app.config['SQLALCHEMY_DATABASE_URI'] = uri -# app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False -# -# db = SQLAlchemy(app) -# -# class User(db.Model): -# __tablename__="user" -# id = db.Column(db.Integer, primary_key=True) -# name = db.Column(db.String(255)) -# age = db.Column(db.Integer) - - from sqlalchemy import create_engine from sqlalchemy.orm import scoped_session, sessionmaker from sqlalchemy.ext.declarative import declarative_base +# create_engine函数用于创建一个数据库引擎 (engine) 对象,该对象用于与底层数据库进行通信。 engine = create_engine('mysql+mysqldb://root:123123@127.0.0.1/example') + +# scoped_session 函数用于封装会话工厂,使其能够被多个线程共享。 +# 使用 scoped_session 可以保证每个线程都能使用同一个会话对象,从而避免了多个线程之间的会话冲突问题。 db_session = scoped_session( +# sessionmaker 函数中指定会话的参数,包括禁用自动提交 (autocommit=False) 和自动flush (autoflush=False)。 +# 这样可以在需要时手动控制 SQL 的提交和刷新机制。bind 参数指定了这个会话将要连接的数据库引擎对象。 sessionmaker( autoflush=False, autocommit=False, - bind=engine - ) -) - -Base = declarative_base() -Base.query = db_session.query_property() + bind=engine)) +Base = declarative_base() # `declarative_base` 是在 SQLAlchemy 中用于实现映射类 (ORM 类) 与表的映射关系的基类。 +Base.query = db_session.query_property() # 将会话工厂的 query 方法设置成了 Base 的属性 query,这样在定义映射类时可以直接使用 Base.query 来查询数据库。 def init_db(): - import models + # 创建了所有继承自 Base 的 ORM 类所映射的表,如果表已经存在,则跳过。 + # 这个函数将会自动找到所有继承自 Base 的 ORM 类,并生成对应的数据表。 + # 其中 bind=engine 表示使用上面定义的数据库引擎来执行 SQL 语句,实现将 ORM 类中的属性映射为数据表中的列及其属性。 Base.metadata.create_all(bind=engine) diff --git a/models.py b/models.py index 0505d49..17b2de2 100644 --- a/models.py +++ b/models.py @@ -8,8 +8,8 @@ 2023/4/17 16:43 zart20 1.0 None ''' -from sqlalchemy import Column, Integer, String -from database import Base +from sqlalchemy import Column, Integer, String, DateTime +from database import * class User(Base): __tablename__ = "users" @@ -22,4 +22,21 @@ class User(Base): self.email = email def __repr__(self): - return f'' \ No newline at end of file + # 有一个内置的函数叫 repr,它能把一个对象用字符串的形式表达出来以便辨认 + return f'' + + +class City_Info(Base): + __tablename__ = "city_info" + id = Column(Integer, primary_key=True) + province = Column(String(120),nullable=False) + cityname = Column(String(120),nullable=False) + usernumber = Column(Integer,default=0) + + def __init__(self, province=None, cityname=None, usernumber=None): + self.province = province + self.cityname = cityname + self.usernumber = usernumber + + def __repr__(self): + return f"" diff --git a/templates/city_info.html b/templates/city_info.html new file mode 100644 index 0000000..b5e0193 --- /dev/null +++ b/templates/city_info.html @@ -0,0 +1,30 @@ + + + + + 城市信息 + + + + + + + + + + + + + {% for row in dic.info %} + + + + + + + + {% endfor %} +
城市信息
{{ dic.filter.0 }}{{ dic.filter.1 }}{{ dic.filter.2 }}{{ dic.filter.3 }}
{{ row.province }}{{ row.cityname }}{{ row.usernumber }}{{ "添加 删除" }}
+ + + \ No newline at end of file