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

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造

數組的同質性對性能的影響是雙重的:1)同質性允許編譯器優化內存訪問,提高性能;2)但限制了類型多樣性,可能導致效率低下。總之,選擇合適的數據結構至關重要。

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

記事本++7.3.1
好用且免費的程式碼編輯器

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器