pip install flask
pip install flask_cors
pip install flask_SQLAlchemy
from flask import Flask, request, jsonify,session
from flask_cors import CORS
import json
import timedelta
# from sql import db
app = Flask(__name__)
#数据库中有 app = Flask(__name__)时 直接使用数据库中的app
# app = db.app
CORS(app, supports_credentials=True)
#app = db.app
app.secret_key="key not base63"
app.config['SESSION_COOKIE_NAME'] = 'session_key'
@app.route("/",methods = ["GET","POST"])
def hello_world():
get_data = request.get_json()
user = get_data.get("user")
password = get_data.get("password")
使用return来发送
return jsonify({"code": 0, "msg": "无可用model", "count": 0, "data": data})
if __name__ == '__main__':
app.run(host="0.0.0.0",part=5000)
这只不直接操作数据线而是使用flask_sqlalchemy来操作数据库
当数据库不存在,需要创建数据库的表可使用以下方法
若数据库已存在,只需连接数据库而不需创建表,可以使用另一方法,但两种方法只是细微的差别,可以先浏览以下方法:
头
# -*- coding:UTF-8 -*-
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import pymysql
app = Flask(__name__)
#连接mysql数据库,需要先存在database
#app.config["SQLALCHEMY_DATABASE_URI"]="mysql+pymsql://root:[email protected]:3306/demo"
#连接sqlite数据线,会自动创建数据库
app.config["SQLALCHEMY_DATABASE_URI"]="sqlite:///"+"/home/china/sqlite/demo.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"]=False
app.config["SECRET_KEY"]="whoami"
db =SQLAlchemy(app)
#火力平台
class platform(db.Model):
__tablename__ = "platform"
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(20))
croodes_x= db.Column(db.Float) # x坐标
croodes_y = db.Column(db.Float) # y坐标
status = db.Column(db.Integer) # 状态
desc = db.Column(db.String(100)) # 描述
#多对多关系关联 使用 ammoAllot作为中间表
#platform 通过platform.ammos 访问ammo表
#ammo表 通过 ammo.platforms 访问paltform表
ammos = db.relationship("ammo", secondary="ammoAllot", backref="platforms")
# 一对多/多对一关系关联
# platform 通过 platform.ammos_num来访问ammoAllot表
# ammoAllot 通过 ammoAllot.platform来访问platform表
ammos_num = db.relationship("ammoAllot",backref="platform")
#链接表+弹药分配/消耗
class ammoAllot(db.Model):
__tablename__ = "ammoAllot"
id = db.Column(db.Integer,primary_key=True)
#外键id 关联 platform 和 ammoAllot 两个表
platform_id = db.Column(db.Integer,db.ForeignKey("platform.id"))
#外键id 关联 ammo 和 ammoAllot 两个表
ammo_id = db.Column(db.Integer,db.ForeignKey("ammo.id"))
allot_num = db.Column(db.Integer) #分配数量
residue_num = db.Column(db.Integer) #剩余数量
#弹药
class ammo (db.Model):
__tablename__ = "ammo"
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(20))
max_distance = db.Column(db.Integer) #最远射程
min_distance = db.Column(db.Integer) #最短射程
desc = db.Column(db.String(100))
# 一对多/多对一关系关联
# ammo 通过 ammo.ammo_allots来访问ammoAllot表
# ammoAllot 通过 ammoAllot.ammo来访问ammo表
ammo_allots = db.relationship("ammoAllot",backref="ammo")
增
p = platform(name = '和谐',croodes_x = '10',croodes_y = '1',status = 0,desc = None)
a = ammo (name = '红星-8',max_distance = 1000,min_distance = 10,disc = None)
aa = ammoAllot(platform_id = 1,ammo_id = 1,allot_num = 24,residue_num = 12)
db.session.add(p)
db.session.add_all([a,aa])
db.session.commit()
查
q1=platform.query.get(1)
q2=platform.query.filter(platform.name == "和谐").first()
q3=platform.query.filter(platform.name == "和谐").all() #查询出来是一个列表
print(q1,q2)
for q in q3:
for a in q.ammos:
print(a.name)
for an in q.ammos_num:
print(an.allot_num)
改
q1=platform.query.filter(platform.name == "和谐").update({"desc":"东方快递,使命必达"})
db.session.commit()
q2 = platform.query.filter(platform.name == "和谐").all()
for q in q2:
q.desc= "东方快递,使命必达"
db.session.add_all(q2)
db.session.commit()
删
q2=platform.query.filter(platform.name == "和谐").delete()
db.session.commit()
头
from sqlalchemy import MetaData
from sqlalchemy import create_engine
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import sessionmaker
配置
engine = create_engine("mysql+pymysql://root:[email protected]:3306/demo")
Session = sessionmaker()
session = Session(bind=engine)
metadata = MetaData(bind=engine)
metadata.reflect(engine, schema="demo")
Base = automap_base(metadata=metadata)
Base.prepare()
导出类
ammo = Base.classes.ammo
platform = Base.classes.platform
ammoAllot = Base.classes.ammoAllot
数据库操作
#在这只举一个例子,可以参考上的数据库操作对应来
session.query(ammo).filter(
ammo.name =="红星-8"
)