File size: 765 Bytes
1b5b024
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app =  Flask(__name__)

# 数据库的变量
HOST = '127.0.0.1'
PORT = 3306
DATA_BASE = 'flask_db'
USER = 'root'
PWD = 'zxydsg123'
DB_URI = f'mysql+pymysql://{USER}:{PWD}@{HOST}:{PORT}/{DATA_BASE}'

app.config['SQLALCHEMY_DATABASE_URI'] = DB_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db =  SQLAlchemy(app)

# 创建模型类
class User(db.Model):
    __tablename__ = 't_user'
    id = db.Column(db.Integer,primary_key = True,autoincrement = True)
    name = db.Column(db.String(32))
    age = db.Column(db.Integer)
    city = db.Column(db.String(32))

    def __repr__(self):
        return f'<User id={self.id}  name={self.name}>'

from flask_migrate import Migrate

Migrate(app, db)