<span style="font-size:18px;">from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy</span>
<span style="font-size:18px;">mysqlname='<span style="color: rgb(230, 219, 116); font-family: 'Source Code Pro'; font-size: 13pt; background-color: rgb(39, 40, 34);">mysql://user:passwd@127.0.0.1/student?charset=utf8</span>'</span>
<span style="font-size:18px;">app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = mysqlname db = SQLAlchemy(app)</span>
<span style="font-size:18px;">class User(db.Model):
"""存储 每种报警类型的数量 , 以 分钟 为单位进行统计
:param source: string ,报警来源
:param network_logic_area: string ,该报警所属的逻辑网络区域
:param start_time: datetime , 报警发生时间
"""
__tablename__ = 'hello'
id = db.Column(db.Integer , primary_key = True)
source = db.Column(db.String(255) )
network_logic_area = db.Column(db.String(255) )
start_time = db.Column(db.DateTime)
count = db.Column(db.Integer)
def __init__(self , source , network_logic_area , start_time , count):
self.source = source
self.network_logic_area = network_logic_area
self.start_time = start_time
self.count = count
def alter(self):
self.count += 1;</span>
<span style="font-size:18px;"> p = User(........)
db.session.add(p)
db.session.commit()</span>
User.query.get(1) <User u'admin'>
peter = User.query.filter_by(username='peter').first() #注意:精确查询函数query.filter_by(),是通过传递参数进行查询;其他增强型查询函数是query.filter(),通过传递表达式进行查询。 print(peter.id) #如果数据不存在则返回None
User.query.filter(User.email.endswith('@example.com')).all()
[<User
u'admin'>,
<User u'guest'>]
peter = User.query.filter(User.username != 'peter').first() print(peter.id)
from sqlalchemy import not_ peter = User.query.filter(not_(User.username=='peter')).first() print(peter.id)
from
sqlalchemy import
and_
peter
=
User.query.filter(and_(User.username=='peter',
User.email.endswith('@example.com'))).first()
print(peter.id)
from
sqlalchemy import
or_
peter
=
User.query.filter(or_(User.username
!=
'peter',
User.email.endswith('@example.com'))).first()
print(peter.id)
<span style="font-size:18px;">insert_table.db.engine.execute(' ..... ')</span>
<span style="font-size:18px;">me = User(........)</span>
<span style="font-size:18px;">db.session.delete(me) db.session.commit()</span>
Code example: u = User.query.first() u.username = 'guest' #更新数据和变量赋值那么简单,但必须是通过查询返回的对象。 db.session.commit()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2026 源码网商城 (www.ymwmall.com) 版权所有