Example #1
0
 def drop_tb(self, base):
     status = self.db_status_chk(base)
     if status == 4:
         tables = [base.metadata.tables[table_name(base)]]
         base.metadata.drop_all(self._engine, tables)
         print('数据表删除成功')
     elif status == 2:
         print('数据表不存在!')
Example #2
0
 def crt_tb(self, base):
     status = self.db_status_chk(base)
     if status == 2:
         # 创建指定表
         tables = [base.metadata.tables[table_name(base)]]
         base.metadata.create_all(self._engine, tables)
         print('数据表创建成功!')
     elif status == 4:
         print('数据表已存在!')
Example #3
0
def drop_tb(base, engine):
    """
   判断数据库是否存在,表是否已存在。
   :param base: 待创建表
   :param engine: 数据库引擎参数
   """
    try:
        metadata = MetaData(engine)
        # 通过反射,判断数据库与数据表是否存在
        Table(table_name(base), metadata, autoload=True)
    except Exception as e:
        if re.search('Unknown database', str(e)):
            print('数据库不存在!')

        elif str(e) == f'`{table_name(base)}`':
            print('数据表不存在!')
        else:
            print(e)  # 未知错误
    else:
        tables = [base.metadata.tables[table_name(base)]]
        base.metadata.drop_all(engine, tables)
        print('数据表删除成功!')
Example #4
0
 def db_status_chk(self, base):
     """
     检查数据库是否存在,数据表是否存在
     """
     pass
     try:
         metadata = MetaData(self._engine)
         # 通过反射,判断表是否已存在
         Table(table_name(base), metadata, autoload=True)
     except Exception as ex:
         if re.search('Unknown database', str(ex)):
             print('数据库不存在!')
             return 1
         elif str(ex) == f'`{table_name(base)}`':  # 数据表不存在时,返回的是表名
             return 2
         else:
             print(ex)  # 其它未知异常输出
             return 3
     else:
         # 数据表已存在
         return 4
Example #5
0
 def test_table_name(self):
     assert table_name(self.Building) == 'building'
     del self.Building.__tablename__
     assert table_name(self.Building) == 'building'
Example #6
0
 def test_target(self, Building):
     assert table_name(Building()) == 'building'
Example #7
0
 def test_attribute(self, Building):
     assert table_name(Building.id) == 'building'
     assert table_name(Building.name) == 'building'
Example #8
0
 def test_class(self, Building):
     assert table_name(Building) == 'building'
     del Building.__tablename__
     assert table_name(Building) == 'building'
Example #9
0
 def test_target(self):
     assert table_name(self.Building()) == 'building'
Example #10
0
 def test_attribute(self):
     assert table_name(self.Building.id) == 'building'
     assert table_name(self.Building.name) == 'building'
 def test_target(self, Building):
     assert table_name(Building()) == 'building'
 def test_class(self, Building):
     assert table_name(Building) == 'building'
     del Building.__tablename__
     assert table_name(Building) == 'building'
 def test_target(self):
     assert table_name(self.Building()) == 'building'