SQLAlchemy操作MariaDB筆記之三

2021-07-05 12:14:14 字數 1701 閱讀 6897

query方法返回的是乙個可迭代輸出的物件,可以索引和切片。引數可以為表物件,表的字段物件。

>>> for instance in session.query(user).order_by(user.id): 

... print instance.name, instance.fullname

>>> for name, fullname in session.query(user.name, user.fullname): 

... print name, fullname

給表物件起別名,同時可以指定欄位的別名

>>> from sqlalchemy.orm import aliased

>>> user_alias = aliased(user, name='user_alias')

sql>>> for row in session.query(user_alias, user_alias.name).all():

... print row.user_alias

fillter_by()方法相當於where子句;order_by

方法相當於orderby子句;

query.filter(user.name==『ed』)

query.filter(user.name!=『ed』)

like

query.filter(user.name.like(『%ed%』))

in

query.filter(user.name.in_([『ed』, 『wendy』, 『jack』]))

從句

query.filter(user.name.in_(

session.query(user.name).filter(user.name.like(『%ed%』))

))not in

query.filter(~user.name.in_([『ed』, 『wendy』, 『jack』]))

is null

query.filter(user.name == none)

and

from sqlalchemy import and_

query.filter(and_(user.name == 『ed』, user.fullname == 『ed jones』))

query.filter(user.name == 『ed』, user.fullname == 『ed jones』)

or

query.filter(user.name == 『ed』).filter(user.fullname == 『ed jones』)

from sqlalchemy import or_

query.filter(or_(user.name == 『ed』, user.name == 『wendy』))

match

query.filter(user.name.match(『wendy』))

sqlalchemy基本操作

sqlalchemy的簡單實用 其他比較好的人寫的 各種基本操作 sqlalchemy指南 常用技巧 usr bin env python coding utf 8 from sqlalchemy import column,string,create engine,metadata from sq...

sqlAlchemy例項操作

from sqlalchemy import create engine from sqlalchemy.ext.declarative import declarative base from sqlalchemy import column,string,integer,float from s...

SQLAlchemy基礎操作二

usr bin env python coding utf 8 import time import threading from sqlalchemy.ext.declarative import declarative base from sqlalchemy import column,int...