怎么在Python中使用瓶动态创建表

  介绍

这篇文章将为大家详细讲解有关怎么在Python中使用瓶动态创建表,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

<强>代码如下:

得到app_factory  import 应用   得到sqlalchemy  import 列,,字符串,整数   ,   class  ProjectModel (app.db.model, app.db.Mixin):   ,   时间=tablename 才能;& # 39;Project_& # 39;   ,   ID 才能=,列(字符串(50),name=& # 39; id # 39;,,医生=& # 39;id # 39;)   PROJECTNUMBER 才能=,列(字符串(100),name=& # 39; PROJECTNUMBER& # 39;,,医生=& # 39;项目编号& # 39;)   ,……   ,   ,   @staticmethod才能   def 才能create_table (project_number)   ,,,table_name =, ProjectModel.tablename  + projectnumber   ,,,structs =, (   ,,,,,{& # 39;fieldname # 39;:, & # 39; id # 39;,, & # 39;类型# 39;:,& # 39;varchar2(50) & # 39;,, & # 39;主要# 39;,,真的,,& # 39;默认# 39;:,& # 39;& # 39;},   ,,,,,{& # 39;fieldname # 39;:, & # 39; PROJECTNUMBER& # 39;,, & # 39;类型# 39;:,& # 39;varchar2(50) & # 39;,, & # 39;默认# 39;:,0,,& # 39;isnull& # 39;:,,,,,,,,,   ,,,,,,,真},   ,,,,.......   ,,,)   ,,,app.db.create_table (table_name,结构体)

那么,内层函数是如何创建的呢?其实就是拼接sql语句....创建表

代码如下:

class  SQLAlchemyDB (SQLAlchemy):   def 才能;__init__(自我,,应用)   ,,,超级(SQLAlchemyDB,,自我). __init__(应用)   ,,,self.__app =,应用   ,,,self.engine.echo =False   ,,,self.conn =, self.engine.connect ()   ,,,self.Model.to_dict (),=, lambda 自我:{c.name: getattr(自我,,c.name,,也没有),for  c 拷贝self.__table__.columns}   ,,,self.Session =, sessionmaker(绑定=self.engine)   ,,,self.ScopedSession =,λ:,scoped_session (self.Session)   ,   #,才能释放碎片空间   def 才能free_idle_space(自我):   ,,,return  self.execute (& # 39; purge  recyclebin& # 39;)   ,,   def 才能connstatus(自我):   ,,,return  self.engine.pool.status ()   ,,   def 才能关闭(自我):   ,,,self.conn.close ()   ,,,self.engine.dispose ()   ,   #才能,非返回数据的记录语句   def 才能执行(自我,sqlexpr)   ,,,试一试:   ,,,,,ret =, self.conn.execute (sqlalchemy.text (sqlexpr))   ,,,except  Exception  as 错:   ,,,,,return 假,,str (err)   ,,,except  sqlalchemy.exc.InvalidRequestError  as 错:   ,,,,,return 假,,str (err)   ,   ,,,return 的确,,& # 39;& # 39;   ,   #,才能动态拼接sql语句,,创建表   def 才能;create_table(自我,,,,结构):   ,,,fieldinfos =, []   ,,,for  struct 拷贝结构:   ,,,,,defaultvalue =, struct.get(& # 39;默认# 39;)   ,,,,,if  defaultvalue :   ,,,,,,,defaultvalue =,“& # 39; {0} & # 39;“.format (defaultvalue), if 类型(defaultvalue),==, & # 39; str # 39;, else  str (defaultvalue)   ,,,,,fieldinfos.append (& # 39; {0}, {1}, {2}, {3}, {4} & # 39; .format (struct [& # 39; fieldname # 39;],, struct[& # 39;类型# 39;],,& # 39;primary 关键# 39;,if  struct.get(& # 39;主要# 39;),else  & # 39; & # 39;,,(& # 39;默认# 39;,+,defaultvalue), if  defaultvalue  else  & # 39; & # 39;,, & # 39; & # 39;, if  struct.get (& # 39; isnull& # 39;), else  & # 39; not 零# 39;))   ,,,sql =, & # 39; create  table  {0}, {1}) & # 39; .format(表,& # 39;& # 39;. join (fieldinfos))   ,,,受潮湿腐烂,,err =, self.execute (sql)   ,,,if 随著:   ,,,,,self.__app.sync_record(表名,,& # 39;sql_createtable& # 39;,, {},, sql)   ,,,return  ret,犯错   ,   #,才能动态判断表是否存在,   def 才能existtable(自我,,表名):   ,,,受潮湿腐烂,,err =, self.GetRecordCount (“user_all_table",,“TABLE_NAME=& # 39;“, +, tablename  +“& # 39;“)   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在Python中使用瓶动态创建表