diff --git a/app.py b/app.py index f0b0fda..6394dcf 100644 --- a/app.py +++ b/app.py @@ -13,6 +13,10 @@ from flask import Flask, render_template app = Flask(__name__) +app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:root@127.0.0.1/example' + + + @app.route("/") def hello(): return "

hello

" diff --git a/database.py b/database.py new file mode 100644 index 0000000..e6bcb8f --- /dev/null +++ b/database.py @@ -0,0 +1,55 @@ +# -*- encoding: utf-8 -*- +''' +@File : models.py +@License : (C)Copyright 2018-2022 + +@Modify Time @Author @Version @Desciption +------------ ------- -------- ----------- +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 + +engine = create_engine('mysql+mysqldb://root:123123@127.0.0.1/example') +db_session = scoped_session( + sessionmaker( + autoflush=False, + autocommit=False, + bind=engine + ) +) + +Base = declarative_base() +Base.query = db_session.query_property() + + +def init_db(): + import models + Base.metadata.create_all(bind=engine) + + + diff --git a/models.py b/models.py new file mode 100644 index 0000000..0505d49 --- /dev/null +++ b/models.py @@ -0,0 +1,25 @@ +# -*- encoding: utf-8 -*- +''' +@File : models.py +@License : (C)Copyright 2018-2022 + +@Modify Time @Author @Version @Desciption +------------ ------- -------- ----------- +2023/4/17 16:43 zart20 1.0 None +''' + +from sqlalchemy import Column, Integer, String +from database import Base + +class User(Base): + __tablename__ = "users" + id = Column(Integer, primary_key=True) + name = Column(String(50), unique=True) + email = Column(String(120), unique=True) + + def __init__(self, name=None, email=None): + self.name = name + self.email = email + + def __repr__(self): + return f'' \ No newline at end of file