python SQLAlchemy数据库操作
生活随笔
收集整理的這篇文章主要介紹了
python SQLAlchemy数据库操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1 增
- 2 刪
- 3 改
- 4 基礎查詢
- 4.1 基礎查詢匯總
- 4.2 限制查詢
- 4.3 計數
- 4.4 去重
- 4.5 排序
- 4.6 模糊查詢
- 4.7 分組查詢
- 4.8 聚合函數
- 5 多表查詢
- 5.1 內連接
- 5.2 左外連接
- 5.3 復雜查詢
- 5.4 三表連接
- 5.5 利用SQLAlchemy執行原生SQL
- 6 原生語句查詢 返回json格式
- 3 統計數量
1 增
Customer為表名
2 刪
# 刪除 @route('/professionDeleteType/<num>') def professionDeleteType(db,num):print(num)try:print('--------刪除--------------')print(num)TypeNameInfo = db.query(Profession).filter(Profession.pid==int(num)).delete()db.commit() # 提交即保存到數據庫# db.close() # 關閉會話except Exception as e:print(e)return redirect('/profession')3 改
Users表
4 基礎查詢
4.1 基礎查詢匯總
# select * from users; result = db.query(Users).all() # select uid, username from users; result = db.query(Users.uid, Users.username).all() # select * from users where uid=17 and username='666'; result = db.query(Users).filter_by(uid=17, username='666').all() # select * from users where uid>=16 or username='666'; result = db.query(Users).filter(or_(Users.uid >= 16,Users.username == '666' )).all()4.2 限制查詢
# 查詢前3行 result = db.query(Users).limit(3).all() # select * from users limit 3,5; result = db.query(Users).limit(3).offset(3).all()limit:查詢多少條
offset:偏移量 -> 3 表示從第4條開始
4.3 計數
select count(*) from users where ...filter:== >= < > != != in not
count = db.query(Users).filter(Users.uid > 16).count() print(count)4.4 去重
select distinct(openid) from users; result = db.query(Users.openid).distinct(Users.openid).all() print(result) for row in result:print(row.openid)4.5 排序
select * from users order by uid desc; result = db.query(Users).order_by(Users.uid.desc()).all()4.6 模糊查詢
select * from users where username like '%929%'; result = db.query(Users).filter(Users.username.like('%929%')).all()4.7 分組查詢
select * from users group by isAdmin; result = db.query(Users).group_by(Users.openid).all() result = db.query(Users).group_by(Users.openid).having(Users.uid > 2).all()4.8 聚合函數
min、max、avg、sum
select sum(u_dealNum) from users; result = db.query(func.sum(Users.u_dealNum)).first() print(result)5 多表查詢
5.1 內連接
查詢發表過文章的 用戶信息+文章信息
select a_title from users, article where users.uid=article.uid and article.a_id=1; select a_title from article inner join users on users.uid=article.uid where article.a_id=1; # 多表查詢時,返回的結果集不再是單純的[Model, Model]數據結構,而是每張表的結果有獨立的對象來維護 result = db.query(Article, Users).join(Users, Article.uid == Users.uid).filter(Article.a_id == 1).all() print(result) for article, users in result:print(article.uid, article.a_title, users.username) result = db.query(Article, Users.username).join(Users, Article.uid == Users.uid).filter(Article.a_id == 1).all() print(result) for article, username in result:print(article.uid, article.a_title, username)5.2 左外連接
查詢所有用戶 寫的文章閱讀量
select nickname,a_title,sum(a_pageviews) as total from users left join article on users.uid=article.uid group by users.uid having total>0; result = db.query(Users.uid, Users.nickname, func.sum(Article.a_pageviews)).outerjoin(Article, Users.uid == Article.uid).group_by(Users.uid).all()5.3 復雜查詢
and和or混用: username like 'charles' or uid>1 and nickname='導演'
result = db.query(Users).filter(or_(Users.username.like('charles'), and_(Users.uid > 1, Users.nickname == "導演"))).all() result = db.query(Users).filter(Users.username.like('charles'), or_(Users.uid > 1, Users.nickname == "導演")).all() print(result) for row in result:print(row.uid, row.username)5.4 三表連接
result = db.query(Article, Users).join(Users, Article.uid == Users.uid) \.join(Label, Article.l_id == Label.l_id).all()5.5 利用SQLAlchemy執行原生SQL
result = db.execute("select * from users").fetchall() print(result)6 原生語句查詢 返回json格式
try:sql = ''Info = db.execute(sql)print(Info) except Exception as e:db.rollback()print(e) results = [] for i in searchs:bid = i['bid'] # 索引isbn = i['isbn'] # isbnprint(bid)print(isbn)result = {"bid": bid,"isbn": isbn,}print(result)results.append(result)data = {"status": '1',"results": results } return json.dumps(data)3 統計數量
from sqlalchemy import func older_num = db.query(func.count(Order_history.seller)).filter_by(seller=uname).scalar()Sqlchemy查詢結果轉json
MySQL 查詢統計 日期查詢
總結
以上是生活随笔為你收集整理的python SQLAlchemy数据库操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 易支付sdk
- 下一篇: bottle中文文档