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的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
