连接mysql(建表和删表)

from   sqlalchemy.ext.declarative  import  declarative_base##拿到父类
from sqlalchemy import Column##拿到字段
from sqlalchemy import create_engine ##创建引擎
from sqlalchemy import text,String,ForeignKey,Integer
from sqlalchemy.orm import relationship


##拿到表的父类对象
base=declarative_base()

######创建表
class depart(base):
__tablename__='depart'

id=Column(Integer,primary_key=True)
title=Column(Integer,primary_key=True)



class user(base):
__tablename__='user'

id=Column(Integer,primary_key=True)
name=Column(String,unique=True,nullable=False)
depart_id=Column(Integer,ForeignKey('depart.id'))

dp=relationship('Depart',backref='pers')##关联那个表,不会在数据库创建

###进行操作

def create_all():
engine=create_engine(
'mysql+pymysql://root:192855wang,,[email protected]:3306/orm4?charset=utf8',
max_overflow=0,##超过连接池最多创建的连接
pool_size=5,##连接池大小
pool_timeout=30,###池中没有线程最多等待的时间,否则报错
pool_recycle=-1,##多久之后对线程传进行操作

)


def drop_all():
engine = create_engine(
'mysql+pymysql://root:192855wang,,[email protected]:3306/orm4?charset=utf8',
max_overflow=0, ##超过连接池最多创建的连接
pool_size=5, ##连接池大小
pool_timeout=30, ###池中没有线程最多等待的时间,否则报错
pool_recycle=-1, ##多久之后对线程传进行操作

)

if __name__ == '__main__':
create_all()

猜你喜欢

转载自www.cnblogs.com/yunxintryyoubest/p/9986862.html