加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

sqlalchemy学习笔记

发布时间:2020-12-12 20:37:36 所属栏目:百科 来源:网络整理
导读:engine = create_engine('sqlite://')# create MetaDatameta = MetaData()# bind to an enginemeta.bind = engine create_all()creates foreign key constraints between tables usually inline with the table definition itself,and for this reason it al

engine = create_engine('sqlite://')

# create MetaData
meta = MetaData()

# bind to an engine
meta.bind = engine

create_all()creates foreign key constraints between tables usually inline with the table definition itself,and for this reason it also generates the tables in order of their dependency. There are options to change this behavior such that ALTERTABLEis used instead.


用create_all的时候会根据外键的依赖性,顺序创建表。删除的时候亦然。

绑定一个数据库:

engine = create_engine('sqlite:///:memory:')

meta = MetaData()

employees = Table('employees',meta,Column('employee_id',Integer,primary_key=True),Column('employee_name',String(60),nullable=False,key='name'),Column('employee_dept',ForeignKey("departments.department_id"))
)
SQLemployees.create(engine)

连接池设置:

create_engine()大多时候会集成一个QueuePool。

在用sqlite的时候,会用SingletonThreadPoolorNullPool 因为sqlite的锁机制。

链接只会在用到的时候建立.

可以用poolclass参数指定pool的类型:

from sqlalchemy.pool import QueuePool
engine = create_engine('sqlite:///file.db',poolclass=QueuePool)

参数为 Nullpoll的时候禁用连接池。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读