在Python的Flask框架下使用sqlalchemy库的简单教程

Valonia ·
更新时间:2024-09-21
· 606 次阅读

flask中的sqlalchemy 相比于sqlalchemy封装的更加彻底一些 , 在一些方法上更简单

首先import类库:

在CODE上查看代码片派生到我的代码片

<span style="font-size:18px;">from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy</span>


然后,需要加载 数据库路径

在CODE上查看代码片派生到我的代码片

<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>

在CODE上查看代码片派生到我的代码片

<span style="font-size:18px;">app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = mysqlname db = SQLAlchemy(app)</span>


通过前面两步 ,我们已经让flask和数据库联系到了一起

下面我们要把 flask和具体的表联系在一起、

这样建立一个model模型

在CODE上查看代码片派生到我的代码片

<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>

上面这个代码,就让falsk和具体的表hello联系在了一起

在这个类中 ,我们首先要指定表,然后把这个表中的列都列出来,最后定义一个 初始化函数 , 让后面插入数据使用


现在开始具体的数据库操作:

1、insert

在CODE上查看代码片派生到我的代码片

<span style="font-size:18px;"> p = User(........) db.session.add(p) db.session.commit()</span>

通过 类User构造了一条数据

2、find

用主键获取数据:
Code example:

User.query.get(1) <User u'admin'>

通过一个精确参数进行反查:
Code example:

peter = User.query.filter_by(username='peter').first() #注意:精确查询函数query.filter_by(),是通过传递参数进行查询;其他增强型查询函数是query.filter(),通过传递表达式进行查询。 print(peter.id) #如果数据不存在则返回None

模糊查询:
Code example:
 

User.query.filter(User.email.endswith('@example.com')).all() [<User u'admin'>, <User u'guest'>]

逻辑非1:
Code example:
 

peter = User.query.filter(User.username != 'peter').first() print(peter.id)

逻辑非2:
Code example:
 

from sqlalchemy import not_ peter = User.query.filter(not_(User.username=='peter')).first() print(peter.id)

逻辑与:
Code example:

from sqlalchemy import and_ peter = User.query.filter(and_(User.username=='peter', User.email.endswith('@example.com'))).first() print(peter.id)

逻辑或:
Code example:

from sqlalchemy import or_ peter = User.query.filter(or_(User.username != 'peter', User.email.endswith('@example.com'))).first() print(peter.id)

filter_by:这个里面只能放具体放入条件,不能放一个复杂的计算 ,

filter: 这个里面可以放一些复杂的计算

.first:取第一条数据

.all:取出所有数据

还有一个其他的方法,可以进行排序、计数之类的操作

3、使用sql语句

可以通过 前面构造的 db 直接使用sql的原生语句

在CODE上查看代码片派生到我的代码片

<span style="font-size:18px;">insert_table.db.engine.execute(' ..... ')</span>


4、delete

在CODE上查看代码片派生到我的代码片

<span style="font-size:18px;">me = User(........)</span>

在CODE上查看代码片派生到我的代码片

<span style="font-size:18px;">db.session.delete(me) db.session.commit()</span>

5、更新数据

Code example: u = User.query.first() u.username = 'guest' #更新数据和变量赋值那么简单,但必须是通过查询返回的对象。 db.session.commit() 您可能感兴趣的文章:Python的Flask框架中使用Flask-SQLAlchemy管理数据库的教程在Python程序和Flask框架中使用SQLAlchemy的教程Python利用flask sqlalchemy实现分页效果Python的Flask框架中SQLAlchemy使用时的乱码问题解决Python的Flask框架与数据库连接的教程Python的Flask框架中使用Flask-Migrate扩展迁移数据库的教程Python框架Flask的基本数据库操作方法分析Python使用Flask-SQLAlchemy连接数据库操作示例



sqlalchemy flask Python 教程

需要 登录 后方可回复, 如果你还没有账号请 注册新账号