sqlalchemy学习笔记

前端之家收集整理的这篇文章主要介绍了sqlalchemy学习笔记前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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的时候禁用连接池。

猜你在找的Sqlite相关文章