SQLalchemy模块用法

安装
pip install sqlalchemy

  

#!/usr/bin/env python
# -*- coding:utf-8 -*-

# 加载模块
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine

# 连接请求
engine = create_engine("mysql+mysqldb://root:123@127.0.0.1:3306/s11", max_overflow=5)
# 实例化
Base = declarative_base() class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String(50)) # 寻找Base的所有子类,按照子类的结构在数据库中生成对应的数据表信息
# Base.metadata.create_all(engine) Session = sessionmaker(bind=engine)
session = Session() # ########## 增 ##########
# u = User(id=2, name='sb')
# session.add(u)
# session.add_all([
# User(id=3, name='sb'),
# User(id=4, name='sb')
# ])
# session.commit() # ########## 删除 ##########
# session.query(User).filter(User.id > 2).delete()
# session.commit() # ########## 修改 ##########
# session.query(User).filter(User.id > 2).update({'cluster_id' : 0})
# session.commit()
# ########## 查 ##########
# ret = session.query(User).filter_by(name='sb').first() # ret = session.query(User).filter_by(name='sb').all()
# print ret # ret = session.query(User).filter(User.name.in_(['sb','bb'])).all()
# print ret # ret = session.query(User.name.label('name_label')).all()
# print ret,type(ret) # ret = session.query(User).order_by(User.id).all()
# print ret # ret = session.query(User).order_by(User.id)[1:3]
# print ret
# session.commit()

  

上一篇:linux删除或隐藏命令历史记录history


下一篇:https_request请求接口返回数据