python操作mysql(pymysql,pandas,numpy, C++)类封装测试v1.4 (tcy)

python操作mysql(pymysql,pandas,numpy, C++)类封装测试v1.4 (tcy)

本项目主要完成类的封装。

项目目的:
   通过python 操作mysql数据库

项目内容:
   1.pymysql操作mysql数据库。(适用对读写速度要求不高的场所 ,数据量中等。)
   2.pandas,numpy操作mysql数据库。(较快的数据读写,数据量适中)
   3.C++操作mysql数据库(最优也最难)

目前完成项目:
    数据库,表的封装        v1.0 完成      2019/1/20
    数据列属性的设置与修改  v1.1 完成 (添加支持返回类型可选list or dict)
    索引键自增             v1.2 完成      2019/2/27

    插入数据               v1.3 完成      2019/3/1
    查询                   v1.4 完成        2019/3/3
    
    pandas,numpy封装      v0.1 onging
    c++封装                v0.1 ng   

封装进行中。。。。。。       欢迎大家分享,修改,指正。

    以上完成的经过全部测试,纯python环境;
    文档内容包括类目录,类测试,类原代码。

   懒人的办法,你不用再去记pymysql及mysql的指令,直接封装完成,直接调用。

说明:
1.上面是测试程序(约400行)
2.下面是类封装(目前约1200行,预计封装完成在2000行)
函数目录

#========================================================================
def __init__(self, is_SSDictCurse=False, **config):
def __del__(self):  
def __connect_db(self, is_SSDictCurse=False, **config):
def __close_connect(self):
def __run(self, sql='', commit=False, fetchall=True, backvalue=True, 
          executemany=False, args=None,back_lst=False):
#========================================================================
def show_db_name_current(self):  
def get_db_name_current(self):
def is_exist_db_current(self, db_name):
def show_db_name_all(self): 
def get_db_names(self):
def is_exist_db(self, db_name):
def create_db(self, db_name, charset='UTF8MB4'):
def add_db(self, db_name, charset='UTF8MB4'):
def drop_db(self, db_name):
def del_db(self, db_name):
def select_db(self, db_name):
#=========================================================================
# 2.表的操作
def __SHOW_TB_NAMES(self, db_name='', tb_name=''):  # 表名更新,但是表行数无实时更新
def __SHOW_CREATE_TABLE(self, tb_name):
    
def show_tb_names(self, db_name=''):  # 2019/3/3  
def get_tb_names(self, db_name=''):   # 2019/3/3
def is_exist_table(self, tb_name):
def show_tb_rows(self, db_name=''):  # 返回字典 2019/3/3
def get_tb_rows(self, db_name=''):   # 返回字典2019/3/3
def get_tb_n(self, tb_name='', db_name=''):  # 2019/3/3
def __alter_table_name(self, tb_old_name, tb_new_name):
def rename_table(self, tb_old_name, tb_new_name):
def create_table(self, tb_name, table_options={}):
def add_table(self, tb_name, table_options):
def drop_table(self, tb_name): 
def del_table(self, tb_name):
def copy_table(self, tb_old_name, tb_new_name, copy_data=False):
def del_table_datas(self, tb_name, fast_del=True):  # 保留字段
def clear_table_datas(self, tb_name, fast_del=True):  # 保留字段
#========================================================================
def __SHOW_FULL_COLUMNS(self, tb_name):
def __DESC(self, tb_name, col_name=''):
def __MODIFY_COL(self, tb_name, col_name='', col_options=''):
def __CHANGE_COL(self, tb_name, col_name='', col_new_name='', col_options=''):
def change_col(self, tb_name, col_name='', col_options='', sql_exist_col_name=False):
def __COLUMNS_DICT(self, tb_name, col_name=''):   
def __COLUMNS_LIST(self, tb_name, col_name=''):  
def COLUMNS(self, tb_name, col_name='', back_dict_dict=False):
    
def get_col_name(self, tb_name, col_name):
def rename_col(self, tb_name, col_name='', col_new_name=''):
    
def add_cols(self, tb_name, cols_options=''):
def del_cols(self, tb_name, cols_names=''):
def get_cols_names(self, tb_name):
    
def get_col_order_str(self, tb_name, col_name):
    
def __IS_EXIST_COLS(self, tb_name, cols_names='', cols_options='', all_cols=False):
def IS_EXIST_COL(self, tb_name, cols_names='', cols_options='', all_cols=False):
def is_exist_col(self, tb_name, col_name=''):
def any_exist_cols(self, tb_name, cols_names='', cols_options=''):
def all_exist_cols(self, tb_name, cols_names='', cols_options=''):

def get_col_type(self, tb_name, col_name):
def get_cols_types(self, tb_name):
def GET_DATA_TYPES(self, tb_name, col_name=None):
    
def add_AI(self, tb_name, col_name):
def get_AI(self, tb_name):
def get_auto_increment_name(self, tb_name):
def del_AI(self, tb_name):
def is_exist_AI(self, tb_name, col_name=''):
def get_last_AI_VALUE(self, tb_name):
def set_AI(self, tb_name, col_name):

def set_PRI(self, tb_name, pri_cols_names='', replace=False):
def add_primary_key(self, tb_name, pri_cols_names='', replace=False):
def get_PRI(self, tb_name):
def del_PRI(self, tb_name):
def del_primary_key(self, tb_name):
def drop_primary_key(self, tb_name):
def is_exist_PRI(self, tb_name, col_name=''):
    
def set_UNI(self, tb_name, col_name, index_name=None):
def del_UNI(self, tb_name, index_name='', col_name=''):
def get_UNI(self, tb_name, col_name=''):
def get_UNI_NAME(self, tb_name, col_name='', back_UNI_Dict=True):
def is_exist_UNI(self, tb_name, col_name=''):
     
def set_index(self, tb_name, index_name, cols_names='', unique_idx=False):
def __SHOW_INDEXS(self, tb_name, col_name='', back_dict_list=False, include_PRI=True):
def get_indexs(self, tb_name, back_dict_list=False, include_PRI=True):
def get_index(self, tb_name, col_name, include_PRI=True):
def __DEL_INDEX(self, tb_name, index_name=''):
def del_index(self, tb_name, index_name='', col_name=''):
def is_exist_index(self, tb_name, col_name='', include_PRI=True):
    
def __get_key(self, tb_name):
def get_key(self, tb_name):
                      
def alter_default_value(self, tb_name, col_name='', default_value=''):
def set_default_value(self, tb_name, col_name='', default_value=''):
def change_default_value(self, tb_name, col_name='', default_value=''):
def drop_default_value(self, tb_name, col_name=''):
def del_default_value(self, tb_name, col_name=''):
def get_default_values(self, tb_name, col_name='', back_dict_dict=False):
#=========================================================================
def insert_data(self, tb_name, cols_names='', cols_values=[[]], repeat_value=False,
                UpdataCols='', UpdataValues='', priority='', include_AI=True):
    
def insert_select_data(self, tb_name, cols_names='', SELECT='', repeat_value=False,
                       UpdataCols='', UpdataValues='', include_AI=True, priority=''): 
def update_data(self, tb_name, cols_names='', values='', where_condition='',
                limit_n=0, priority=''):  
def del_data(self, tb_name, where_condition='', order_by_cols_desc='', 
             limit_n=0, priority=''): 
# =========================================================================
def select(self, cols_names='', tb_names='', where='', group_by='',
           having='', limit=(), order_by_desc=False, order_by_cols='',
           distinct=False, back_list=False):
#==========================================================================    
测试代码

# if __name__ == "__main__":
import py_mysql_class
#1. 定义数据库访问参数

config = {
            'host': '127.0.0.1',#你的mysql服务器IP地址
            'port': 3306,'user': 'root','password': 'root',#'mysql服务器root密码
            'charset': '','autocommit': False ,'database': None
         }

# 初始化打开数据库连接
db = py_mysql_class.PyMySqlClass(is_SSDictCurse=False,**config)

#===============================================================================
#===============================================================================
#1.测试数据库
db1 = 'new_futures'#数据库存在
db2='new_futures33'#数据库不存在

def test_db():
    print('11.exist_db=',db.is_exist_db(db1),db.is_exist_db(db2),db.is_exist_db_current(db1))#True False False
    db.select_db(db1)
    print('12.current_db=',db.is_exist_db_current(db1))#True
    db.create_db(db2)
    print('13.creat new db=',db.is_exist_db(db2))#True
    db.drop_db(db2)
    print('14.db2 not exist=', db.is_exist_db(db2))#False
    print('15.show current db=', db.show_db_name_current(),db.get_db_name_current())#new_futures
    print('16.show all db=', db.show_db_name_all(),db.get_db_names())#True
'''
16.show all db= ['db1', 'information_schema', 'mysql', 'new_futures', 
        'new_schema', 'performance_schema', 'sakila', 'sys', 'world']
'''
# test_db()
#===============================================================================
#===============================================================================
#2.数据表测试

db1 = 'new_futures'#数据库存在
tb_name = 'm2009'
tb_old_name = 'm2010'
tb_new_name1 = 'm2011'
tb_new_name2='m2012'
tb_new_name3='m2013'

db.select_db(db1)
def test_tb():
    # 2.1显示数据
    d= {'id':'int NOT NULL auto_increment  primary key','name':'varchar(20) NOT NULL',
        'date':'date','open':'int default 4000','vol':'float default 200'}
    print('21.show current db=',db.get_db_name_current())#new_futures
    print('22.show all tb_names in current db=',db.get_tb_names())#['m1112', 'm1805',...]
    print('23.show all tb_rows in current db=', db.get_tb_rows()) # {'m1112': 2203,...]
    print('24.show all table in db1=', db.show_tb_names(db2),db.is_exist_db(db2))#None False

    print('31.tb_name not exist=',db.is_exist_table(tb_name))#False
    db.create_table(tb_name,d)       #创建数据表
    print('32.tb_name exist=',db.is_exist_table(tb_name))#True

    print('41.ta_name exist,tb_new_name1 not exist=',db.is_exist_table(tb_name),
          db.is_exist_table(tb_new_name1))#True False
    db.rename_table(tb_name,tb_new_name1)#重命名表
    print('42.ta_name not exist,tb_new_name1 exist=',db.is_exist_table(tb_name), 
          db.is_exist_table(tb_new_name1))#False True
    # 2.2插入数据
    data=[['3200','Tom','2019-03-01'],['3330','Bob1','2019-03-02'],
          ['3400','Jim','2019-03-03']]#插入数据单行或多行
    print('51.show tb_new_name1 data=',db.select(tb_new_name1))#()
    db.insert_data(tb_new_name1,('open','name','date'),data)
    print('52.show tb_new_name1 data=', db.select(tb_new_name1))
    #[{'id': 1, 'name': 'Tom', 'date': datetime.date(2019, 3, 1), 'open': 3200, 'vol': 200.0},{...},...]

    # 2.3 复制表
    print('53.tb_new_name2不存在=', db.is_exist_table(tb_new_name2))#False
    db.copy_table(tb_new_name1,tb_new_name2)#不拷贝数据
    print('54.tb_new_name2存在=', db.is_exist_table(tb_new_name2))#True
    print('55.tb_new_name2显示=', db.select(tb_new_name2))#()

    print('56.tb_new_name3不存在=', db.is_exist_table(tb_new_name3))#False
    db.copy_table(tb_new_name1,tb_new_name3,copy_data=True)#拷贝数据
    print('57.tb_new_name3存在=', db.is_exist_table(tb_new_name3))#True
    print('58.tb_new_name3显示=', db.select(tb_new_name3))
    # [{'id': 1, 'name': 'Tom', 'date': datetime.date(2019, 3, 1), 'open': 3200, 'vol': 200.0},{...},...]
    # 2.4 清空数据表数据
    db.clear_table_datas(tb_new_name3)  # 保留字段
    print('61.tb_new_name3无数据=', db.select(tb_new_name3))#()
    print('62.tb_new_name3显示列名=',db.get_cols_names(tb_new_name3))
    #['id', 'name', 'date', 'open', 'vol']

    # 2.5 删除数据表
    print('71.tb all存在=', db.is_exist_table(tb_new_name1),#True True True
          db.is_exist_table(tb_new_name2), db.is_exist_table(tb_new_name3))
    db.drop_table(tb_new_name1)
    db.drop_table(tb_new_name2)
    db.drop_table(tb_new_name3)
    print('72.tb all no存在=', db.is_exist_table(tb_new_name1),#False False False
          db.is_exist_table(tb_new_name2), db.is_exist_table(tb_new_name3))
# test_tb()
#===============================================================================
#===============================================================================
#3.字段查看测试2019/2/27
db1 = 'new_futures'#数据库存在
tb_name='m1809'

db.select_db(db1)

#1)获取列的指定类型
def test_COLUMNS():
    # 3.1 获取表字段信息

    d=db.COLUMNS(tb_name)
    print('3.11=',d['name'])# ['No', 'name', 'code', ...]
    print('3.12=',d['type'])# ['int(11)', 'char(20)', 'datetime', 'date', 'time',...]
    print('3.13=',d['null'])# ['NO', 'NO', 'YES', 'YES',...]
    print('3.14=',d['key']) # ['PRI', '', '', '', 'UNI', '', 'MUL', '', '', '', '']
    print('3.15=',d['default'])# [None, None, '2019-01-28', '10',...]
    print('3.16=',d['extra'])  # ['auto_increment', '', '', '', '', '', '', '', '', '', '']
    
    d = db.COLUMNS(tb_name, 'no')
    print('3.21=', d['name'])  # ['No']
    print('3.22=', d['type'])  # ['int(11)']
    print('3.23=', d['null'])  # ['NO']
    print('3.24=', d['key'])   # ['PRI']
    print('3.25=', d['default'])# [None.]
    print('3.26=', d['extra'])  # ['auto_increment']

    d = db.COLUMNS(tb_name,back_dict_dict=True)

    print('3.12=', d['type'])  # {'No': 'int(11)',...}
    print('3.13=', d['null'])  # {'No': 'NO', 'name': 'NO',...}
    print('3.14=', d['key'])   # {'No': 'PRI', 'name': '',...}
    print('3.15=', d['default'])# {'No': None, 'name': None, 'code': 'm',...}
    print('3.16=', d['extra'])  # {'No': 'auto_increment', 'name': '',...}
    d = db.COLUMNS(tb_name, col_name='no',back_dict_dict=True)

    print('3.22=', d['type'])  # {'No': 'int(11)'}
    print('3.23=', d['null'])  # {'No': 'NO'}
    print('3.24=', d['key'])   # {'No': 'PRI'}
    print('3.25=', d['default'])# {'No': None}
    print('3.26=', d['extra'])  # {'No': 'auto_increment'}
# test_COLUMNS()
def testIS_EXIST_COL():
    print('0=', db.IS_EXIST_COL(tb_name, cols_names='no1', cols_options='', all_cols=False))
    print('1=', db.IS_EXIST_COL(tb_name, cols_names='no1', cols_options='', all_cols=True))
    print('2=',db.IS_EXIST_COL(tb_name, cols_names='no', cols_options='', all_cols=False))
    print('3=',db.IS_EXIST_COL(tb_name, cols_names='no', cols_options='', all_cols=True))
    print('4=',db.IS_EXIST_COL(tb_name, cols_names='no1,name', cols_options='', all_cols=False))
    print('5=',db.IS_EXIST_COL(tb_name, cols_names='no1,name', cols_options='', all_cols=True))

    print('10=', db.IS_EXIST_COL(tb_name, cols_options='no1 not null', all_cols=False))
    print('11=', db.IS_EXIST_COL(tb_name, cols_options='no1 not null', all_cols=True))
    print('12=', db.IS_EXIST_COL(tb_name, cols_options='no  not null',  all_cols=False))
    print('13=', db.IS_EXIST_COL(tb_name, cols_options='no not null',  all_cols=True))
    print('14=', db.IS_EXIST_COL(tb_name, cols_options='no1 not null,name not null', all_cols=False))
    print('15=', db.IS_EXIST_COL(tb_name, cols_options='no1 not null,name not null', all_cols=True))
# testIS_EXIST_COL()
def test_get_cols_names_rename_col():
    # 显示列名--返回str的list
    d=db.get_cols_names(tb_name)
    print('3.17=',d)#['No', 'name', 'code',...]
    d=db.get_col_name(tb_name, col_name='no')#No
    print('3.18=',d)
    # 更改列名
    print('3.19=',db.is_exist_col(tb_name, 'open1'))#False
    db.rename_col(tb_name, col_name='open', col_new_name='open1')
    print('3.20=', db.is_exist_col(tb_name, 'open1'))#True
    db.rename_col(tb_name, col_name='open1', col_new_name='open')
# test_get_cols_names_rename_col()
#-------------------------------------------------------------------
def test_is_exist_col():
    print('3.0.---=',db.get_tb_n('m1809'))#6
    # 添加字段
    print('3.1.添加字段不存在=',db.is_exist_col(tb_name,'age'))
    db.add_cols(tb_name, 'age int not null default 40')
    print('3.2.添加字段存在=',db.is_exist_col(tb_name,'age'))
    # 删除字段
    db.del_cols('m1809','age')
    print('3.3.删除字段=',db.is_exist_col(tb_name,'age'))
# test_is_exist_col()
def test_any_exist_cols():
    # 多列名是否存在判断
    print('3.11=',db.any_exist_cols(tb_name, cols_names='no,open'))#都存在 True
    print('3.12=', db.any_exist_cols(tb_name, 'name,open_'))       #1个不存在 True
    print('3.13=', db.any_exist_cols(tb_name,cols_options='no int ,name varchar(10)'))#都存在 True
    print('3.14=', db.any_exist_cols(tb_name,cols_options='no_ int ,name varchar(10)'))#1个不存在 True

    print('3.15=', db.all_exist_cols(tb_name, cols_names='no,open'))  # 都存在 True
    print('3.16=', db.all_exist_cols(tb_name, 'name,open_'))          # 1个不存在 False
    print('3.17=', db.all_exist_cols(tb_name, cols_options='no int ,name varchar(10)'))  # 都存在 True
    print('3.18=', db.all_exist_cols(tb_name, cols_options='no_ int ,name varchar(10)')) # 1个不存在 False
# test_any_exist_cols()
def test_is_exist_tb_cols():
    print('1.exist=',db.is_exist_tb_cols(tb_name+'jj'))
    print('2.exist=', db.is_exist_tb_cols(tb_name))
    print('3.exist=', db.is_exist_tb_cols(tb_name, 'no333'))
    print('4.exist=', db.is_exist_tb_cols(tb_name, 'no'))
    print('5.exist=', db.is_exist_tb_cols(tb_name, 'no,name'))
    print('6.exist=', db.is_exist_tb_cols(tb_name, 'no,name1'))
# test_is_exist_tb_cols()
#===================================================================
def test_get_cols_types():
    #数据类型
    print('1.no data type=',db.get_col_type(tb_name,'no'))#int(11)
    print('2.no1 not data type=', db.get_col_type(tb_name, 'no1'))  # = 空

    print('3.not table data type=', db.get_cols_types(tb_name+'ss'))#{}
    print('4.tb data type=', db.get_cols_types(tb_name))  # {'No': 'int(11)', 'name': 'char(20)',...}
    print('5.no data type=', db.GET_DATA_TYPES(tb_name, 'no'))  # {'No': 'int'}
    print('6.tb data type=', db.GET_DATA_TYPES(tb_name))  # {'close': 'float', 'code': 'char',...}
    print('7.tb not data type=', db.GET_DATA_TYPES(tb_name+'ssss'))#{}
# test_get_cols_types()
#===================================================================
def test_get_AI():
    #自增测试
    print('10.not auto increment=', db.get_auto_increment_name(tb_name+'s'))  #不存在显示空''
    print('11.get auto increment=',db.get_auto_increment_name(tb_name))#No;不存在显示空''
    print('11.get auto increment=', db.get_AI(tb_name))  # No;不存在显示空'

    print('12.exist AI=', db.is_exist_AI(tb_name))           #true
    print('13.not exist AI=', db.is_exist_AI(tb_name+'ss'))  #False
    print('14.exist PRI AI=', db.is_exist_AI(tb_name, 'no'))  # true
    print('15.not exist AI=', db.is_exist_AI(tb_name,'no1'))#false

    print('16.exist PRI AI=', db.get_col_order_str(tb_name,'no'))#不存在显示''
    # No int(11) NOT NULL AUTO_INCREMENT
    print('17.not exist AI=', db.get_col_order_str(tb_name+'s', 'no'))  # 不存在显示''
    print('18.not exist AI=', db.get_col_order_str(tb_name , 'no1'))  # 不存在显示''

# test_get_AI()
#-------------------------------------------------------------------
def test_add_del_AI():
    #自增键测试
    print('1.是自增键=',db.is_exist_AI(tb_name))
    print('2.是自增键=', db.is_exist_AI(tb_name, 'no'))
    print('3.del AI=',db.del_AI(tb_name))
    print('4.不是自增键=', db.is_exist_AI(tb_name, 'no'))
    print('5.不是自增键=', db.is_exist_AI(tb_name))
    print('6.add AI=',db.add_AI(tb_name,'no'))
    print('7.是自增键=', db.is_exist_AI(tb_name))
    print('8.是自增键=', db.is_exist_AI(tb_name, 'no'))
# test_add_del_AI()
#===================================================================
def test_cols_PRI():
    #首键测试
    # def __get_key(self, tb_name):
    #     if not self.is_exist_table(tb_name): return None
    #     d = self.COLUMNS(tb_name, back_dict_dict=True)
    #     if not bool(d): return None
    #     return d['key']

    # def get_PRI(self, tb_name):
    #     d = self.__get_key(tb_name)
    #     return [k for k in d if d[k] == 'PRI' if d]
    print('9.get首键=', db.get_PRI(tb_name+'s'))  # None
    print('10.get首键=', db.get_PRI(tb_name))  # ['NO']
    print('11.是首键=', db.is_exist_PRI(tb_name, 'no'))#true
    print('12.是首键=', db.is_exist_PRI(tb_name))#True
    print('13.del首键=',db.del_primary_key(tb_name))#None
    print('14.不是首键=', db.is_exist_PRI(tb_name, 'no'))#False
    print('15.不是首键=', db.is_exist_PRI(tb_name))#False

    print('16.add首键=', db.add_primary_key(tb_name,'no'))#None
    print('17.是首键=', db.is_exist_PRI(tb_name, 'no'))#True
    print('18.是首键=', db.is_exist_PRI(tb_name))#True

    print('21.del首键=', db.del_primary_key(tb_name))#None
    print('22.不是首键=', db.is_exist_PRI(tb_name, 'no'))#False
    print('23.不是首键=', db.is_exist_PRI(tb_name))#False

    print('24.获得索引=', db.get_indexs(tb_name))
    # {'date': 'date_UNIQUE', 'open': 'idx_m1809_open_high', 'high': 'idx_m1809_open_high'}
    print('25.add首键=', db.add_primary_key(tb_name, 'no,vol'))#None
    print('26.是首键=', db.is_exist_PRI(tb_name, 'no'))#True
    print('27.是首键=', db.is_exist_PRI(tb_name))#True
    print('28.获得索引=', db.get_indexs(tb_name))
    '''
    {'NO': 'PRIMARY', 'vol': 'PRIMARY', 'date': 'date_UNIQUE', 'open': 'idx_m1809_open_high',
     'high': 'idx_m1809_open_high'}
     '''
    #恢复原始数据
    print('31.del首键=', db.del_primary_key(tb_name))  # None
    print('32.add首键=', db.add_primary_key(tb_name, 'no'))  # None
    print('33.添加自增=', db.add_AI(tb_name, 'no'))  # None
# test_cols_PRI()
#-------------------------------------------------------------------

def test_is_exist_PRI_is_exist_UNI():
    # 列是否是首键唯一键
    print('1.exist PRI=',db.is_exist_PRI(tb_name, 'no'))#true
    print('2.not exist PRI=',db.is_exist_PRI(tb_name, 'name')) #false
    print('3.exist PRI=', db.is_exist_PRI(tb_name))  # True
    # 列是否是唯一值键
    print('3.not exist UNI=',db.is_exist_UNI(tb_name, 'no'))   #false
    print('4. exist UNI=',db.is_exist_UNI(tb_name, 'date'))    #TRUE
    print('5.not exist UNI=', db.is_exist_UNI(tb_name, 'name'))#FALSE
    print('6.not exist UNI=', db.is_exist_UNI(tb_name, 'open'))#FALSE
    print('7.not exist UNI=', db.is_exist_UNI(tb_name))  # True

# test_is_exist_PRI_is_exist_UNI()
def test_UNI():

    print('1.get UNI=',db.get_UNI(tb_name))#['date']
    print('2.get UNI=',db.get_UNI(tb_name+'kk'))#None
    print('3.get UNI=',db.get_UNI(tb_name, col_name='date'))#date
    print('4.get UNI=',db.get_UNI(tb_name, col_name='no'))#''
    print('5.get UNI=', db.get_UNI(tb_name, col_name='open'))#''
    print('6.get UNI=', db.get_UNI(tb_name, col_name='no1'))#''

    print('10.get UNI=', db.get_UNI_NAME(tb_name))  # {'date': 'date_UNIQUE'}
    print('11.get UNI=', db.get_UNI_NAME(tb_name, back_UNI_Dict=False))  # ['date_UNIQUE']
    print('12.get UNI=', db.get_UNI_NAME(tb_name + 'kk'))  # None
    print('13.get UNI=', db.get_UNI_NAME(tb_name, col_name='date'))  # date_UNIQUE
    print('14.get UNI=', db.get_UNI_NAME(tb_name, col_name='date', back_UNI_Dict='dict'))  # date_UNIQUE

    print('15.get UNI=', db.get_UNI_NAME(tb_name, col_name='no'))  # ''
    print('16.get UNI=', db.get_UNI_NAME(tb_name, col_name='no1'))  # ''


    db.set_UNI(tb_name,  col_name='time')
    print('20.get UNI=', db.get_UNI_NAME(tb_name))  # {'date': 'date_UNIQUE', 'time': 'time'}
    db.del_UNI(tb_name,col_name='time')
    print('21.get UNI=', db.get_UNI_NAME(tb_name))  # {'date': 'date_UNIQUE'}

    db.set_UNI(tb_name, index_name='idx_TM_1', col_name='time')
    print('22.get UNI=', db.get_UNI_NAME(tb_name))  # {'date': 'date_UNIQUE', 'time': 'idx_TM_1'}
    db.del_UNI(tb_name, index_name='idx_TM_1')
    print('23.get UNI=', db.get_UNI_NAME(tb_name))  # {'date': 'date_UNIQUE'}

    db.set_UNI(tb_name, index_name='idx_TM_2', col_name='time')
    print('24.get UNI=', db.get_UNI_NAME(tb_name))  # {'date': 'date_UNIQUE', 'time': 'idx_TM_2'}
    db.del_UNI(tb_name, 'idx_TM_2')
    print('25.get UNI=', db.get_UNI_NAME(tb_name))  # {'date': 'date_UNIQUE'}

    print('31.set UNI=',db.set_UNI(tb_name, index_name='idx_TM_3', col_name='time'))#None
    print('32.set UNI=', db.set_UNI(tb_name, index_name='idx_TM_3', col_name='time'))#-1
    print('33.set UNI=', db.set_UNI(tb_name, index_name='idx_TM_3', col_name='time33'))#-1
    print('34.set UNI=', db.set_UNI(tb_name+'ss', index_name='idx_TM_3', col_name='time'))#-1

    print('41.get UNI=', db.get_UNI_NAME(tb_name))  # {'date': 'date_UNIQUE', 'time': 'idx_TM_3'}
    print('42.del UNI=', db.del_UNI(tb_name+'ss', 'idx_TM_3'))#-1
    print('43.del UNI=', db.del_UNI(tb_name, 'idx_TM_333'))#-1
    print('44.del UNI=', db.del_UNI(tb_name, col_name='time333'))#-1
    print('45.del UNI=', db.del_UNI(tb_name, 'idx_TM_3'))#None

    print('46.get UNI=', db.get_UNI_NAME(tb_name))  # {'date': 'date_UNIQUE'}

# test_UNI()
#-------------------------------------------------------------------
def test_get_indexs_is_exist_index():
    #索引测试
    print('1.show all index name=\n',db.get_indexs(tb_name))
    # {'No': 'PRIMARY', 'date': 'date_UNIQUE', 'open': 'idx_m1809_open_high', 'high': 'idx_m1809_open_high'}
    print('2.show all index name=\n', db.get_indexs(tb_name,include_PRI=False))
    #{'date': 'date_UNIQUE', 'open': 'idx_m1809_open_high', 'high': 'idx_m1809_open_high'}
    print('3.show all index name=\n', db.get_indexs(tb_name,back_dict_list=True)['name'])
    #['NO', 'date', 'open', 'high']
    print('4.show all index name=\n', db.get_indexs(tb_name, back_dict_list=True,include_PRI=False)['name'])
    #['date', 'open', 'high']
    print('5.show all index name=\n', db.get_indexs(tb_name, back_dict_list=True)['idx'])
    #['PRIMARY', 'date_UNIQUE', 'idx_m1809_open_high', 'idx_m1809_open_high']
    print('6.show all index name=\n', db.get_indexs(tb_name, back_dict_list=True,include_PRI=False)['idx'])
    #['date_UNIQUE', 'idx_m1809_open_high', 'idx_m1809_open_high']

    print('7.show index name=', db.get_index(tb_name, 'no'))#PRIMARY
    print('8.show index name=', db.get_index(tb_name, 'no',include_PRI=False))#空
    print('9.show index name=', db.get_index(tb_name,'open')) #idx_m1809_open_high
    print('10.show index name=', db.get_index(tb_name,'name'))# 显示空字符
    print('11.show index name=', db.get_index(tb_name, 'vol'))# 显示空字符

    print('21.show index name=', db.is_exist_index(tb_name,include_PRI=True))    #True
    print('22.show index name=', db.is_exist_index(tb_name, include_PRI=False))  # True
    print('23.show index name=', db.is_exist_index(tb_name+'s', include_PRI=True))  # False
    print('24.show index name=', db.is_exist_index(tb_name+'s', include_PRI=False)) # False

    print('25.show index name=', db.is_exist_index(tb_name, 'no',True))   # True
    print('26.show index name=', db.is_exist_index(tb_name, 'no',False))  # False
    print('27.show index name=', db.is_exist_index(tb_name, 'date',True)) #true
    print('28.show index name=', db.is_exist_index(tb_name, 'open',False))#true
    print('29.show index name=', db.is_exist_index(tb_name, 'name',True)) #false
    print('30.show index name=', db.is_exist_index(tb_name, 'name', False))  # false
# test_get_indexs_is_exist_index()
def test_add_del_index():

    # 添加索引
    print('11.全部列名=', db.get_cols_names(tb_name))
    #['NO', 'name', 'code', 'datetime', 'date', 'time', 'open', 'high', 'low', 'close', 'vol']
    print('12.查看索引列名=',db.get_indexs(tb_name,back_dict_list=True,include_PRI=False)['name'])
    #['date', 'open', 'high']
    print('13.查看索引名=', db.get_indexs(tb_name, True,False)['idx'])
    #['date_UNIQUE', 'idx_m1809_open_high', 'idx_m1809_open_high']
    print('14.add idx=',db.set_index(tb_name,'low_close','low,close'))#None
    print('21.查看索引列名=', db.get_indexs(tb_name,True,False)['name'])
    # ['date', 'open', 'high', 'low', 'close']
    print('22.查看索引名=', db.get_indexs(tb_name,True,False)['idx'])
    #['date_UNIQUE', 'idx_m1809_open_high', 'idx_m1809_open_high', 'low_close', 'low_close']
    print('23.del idx=',db.del_index(tb_name,'low_close'))#None
    print('24.查看索引列名=', db.get_indexs(tb_name,True,False)['name'])
    # ['date', 'open', 'high']
    print('25.查看索引名=', db.get_indexs(tb_name,True,False)['idx'])
    #['date_UNIQUE', 'idx_m1809_open_high', 'idx_m1809_open_high']

    print('31.add idx=', db.set_index(tb_name, 'low_close', 'no'))  # -1
    print('32.add idx=', db.set_index(tb_name, 'low_close', 'date'))  # -1
    print('33.add idx=', db.set_index(tb_name, 'low_close', 'open'))  # -1
    print('34.add idx=', db.set_index(tb_name, 'low_close', 'low,close',unique_idx=True))  # -1
    print('35.add idx=', db.set_index(tb_name+'s', 'low_close', 'low'))  # -1
    print('36.add idx=', db.set_index(tb_name, 'low_close', ''))  # -1
    print('37.add idx=', db.set_index(tb_name, '', 'low'))  # None
    print('38.add idx=', db.set_index(tb_name, '', 'low,vol'))  # -1
    print('39.add idx=', db.set_index(tb_name, '', 'time,vol'))  # None
    print('40.查看索引列名=', db.get_indexs(tb_name, True, False)['name'])
    # ['date', 'open', 'high', 'low', 'time', 'vol']
    print('41.查看索引名=', db.get_indexs(tb_name, True, False)['idx'])
    #['date_UNIQUE', 'idx_m1809_open_high', 'idx_m1809_open_high', 'low', 'time', 'time']

    #删除索引测试
    print('51.del idx=', db.del_index(tb_name+'k', 'low_close'))  # -1
    print('52.del idx=', db.del_index(tb_name, 'low_close333'))   # -1
    print('53.del idx=', db.del_index(tb_name, col_name='low333'))# -1

    print('54.del idx=',db.del_index(tb_name,'low'))#None
    print('55.查看索引列名=', db.get_indexs(tb_name,True,False)['name'])
    # ['date', 'open', 'high', 'time', 'vol']
    print('56.查看索引名=', db.get_indexs(tb_name,True,False)['idx'])
    #['date_UNIQUE', 'idx_m1809_open_high', 'idx_m1809_open_high', 'time', 'time']
    print('57.del idx=',db.del_index(tb_name,col_name='time,vol'))#-1
    print('58.查看索引列名=', db.get_indexs(tb_name,True,False)['name'])
    # ['date', 'open', 'high']
    print('59.查看索引名=', db.get_indexs(tb_name,True,False)['idx'])
    #59.查看索引名= ['date_UNIQUE', 'idx_m1809_open_high', 'idx_m1809_open_high', 'time', 'time']

    print('1.del idx=', db.del_index(tb_name, col_name='time'))  # None
    print('2.查看索引列名=', db.get_indexs(tb_name, True, False)['name'])
    # ['date', 'open', 'high']
    print('3.查看索引名=', db.get_indexs(tb_name, True, False)['idx'])
    #['date_UNIQUE', 'idx_m1809_open_high', 'idx_m1809_open_high']

    print('60.del idx=',db.del_index(tb_name,col_name='no'))#-1
    print('61.查看索引列名=', db.get_indexs(tb_name,True,False)['name'])
    # ['date', 'open', 'high']
    print('62.查看索引名=', db.get_indexs(tb_name,True,False)['idx'])
    #['date_UNIQUE', 'idx_m1809_open_high', 'idx_m1809_open_high']
    print('63.del idx=',db.del_index(tb_name))#None
    print('64.查看索引列名=', db.get_indexs(tb_name,True,False)['name'])# []
    print('65.查看索引名=', db.get_indexs(tb_name,True,False)['idx'])#[]
    #恢复时间默认值
    db.set_index(tb_name, 'date_UNIQUE','date',True)
    db.set_index(tb_name, 'idx_m1809_open_high', 'open,high')


# test_add_del_index()
#===================================================================
import datetime
def test_default_value1():
    
    # #查看默认值
    print('1.查看默认值=',db.get_default_values(tb_name))
    print('2.查看默认值=', db.get_default_values(tb_name,'no'))
    print('3.查看默认值=', db.get_default_values(tb_name,'close'))
    print('4.查看默认值=', db.get_default_values(tb_name,back_dict_dict=True))
    print('5.查看默认值=', db.get_default_values(tb_name, 'no',back_dict_dict=True))
    print('6.查看默认值=', db.get_default_values(tb_name, 'close',back_dict_dict=True))
    """
    1.查看默认值= [None, 'zhangfei', 'Tom', '2019-01-28 22:46:30', '2019-01-28', '22:46:00', '100', '200', '300', '400', None]
    2.查看默认值= [None]
    3.查看默认值= ['400']
    4.查看默认值= {'NO': None, 'name1': 'zhangfei', 'code': 'Tom', 'datetime': '2019-01-28 22:46:30', 'date': '2019-01-28', 'time': '22:46:00', 'open': '100', 'high': '200', 'low': '300', 'close': '400', 'vol': None}
    5.查看默认值= {'NO': None}
    6.查看默认值= {'close': '400'}
    """
# test_default_value1()
def test_default_value2():
    alter_dv=db.alter_default_value
    drop_dv=db.drop_default_value
    alter_dv('m1809','open','4000')#数字型可为数字,str
    # print('14.alter默认值=',alter_dv(tb_name+'s', 'open', '10'))#-1
    # print('15.alter默认值=',alter_dv(tb_name, 'open333', '10')) #-1
    # print('16.alter默认值=', alter_dv(tb_name, 'no', '10'))     #-1

    d1 = datetime.datetime.now()
    
    alter_dv(tb_name, 'name', '张飞')
    alter_dv(tb_name, 'code', '刘备')
    alter_dv(tb_name, 'datetime', d1)
    alter_dv(tb_name, 'date', d1.date())
    alter_dv(tb_name, 'time', d1.time())
    alter_dv(tb_name, 'open', '10')
    alter_dv(tb_name, 'high', '20')
    alter_dv(tb_name, 'low', 30)
    alter_dv(tb_name, 'close', '40')
    alter_dv(tb_name, 'vol', '50')
    print('22.查看默认值=', db.get_default_values(tb_name, back_dict_dict=True))

    # 3.16 删除默认值
    drop_dv(tb_name, 'name')
    drop_dv(tb_name, 'code')
    drop_dv(tb_name, 'datetime')
    drop_dv(tb_name, 'date')
    drop_dv(tb_name, 'time')
    drop_dv(tb_name, 'open')
    drop_dv(tb_name, 'high')
    drop_dv(tb_name, 'low')
    drop_dv(tb_name, 'close')
    drop_dv(tb_name, 'vol')
    print('23.查看默认值=', db.get_default_values(tb_name, back_dict_dict=True))

    # print('36.del默认值=', drop_dv(tb_name+'s', 'open'))#-1
    # print('37.del默认值=', drop_dv(tb_name, 'open333'))#-1

    #恢复默认值
    alter_dv(tb_name, 'name', '布什')
    alter_dv(tb_name, 'code', '汤姆')
    alter_dv(tb_name, 'datetime', d1)
    alter_dv(tb_name, 'date', d1.date())
    alter_dv(tb_name, 'time', d1.time())
    alter_dv(tb_name, 'open', '100')
    alter_dv(tb_name, 'high', '200')
    alter_dv(tb_name, 'low', 300)
    alter_dv(tb_name, 'close', '400')
    alter_dv(tb_name, 'vol', '500')
    print('24.查看默认值=', db.get_default_values(tb_name, back_dict_dict=True))

# test_default_value2()
"""
    22.查看默认值= {'NO': None, 'name': '张飞', 'code': '刘备', 'datetime': '2019-02-28 17:08:18', 'date': '2019-02-28', 'time': '17:08:18', 'open': '10', 'high': '20', 'low': '30', 'close': '40', 'vol': '50'}
    23.查看默认值= {'NO': None, 'name': None, 'code': None, 'datetime': None, 'date': None, 'time': None, 'open': None, 'high': None, 'low': None, 'close': None, 'vol': None}
    24.查看默认值= {'NO': None, 'name': '布什', 'code': '汤姆', 'datetime': '2019-02-28 17:08:18', 'date': '2019-02-28', 'time': '17:08:18', 'open': '100', 'high': '200', 'low': '300', 'close': '400', 'vol': '500'}
"""
#==============================================================================
#4.插入记录
dt=datetime.datetime.now()
def test_insert_data():
    # def insert_data(self, tb_name,cols_names='',cols_values=[],repeat_value=False,
    #                    UpdataCols='',UpdataValues='',priority='',executemany=True):

    tb_name='m2010'
    col_options= {'id':'int NOT NULL auto_increment  primary key',
        'name':'varchar(30) NOT NULL','age':'int default 10','date':'date ',
        'open':'float not null default 3000','vol':'float default 0'}

    col_options1 = " id int NOT NULL auto_increment  primary key, \
         name varchar(30) NOT NULL, age int default 10, date date default '%s',\
         open float not null default 3000, vol float default 0"%(dt.date())

    # print(dt.strftime("%Y-%m-%d"))#2019-03-01
    # db.drop_table(tb_name)
    # print('1=', db.is_exist_table(tb_name))#false
    # db.create_table(tb_name,col_options)    #创建数据表
    # print('2=',db.is_exist_table(tb_name))#true
    # db.drop_table(tb_name)
    # print('3=', db.is_exist_table(tb_name))#false
    # db.create_table(tb_name, col_options1)  # 创建数据表
    # print('4=', db.is_exist_table(tb_name))#true

    data=[['3000','Tom','2019-03-01'],['3100','Bob','2019-03-02']]#插入数据单行或多行
    data1= [['3200', '刘备', '2019-03-01'], ['3300', '张飞', '2019-03-02']]  # 插入数据单行或多行
    # print('11数据表行数=', db.get_tb_n(tb_name))
    # db.insert_data(tb_name,'open,name,date',data)
    # print('12数据表行数=',db.get_tb_n(tb_name))
    # db.insert_data(tb_name,'open,name,date',data1)
    # print('13数据表行数=',db.get_tb_n(tb_name))
    #
    # db.insert_data(tb_name,'open,name,date',data,True,UpdataCols='name',UpdataValues='name')
    # print('14数据表行数=', db.get_tb_n(tb_name))
    print('15数据表行数=', db.get_last_AI_VALUE(tb_name))
# test_insert_data()
#===========================================================================
#测试更新数据
def test_updata():
    # update db_database.tb_m1809 set No=11,name="Smith" where name="Tom" limit 10
    cols='No,name';values="6 ,tcy"
    where_con='name="John" '
    db.update_data('m1809',cols,values,where_con)
# test_updata()
#===========================================================================
#删除数据
tb_name='m1809'
def test_del_data():
    # del_data(self, tb_name,where_condition='',order_by_cols_desc='',limit_n=0,priority=''):
     print(db.get_tb_n(tb_name))
     db.del_data(tb_name, 'no>4')

# test_del_data()
#===========================================================================
#查询测试
tb_name='m1809'
def test_select():

    # print('1.select=',db.select('',tb_name))
    # print('2.select=',db.select('',tb_name,back_list=True))
    """ 
    select = [{'NO': 1, 'name': '豆粕', 'code': 'm1809', 'datetime': 
               datetime.datetime(2019, 1, 27, 14, 8, 5),...},{},...]
    select = ((1, '豆粕', 'm1809', datetime.datetime(2019, 1, 27, 14, 8, 5), 
               datetime.date(2019, 1, 27), datetime.timedelta(0, 50885), 
               3000.0, 110.0, 2900.0, 2950.0, 1542), (...), ...)
    """
    # print('3.select=', db.select('name,open', tb_name))
    # print('4.select=', db.select('name,avg(vol)', tb_name))
    # print('5.select=', db.select('tb1.name,m1809.name','new_futures.m1809,db1.tb1')[0])
    # print('6.select=', db.select('*', tb_name,distinct=True)[0])
    # print('7.select=', db.select('*', tb_name, where="no >1 and name like '%bob%'"))
    # print('8.select=', db.select('*', tb_name, where="no >11 and name like '%bob%'",
    #                              order_by_desc=True,order_by_cols='no'))
    print('9.select=', db.select('*', tb_name, where="no <13",having="name='Tom'"))
    print('10.select=', db.select('no', tb_name,where='no between 3 and 5'))
    print('11.select=', db.select('no,name', tb_name, limit=(1,3)))
    print('12.select=', db.select('no,name,avg(open)', tb_name,group_by='name,no', limit=(1, 3)))
test_select()
类封装

import re
import pymysql
# import mysql
# import MySQLdb
class PyMySqlClass(object): # 继承object类所有方法
    """
    writed by tcy in shanghai yexie in 2019/1/24--2/28
    Mysql语句pymsql的封装。你能通过这些方法执行数据库的读写创建等操作。

    pymysql connect参数:
    self, host=None, user=None, password='', database=None, port=0, unix_socket=None,
    charset='', sql_mode=None, read_default_file=None, conv=None, use_unicode=None, client_flag=0,
    cursorclass=<class 'pymysql.cursors.Cursor'>, init_command=None, connect_timeout=10,
    ssl=None, read_default_group=None, compress=None, named_pipe=None, autocommit=False,
    db=None, passwd=None, local_infile=False, max_allowed_packet=16777216, defer_connect=False,
    auth_plugin_map=None, read_timeout=None, write_timeout=None,
    bind_address=None, binary_prefix=False, program_name=None, server_public_key=None
    """
    __slots__=( 'db_name','n_row','con','cursor','cursor_lst','err','war') # 用tuple定义允许绑定的属性名称

    __config = {
        'host': '127.0.0.1', 'port': 3306, 'user': 'root', 'password': 'root',
        'charset': '', 'database': None, 'connect_timeout': 10, 'autocommit': False,
        'max_allowed_packet': 16777216, 'read_timeout': None, 'write_timeout': None
    }
    def __init__(self ,is_SSDictCurse=False,**config):
        self.n_row = 0  # 查询结果影响的行数
        self.err = ''
        self.war = ''

        self.con = None
        self.cursor = None
        self.cursor_lst = None

        self.__connect_db(is_SSDictCurse, **self.__config)
        self.db_name=self.__config['database']

    def __del__(self):
        self.db_name = ''
        self.n_row = 0
        self.err = ''
        self.war = ''

        self.__close_connect()
    #0.数据库连接
    #==========================================================================================
    #0.1定义1个连接
    def __connect_db(self ,is_SSDictCurse=False,**config):
        """
        :param is_SSDictCurse: bool; 每个操作都独占一个连接,当前只能执行一个操作
        :param config: dict 连接配置参数;可指定数据库也可不指定
        :return: None 你能创建1个新的连接
        """
        if not bool(config):return -1
        for i in config:
            for i1 in self.__config:
                if i==i1:self.__config[i]=config[i]

        try:
            self.con = pymysql.connect(**self.__config)
            if is_SSDictCurse == False:  # 所有查询共用一个连接 _con,一个游标 _cursor
                _cursor_lst = self.con.cursor(pymysql.cursors.Cursor)  # 输出结果为lst
                _cursor = self.con.cursor(pymysql.cursors.DictCursor)  # 输出结果为字典
                   # if autocommit: _con.autocommit(1)                 # 自动提交事务
            else:  #不缓存游标            # 每个操作都独占一个连接,当前只能执行一个操作
                _SScursor_lst = self.con.cursor(pymysql.cursors.SSCursor)  # 迭代为lst
                _SScursor = self.con.cursor(pymysql.cursors.SSDictCursor)  # 迭代为字典

            self.cursor_lst = _cursor_lst if is_SSDictCurse == False else _SScursor_lst
            self.cursor = _cursor if is_SSDictCurse == False else _SScursor
            return None
        except Exception as  e:
            self.err = "DB Connect Error!" + str(e)
            return -1

    # 0.2关闭数据库连接
    def __close_connect(self):
        self.db_name='';self.err='';self.war='';self.n_row=0;
        if self.con :
            self.con.close()
            return None
        else:
            self.err = "DB close Error!.Not connect."
            return -1
    #==========================================================================================

    #1.数据库操作
    def __run(self,sql='',commit=False,fetchall=True,backvalue=True,executemany=False,args=None,
              back_lst=False):
        '''
        你也能直接用此方法执行mysql语句
        :param sql: str;Mysql语句
        :param commit: bool;True提交更改
        :param fetchall: bool or int>0;False不返回数据,True返回全部数据;int返回指定数量数据
        :param backvalue: bool;为True返回数据
        :param executemany: True用executemany指令
        :param args: 指令参数
        :param back_lst:false 结果默认以字典形式返回True,list返回
        :return: result[0]=None or 查询结果dict or list,result[1]成功执行返回None,失败返回-1
        '''
        self.n_row=0;result=None;runok=None
        try:
            cursor=self.cursor_lst if back_lst else self.cursor
            # 执行SQL语句
            if executemany==False:
                self.n_row = cursor.execute(sql, args) if args else cursor.execute(sql)
            else:
                self.n_row=cursor.executemany(sql,args)#必须要有args参数

            if commit:
                self.con.commit()     # 提交到数据库执行

            if backvalue:
                if isinstance(fetchall,bool):
                    result = (cursor.fetchall() if fetchall else cursor.fetchone())
                elif isinstance(fetchall,int) and (fetchall>0):
                    result=cursor.fetchmany(fetchall)
        except Exception as  e:       # Exception 是所有异常的基类,这里表示捕获所有的异常
            self.con.rollback()       # 发生错误时回滚
            self.n_row =0;runok=-1
            self.err = "__run() Error!"+str(e)
        finally:
            return (result,runok)

    # 1.1 查看当前数据库
    def show_db_name_current(self):
        '''
        :return: 查看当前数据库--返回str
        '''
        sql = "select database()"
        lst = self.__run(sql,back_lst=True)[0]
        return lst[0][0] if lst else  ''
    
    def get_db_name_current(self):
        return self.show_db_name_current()

    # 1.3 数据库是否是当前数据库
    def is_exist_db_current(self, db_name):
        sql = "select database()"
        lst = self.__run(sql, back_lst=True)[0]
        if not bool(lst): return False
        return lst[0][0] == db_name

    # 1.4 查看所有数据库
    def show_db_name_all(self):
        '''
        :return: 查看所有数据库--返回字符串list
        show create table m1809;#显示创建mysql命令str
        show table status;#Rows Auto_increment
        #显示数据库中数据表
        SELECT * FROM information_schema.tables WHERE Table_Schema='new_futures' having table_name='m1809';
           #TABLE_SCHEMA,TABLE_NAME,TABLE_ROWS,AUTO_INCREMENT,
        select * from information_schema.COLUMNS where TABLE_NAME='m1809';
           #TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLUMN_DEFAULT()默认值,IS_NULLABLE,DATE_TYPE,
           COLUMN_TYPE,COLUMN_KEY,EXTRA,CHARACTER_SET_NAME,COLLATION_NAME

        '''
        sql = "show databases"
        lst = self.__run(sql)[0]
        return ( [d['Database'] for d in lst] if lst else None)

    def get_db_names(self):
        return self.show_db_name_all()

    #1.6 数据库是否存在
    def is_exist_db(self,db_name):
        sql="show databases like '%s'"%(db_name)
        result=self.__run(sql)
        return  bool(result[0]) and (result[1]==None)


    #1.7 创建数据库
    def create_db(self,db_name,charset='UTF8MB4'):
        if  self.is_exist_db(db_name):return -1
        sql='CREATE DATABASE IF NOT EXISTS %s DEFAULT CHARACTER SET %s' % (db_name,charset)
        result=self.__run(sql,commit=True,backvalue=False)
        return result[1]
    # 1.7 创建数据库
    def add_db(self, db_name, charset='UTF8MB4'):
        return self.create_db(db_name,charset)

    #1.8 删除数据库
    def drop_db(self,db_name):
        if not self.is_exist_db(db_name):return -1
        sql='drop database if exists  %s ' % (db_name)
        result=self.__run(sql,commit=True,backvalue=False)
        if result[1]==-1:return -1
        if self.db_name==db_name:
            self.db_name=''
        return None
    #1.8 删除数据库
    def del_db(self, db_name):
        return self.drop_db(db_name)

    #1.9 选择数据库
    def select_db(self,db_name):
        '''
        Mysql语句:use new_futures;
        '''
        if not self.is_exist_db(db_name):return -1
        if not self.is_exist_db_current(db_name):
            self.con.select_db(db_name)
            self.db_name=db_name
        return None

    #2.表的操作
    #==========================================================================
    # 2.0显示所有表的表名行数
    def __SHOW_TB_NAMES(self,db_name='',tb_name=''):#表名更新,但是表行数无实时更新
        """
        :param db_name:str
        :param tb_name:str返回指定表的表名,行数;无指定返回数据库中全部表的表名行数
        :return:
         #显示数据库中数据表
        SELECT * FROM information_schema.tables WHERE Table_Schema='new_futures' having table_name='m1809';
           #TABLE_SCHEMA,TABLE_NAME,TABLE_ROWS,AUTO_INCREMENT,
        """
        db_name=db_name if db_name else self.get_db_name_current()
        sql= "select TABLE_NAME,TABLE_ROWS from information_schema.tables where table_schema = '%s'"
        if tb_name:
            sql=sql+"  having table_name='%s'"
            sql=sql %(db_name,tb_name)
        else:
            sql=sql%(db_name)
        sql_1 = 'flush table information_schema.tables'
        self.__run(sql_1, commit=True, backvalue=False)[0]

        lst = self.__run(sql, back_lst=True)[0]

        table_names =[v[0] for v in lst] if lst else None
        table_rows = [v[1] for v in lst] if lst else None
        return {'table_names':table_names,'table_rows':table_rows}

        return self.__SHOW_TB_NAMES()
    #2.0显示所有表表名
    def show_tb_names(self, db_name=''):#2019/3/3
        """
        :param db_name:
        :return: #返回list or None
        """
        db_name=db_name if db_name else self.get_db_name_current()
        sql="show tables from  %s"%(db_name)
        result=self.__run(sql,back_lst=True)[0]
        if not result:return None
        return [v[0] for v in result if v]
    
    def get_tb_names(self, db_name=''):#2019/3/3
        """
        :param db_name:str
        :return: #返回list or None
        """
        return self.show_tb_names(db_name)

    # 2.0 判断数据表是否存在
    def is_exist_table(self, tb_name):
        sql = "show tables like '%s'" % tb_name
        result = self.__run(sql)
        return bool(result[0]) and (result[1] == None)

    #2.0显示所有表行数
    def show_tb_rows(self, db_name=''):#返回字典 2019/3/3
        tb_names=self.get_tb_names(db_name)
        if not tb_names:return None
        return {k:self.get_tb_n(k, db_name) for k in tb_names if k}

    def get_tb_rows(self, db_name=''):  # 返回字典2019/3/3
        """
        :param db_name:
        :return: # 返回字典
        """
        return self.show_tb_rows(db_name)

    def get_tb_n(self, tb_name='',db_name=''):#2019/3/3
        """
        :param tb_name:str
        :return:None or int 返回当前数据库指定表的行数
        """
        if not self.is_exist_table(tb_name):return None
        db_name=db_name+r'.' if db_name else ''
        sql="select count(*) from %s%s "%(db_name,tb_name)
        result=self.__run(sql,back_lst=True)[0]
        return result[0][0] if  result else None


    #2.1 修改表名
    def __alter_table_name(self, tb_old_name, tb_new_name):
        if (not self.is_exist_table(tb_old_name)) or self.is_exist_table(tb_new_name):
            return -1
        sql = 'rename table %s to %s' % (tb_old_name, tb_new_name)
        result=self.__run(sql, commit=True, backvalue=False)
        return result[1]
    #2.1 修改表名
    def rename_table(self, tb_old_name, tb_new_name):
        return  self.__alter_table_name(tb_old_name, tb_new_name)

    # 2.2 创建数据表
    def create_table(self, tb_name, table_options={}):
        """
        MySQL format:
        create table if not exists tb_name (
                id bigint unsigned auto_increment primary key,#主键只能设1个
                user varchar(10) not null default '',
                createtime datetime) select_statement;        #加快速度

        Pymql format:
        '`id` bigint(11) NOT NULL AUTO_INCREMENT,'

        args:
                tb_name  :str   表名字
                table_options :dict:属性键值对,{'col_name':'varchar(20) NOT NULL'...}
                               str: 'col_name varchar(20) NOT NULL,...'
                foreign_key :str  主外键约束,PRIMARY KEY(`id`)
        """
        if self.is_exist_table(tb_name): return -1
        if isinstance(table_options, str) and bool(table_options):
            options_str = " ( " + table_options + " ) "
        elif isinstance(table_options, dict):
            options_str = ''.join([''.join([' ', i, ' ', v, ',']) for i, v in table_options.items()])
            options_str = '(' + options_str.rstrip(',') + ')'
        else:
            return -1
        sql = 'CREATE TABLE IF NOT EXISTS %s ' % tb_name + options_str

        result = self.__run(sql, commit=True, backvalue=False)
        return result[1]

    def add_table(self, tb_name, table_options):
        self.create_table(tb_name, table_options)

    #2.3 删除表
    def drop_table(self, tb_name):
        if not self.is_exist_table(tb_name):return -1
        sql = "DROP TABLE if exists %s" % tb_name
        result=self.__run(sql,commit=True,backvalue=False)
        return result[1]
    def del_table(self, tb_name):
        return self.drop_table(tb_name)

    #2.4 复制表
    def copy_table(self, tb_old_name, tb_new_name, copy_data=False):
        '''
        :param tb_name: old table name
        :param tb_new_name: create new table name
        :param copy_all:true 复制数据,false仅仅拷贝表结构
        :return:None
        '''
        if not self.is_exist_table(tb_old_name):return -1
        copyall = 'like' if copy_data==False else 'as select * from'
        sql = 'create table if not exists %s %s %s' % (tb_new_name, copyall, tb_old_name)
        result=self.__run(sql, commit=True, backvalue=False)
        return result[1]

    #2.5 清空数据表
    def del_table_datas(self, tb_name,fast_del=True):#保留字段
        """
        :param tb_name: str
        :param fast_del: bool;快速删除表数据不能恢复
        :return: None

        说明:
             TRUNCATE TABLE 数据表名
             #删除后无法恢复;不能删除参与索引和视图的表
        """
        if not self.is_exist_table(tb_name):return -1
        sql = "truncate table " if fast_del else "DELETE FROM "
        sql = sql+"%s" % tb_name
        result=self.__run(sql,commit=True,backvalue=False)
        return result[1]
    #2.5 清空数据表
    def clear_table_datas(self,tb_name,fast_del=True):#保留字段
        return self.del_table_datas(tb_name,fast_del)

    #3.表结构
    #========================================================================
    # 修改表信息
    '''
    ALTER TABLE tb_name#增加一个字段 多个命令之间用逗号分割
            ADD col_name int not null  [FIRST|AFTER col_name1]   #添加字段
            ADD primary key (col_name1)                          #添加首键
            ADD [UNIQUE]  INDEX [index_name] (col_name1,...)     #添加索引
            ALTER col_name {SET DEFAULT 0 |DROP DEFAULT}         #修改默认值
            CHANGE col_name  int(5) not null auto_increment      #修改列属性
            DROP col_name           #删除列名
            DROP PRIMARY KEY        #删除首键
            DROP INDEX index_name   #删除索引
            RENAME  tb_new_name     #修改表名
     '''
    #3.01显示表结构
    def __SHOW_FULL_COLUMNS(self,tb_name):
        """
        :param tb_name:str
        :return:显示表结构
        
        """
        if not self.is_exist_table(tb_name): return None
        sql='show full columns from %s '%(tb_name)
        return  self.__run(sql)[0]

    #3.02 显示表结构
    def __DESC(self,tb_name,col_name=''):
        '''
        :param tb_name: str
        :param col_name: str 模板包含%,_
        :return: list or None

        实例:DESC查询结果
        [{'Field':'date','Type':'date','Null':'YES','Key':'UNI','Default':'2019-01-28','Extra': ''},...]

        select * from information_schema.COLUMNS where TABLE_NAME='m1809';
           #TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLUMN_DEFAULT()默认值,IS_NULLABLE,DATE_TYPE,
           COLUMN_TYPE,COLUMN_KEY,EXTRA,CHARACTER_SET_NAME,COLLATION_NAME
        '''
        if not self.is_exist_table(tb_name):return None
        sql='DESC %s  %s '%(tb_name,col_name)
        return  self.__run(sql)[0]

    
    # 3.011 更改列的类型:
    def __MODIFY_COL(self, tb_name, col_name='', col_options=''):
        '''
        :param tb_name: str;
        :param col_name: str;
        :param col_options: str;列的类型
        :return: None

        ALTER TABLE t1 MODIFY column_b BIGINT NOT NULL
        '''
        if self.is_exist_table(tb_name) and self.all_exist_cols(tb_name, col_name):
            sql = 'Alter Table %s MODIFY %s %s' % (tb_name, col_name, col_options)
            result = self.__run(sql, commit=True, backvalue=False)
            return result[1]
        else:
            self.err = tb_name + " or " + col_name + " not exist! __CHANGE_COL() fail. "
            return -1
    #3.012 更改列名并修改列属性--更改列名必须指定类型
    def __CHANGE_COL(self, tb_name, col_name='',col_new_name='',col_options=''):
        '''
        :param tb_name:
        :param col_name: str;旧列名
        :param col_options: str;字段属性;如 int(5) not null auto_increment
        :param col_new_name: str;修改的列名
        :return: None

        ALTER TABLE m1809 CHANGE col_name col_new_name int(5) [not null auto_increment]
        '''
        if not self.is_exist_table(tb_name): return -1
        if not self.all_exist_cols(tb_name, col_name):return -1
        if (col_name.lower()!=col_new_name.lower()) and self.is_exist_col(tb_name,col_new_name):return -1

        sql = 'alter table %s change %s %s %s'%(tb_name, col_name, col_new_name,col_options)
        result=self.__run(sql, commit=True, backvalue=False)
        return result[1]


    #3.013 更改列的类型:
    def change_col(self, tb_name, col_name='', col_options='',sql_exist_col_name=False):
        """
        :param tb_name: str
        :param col_name: str
        :param col_options: str;
               sql_exist_col_name=false时语句类似格式:
                        int(5) [not null auto_increment]
               sql_exist_col_name=True时语句类似格式:
                        col_new_name int(5) [not null auto_increment]
                        同时必须指定__CHANGE_COL参数col_new_name=''
        :param sql_exist_col_name: bool
        :return: None

        ALTER TABLE m1809 CHANGE col_name col_new_name int(5) [not null auto_increment]
        """
        col_new_name=''if sql_exist_col_name else col_name
        return self.__CHANGE_COL(tb_name, col_name, col_new_name, col_options)

    #3.014 获取表字段信息
    def __COLUMNS_DICT(self, tb_name,col_name=''):
        '''
        :param tb_name: str
        
        :param col_name: str;指定列返回指定数据,缺失返回全部列类型options;只能指定1列
        :return: 返回dict{{列名1:类型11,...},{列名1:类型21,...},...}
                          列名为{'Field'},类型为{'Type,Null,Key,Default,Extra'}
        '''
        type_={};null={};key={};default={};extra={}
        if  self.is_exist_table(tb_name):
            lst=self.__DESC(tb_name,col_name)
            if lst:
                for i in lst:
                    type_[i['Field']] = i['Type']
                    null[i['Field']] = i['Null']
                    key[i['Field']] = i['Key']
                    default[i['Field']] = i['Default']
                    extra[i['Field']] = i['Extra']
        return {'type':type_,'null':null,'key':key,'default':default,'extra':extra}

    def __COLUMNS_LIST(self, tb_name,col_name=''):
        '''
        :param tb_name: str
        :param col_name: str;返回dict指定列,缺失返回全部列类型options;只能指定1列
        :return: 返回dict{'field','type','null','key','default','extra'}
        '''
        field=[];type_=[];null=[];key=[];default=[];extra=[]
        if  self.is_exist_table(tb_name):
            lst=self.__DESC(tb_name,col_name)
            if lst:
                for i in lst:
                    field.append(i['Field'])
                    type_.append(i['Type'])
                    null.append(i['Null'])
                    key.append(i['Key'])
                    default.append(i['Default'])
                    extra.append(i['Extra'])
        return {'name':field,'field':field,'type':type_,'null':null,'key':key,'default':default,'extra':extra}

    def COLUMNS(self, tb_name, col_name='',back_dict_dict=False):
        """
        :param tb_name: str
        :param col_name: str
        :param back_dict_dict: False返回{list,list,...},True返回{字典,...}
        :return: 返回字典列表或字典的字典
        """
        if not back_dict_dict:
            return self.__COLUMNS_LIST(tb_name,col_name)
        else:
            return self.__COLUMNS_DICT(tb_name, col_name)

    #3.1.11 显示列名        #返回str的list
    def get_cols_names(self, tb_name):
        return self.COLUMNS(tb_name)['field']
    def get_col_name(self, tb_name,col_name):
        lst= self.COLUMNS(tb_name,col_name)['field']
        return lst[0] if lst else ''
    
    #3.1.12更改列名
    def rename_col(self, tb_name, col_name='',col_new_name=''):
        '''
        ALTER TABLE m1809 CHANGE col_name col_new_name int(5) [not null auto_increment]
        '''
        if not self.is_exist_table(tb_name) and not self.is_exist_col(tb_name,col_name):return -1
        if self.is_exist_col(tb_name,col_new_name):return -1
        type_=self.COLUMNS(tb_name,col_name)['type']
        if not bool(type_):return -1
        return self.__CHANGE_COL(tb_name, col_name, col_new_name, type_[0])

    #3.1.13 多列名是否存在判断
    def __IS_EXIST_COLS(self,tb_name,cols_names='',cols_options='',all_cols=False):
        """
        :param tb_name: str
        :param cols_names: str;类似'id,name,age,...'最后无逗号;
        :param cols_options: str;
               类似"id int not null,name varchar(10) default '',..."最后无逗号
        :param all_cols =True所有列都存在结果为True
        :return: bool
        """
        if not self.is_exist_table(tb_name):return False
        #获取参数的列名
        if cols_names!='':
            lst=cols_names.lower().split(',')
        else:
            lst=[i.split()[0] for i in cols_options.lower().split(',')]
        n=len(lst)
        #获取数据库列名
        sql = "select column_name from information_schema.COLUMNS where TABLE_NAME='%s' "
        if n==1:
            sql = sql+"having column_name='%s'" 
            sql=sql % (tb_name, lst[0])
        elif n>1:
            sql=sql % (tb_name)
        else:return False
        
        result = self.__run(sql,back_lst=True)[0]
        if not bool(result):return False
        db_col_names=[i[0].lower() for i in result]
        #判断
        result = [v for v in lst for v1 in db_col_names if v == v1]
        return (all_cols and len(result) == len(lst)) or ((not all_cols) and (len(result) > 0))

    # 3.1.14 列名是否存在判断(多列(any,all),单列)
    def IS_EXIST_COL(self,tb_name,cols_names='',cols_options='',all_cols=False):
        return  self.__IS_EXIST_COLS(tb_name, cols_names, cols_options, all_cols)
    #3.1.14 列名是否存在判断
    def is_exist_col(self,tb_name,col_name=''):
        sql="select column_name from information_schema.COLUMNS where TABLE_NAME='%s' " \
            "having column_name='%s'"%(tb_name,col_name)
        result = self.__run(sql)
        return bool(result[0]) and (result[1]==None)

    def any_exist_cols(self,tb_name,cols_names='',cols_options=''):
        """
        :param tb_name: str
        :param cols_names: str;类似'id,name,age,...'最后无逗号
        :param cols_options: str;
               类似"id int not null,name varchar(10) default '',..."最后无逗号
        :return: bool
        """
        return self.__IS_EXIST_COLS(tb_name,cols_names,cols_options,False)
    def all_exist_cols(self,tb_name,cols_names='',cols_options=''):
        return self.__IS_EXIST_COLS(tb_name,cols_names,cols_options,True)

    # 3.1.15 列是否是首键
    def is_exist_PRI(self, tb_name,col_name=''):
        col_name= col_name if col_name else ''
        return 'PRI' in self.COLUMNS(tb_name, col_name)['key']
    # 3.1.16 列是否是唯一值键
    def is_exist_UNI(self, tb_name,col_name=''):
        col_name = col_name if col_name else ''
        return 'UNI' in self.COLUMNS(tb_name, col_name)['key']
    
    #3.1.21 获取列的数据类型
    def get_col_type(self, tb_name,col_name):
        """
        :param tb_name: str
        :param col_name: str
        :return: str
        """
        d=self.COLUMNS(tb_name,col_name,True)['type']
        return d[list(d.keys())[0]] if d else ''
    def get_cols_types(self, tb_name):
        """
        :param tb_name: str
        :return: {列名1:类型1,...}
        """
        return self.COLUMNS(tb_name,'',True)['type']

    def GET_DATA_TYPES(self,tb_name,col_name=None):
        """
        :param tb_name:str
        :param col_name:str 列名无返回全部,指定返回指定列python数据类型
        :return:字典{列名:数据类型,...}
        select * from information_schema.COLUMNS where TABLE_NAME='m1809';
           #TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLUMN_DEFAULT()默认值,IS_NULLABLE,DATA_TYPE,
           COLUMN_TYPE,COLUMN_KEY,EXTRA,CHARACTER_SET_NAME,COLLATION_NAME
           (('No', 'int'), ('name', 'char'),...)
        """
        d={}
        if not self.is_exist_table(tb_name): return d
        sql="select COLUMN_NAME,DATA_TYPE from information_schema.COLUMNS where TABLE_NAME='%s' "
        if col_name:
            sql=sql+" having COLUMN_NAME='%s'"
            sql=sql %(tb_name,col_name)#必须分开书写,合并一行错误
        else:
            sql =sql %(tb_name)
        lst=self.__run(sql,back_lst=True)[0]
        if not bool(lst): return d
        for i in lst:
            d[i[0]]=i[1]
        return d

    #3.1.31 显示索引2019/2/24
    def __SHOW_INDEXS(self, tb_name,col_name='',back_dict_list=False,include_PRI=True):
        """
        :param tb_name: str
        :return:back_dict_list=False dict={'col_name':index_name,...}
                back_dict_list=True  dict{'name':[...],'idx'[...]}

        show index from m1809显示结果:
        [{'Table': 'm1809', 'Non_unique': 0, 'Key_name': 'PRIMARY', 'Seq_in_index': 1,
          'Column_name':    'No', 'Collation': 'A', 'Cardinality': 14, 'Sub_part': None,
          'Packed': None, 'Null': '', 'Index_type': 'BTREE', 'Comment': '',
          'Index_comment': '', 'Visible': 'YES', 'Expression': None},{...},...]
        本方法执行结果类似:
        {'No': 'PRIMARY', 'date': 'date_UNIQUE', 'open': 'idx_m1809_open_high', 'high': 'idx_m1809_open_high'}

        """
        d=dict();lst_name=[];lst_idx=[];sql = 'show index from %s '
        if not self.is_exist_table(tb_name):return d

        if col_name:
            sql=sql+" where Column_name='%s'"
            sql=sql%(tb_name,col_name)
        else:
            sql=sql % (tb_name)

        lst = self.__run(sql)[0]
        if not bool(lst):return d

        for i in lst:
            if back_dict_list:
                if include_PRI or ((not include_PRI) and( i['Key_name']!='PRIMARY')):
                    lst_name.append(i['Column_name'])
                    lst_idx.append(i['Key_name'])
            else:
                if include_PRI or ((not include_PRI) and (i['Key_name'] != 'PRIMARY')):
                    d[ i['Column_name']]=i['Key_name']
        return d if not back_dict_list else {'name':lst_name,'idx':lst_idx}
    #3.1.32显示全部索引
    def get_indexs(self, tb_name,back_dict_list=False,include_PRI=True):
        """
        :param tb_name: str
        :return: dict={'col_name':index_name,...}
        """
        return self.__SHOW_INDEXS(tb_name,back_dict_list=back_dict_list,include_PRI=include_PRI)
    #3.1.33显示指定列索引名
    def get_index(self, tb_name,col_name,include_PRI=True):
        d=self.__SHOW_INDEXS(tb_name,col_name,include_PRI=include_PRI)
        return list(d.values())[0] if d else ''
    # 3.1.34 列是否是索引判断
    def is_exist_index(self, tb_name, col_name='',include_PRI=True):
        d=self.__SHOW_INDEXS(tb_name,col_name,include_PRI=include_PRI)
        return bool(d)

    # 3.1.41 显示自增的列名
    def get_auto_increment_name(self, tb_name):
        """
        :param tb_name: str
        :return: str list
        说明:自增只能设置1个
        """
        lst=self.__SHOW_CREATE_TABLE(tb_name)
        if not (bool(lst) or self.is_exist_table(tb_name)):return ''

        lst=[i for i in lst if 'AUTO_INCREMENT' in i.upper() ]
        if not bool(lst):return ''
        result=lst[0].split()
        return result[0].replace('`','') if result else ''

    def get_AI(self, tb_name):
        return self.get_auto_increment_name(tb_name)
    # 3.1.51获取列的mysql语句
    def __SHOW_CREATE_TABLE(self, tb_name):
        """
        :param tb_name:str
        :return: 字符串list;类似下面
                lst= ['`m1809` (', '`NO` int(11) NOT NULL AUTO_INCREMENT',
                    '`name` char(20) NOT NULL', "`code` char(10) DEFAULT 'm'",
                    "`datetime` datetime DEFAULT '2019-01-28 22:46:30'",
                    "`date` date DEFAULT '2019-01-28'", "`time` time DEFAULT '22:46:00'",
                    "`open` float DEFAULT '100'", "`high` float DEFAULT '200'",
                    "`low` float DEFAULT '300'", "`close` float DEFAULT '400'",
                    '`vol` int(11) NOT NULL', '`备注` varchar(45) DEFAULT NULL',
                    '`备注remark` varchar(45) DEFAULT NULL', '`NO`)',
                    '`date_UNIQUE` (`date`)', '`idx_m1809_open_high` (`open``high`)']

                lst= [{'Table': 'm1809',\n ...
                       `备注remark` varchar(45) DEFAULT NULL\n) ENGINE=...]

        mysql语句:'show create table m1809;'显示结果
            [{'Table': 'm1809', 'Create Table': "CREATE TABLE `m1809` (\n
              `No` int(11) NOT NULL AUTO_INCREMENT,\n`name` char(20) NOT NULL,\n
              `datetime` datetime DEFAULT '2019-01-28 22:46:30',\n
              `date` date DEFAULT '2019-01-28',\n`time` time DEFAULT '22:46:00',\n
              `open` float DEFAULT NULL,\n`high` float DEFAULT '300',\n
              `vol` int(11) DEFAULT '200',\n  PRIMARY KEY (`No`),\n
              UNIQUE KEY `date_UNIQUE` (`date`),\n
              KEY `idx_m1809_open_high` (`open`,`high`)\n)
              ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci"}]
        """
        if not self.is_exist_table(tb_name): return None

        sql = 'show create table %s ' % (tb_name)
        lst=str(self.__run(sql)[0])
        if not bool(lst): return None

        lst=lst.split('\\n')
        p = re.compile(r"(`\w+`.*)")
        lst=[p.findall(i)[0].replace(',','') for i in lst if p.findall(i)]
        return lst

    # 3.1.42列是否存在自增属性
    def is_exist_AI(self, tb_name, col_name=''):
        if not self.is_exist_table(tb_name): return False
        ai_name = self.get_auto_increment_name(tb_name)
        return bool(ai_name) if col_name=='' else (col_name.lower()==ai_name.lower())

    #3.1.51获取列的mysql语句
    def get_col_order_str(self, tb_name,col_name):
        if not self.is_exist_tb_cols(tb_name,col_name): return ''
        col_name =r'`'+col_name+r'`'
        lst=self.__SHOW_CREATE_TABLE(tb_name)
        if not bool(lst):return ''
        lst=[i for i in lst if col_name.lower() in i.lower()]
        if not bool(lst): return ''
        return lst[0].replace('`','').replace("'",'').replace('"','')

    #-------------------------------------------------------------------------
    #3.2.11 添加字段
    def add_cols(self, tb_name, cols_options=''):
        """
        :param tb_name: str
        :param cols_options: str;
               类似"id int not null,name varchar(10) default "Bob",..";最后无逗号
        :return: None
        
        Mysql语句:
        ALTER TABLE tb_name#增加一个字段 多个命令之间用逗号分割
            ADD col_name int not null  [FIRST|AFTER col_name1]  #添加字段
        """
        if not self.is_exist_table(tb_name):return -1
        if self.any_exist_cols(tb_name,cols_options=cols_options):return -1
        sql = 'alter table %s  add  %s ' % (tb_name,cols_options)
        result=self.__run(sql, commit=True,backvalue=False)
        return result[1]
         
    #3.2.12 删除字段
    def del_cols(self, tb_name,cols_names=''):
        """
        :param tb_name: str
        :param cols_names: str;类似"id,name,age,...";最后无逗号
        :return: None
        
        Mysql语句:
        ALTER TABLE tb_name #增加一个字段 多个命令之间用逗号分割
            DROP col_name   #删除列名
        """
        if not self.is_exist_table(tb_name): return -1
        if not self.all_exist_cols(tb_name,cols_names):return -1
        sql = 'alter table %s drop %s ' % (tb_name,cols_names)
        result=self.__run(sql, commit=True,backvalue=False)
        return result[1]
    #删除自增键
    def del_AI(self,tb_name):
        if not self.is_exist_table(tb_name): return -1
        ai_name=self.get_auto_increment_name(tb_name)
        if not bool(ai_name):return None
        sql=self.get_col_order_str(tb_name,ai_name).upper()
        if not bool(sql):return -1
        sql=sql.replace('AUTO_INCREMENT','')

        return self.change_col(tb_name,ai_name,sql,sql_exist_col_name=True)
    #获取自增AI的值
    def get_last_AI_VALUE(self, tb_name):
        if not self.is_exist_table(tb_name):return None
        sql='select @@IDENTITY'
        result=self.__run(sql)[0]
        return result
    #添加自增键
    def set_AI(self, tb_name, col_name):
        return self.add_AI(tb_name,col_name)
    def add_AI(self,tb_name,col_name):
        if not (self.is_exist_table(tb_name) or self.is_exist_col(tb_name,col_name)): return -1

        ai_name = self.get_auto_increment_name(tb_name)
        if bool(ai_name) and(ai_name.lower()==col_name.lower()): return None
        if bool(ai_name) and (ai_name.lower() != col_name.lower()): return -1

        col_type=self.get_col_type(tb_name,col_name).lower()
        if 'int'not in col_type:return -1

        sql = self.get_col_order_str(tb_name, col_name)
        if not bool(sql): return -1

        sql = sql+' AUTO_INCREMENT'
        return self.change_col(tb_name, col_name, sql,sql_exist_col_name=True)
    #3.2.13 添加首键
    def set_PRI(self, tb_name, pri_cols_names='', replace=False):
        return self.add_primary_key(tb_name, pri_cols_names,replace)
    def add_primary_key(self, tb_name, pri_cols_names='',replace=False):
        """
        :param tb_name: str;
        :param pri_cols_names:str;类似"name,age,...";最后无逗号 
        :param replace:bool;False存在首键不替换,True存在首键替换
        :return: None
        
        Mysql语句:
        ALTER TABLE tb_name#增加一个字段 多个命令之间用逗号分割
            ADD primary key (col_name1,...)      #添加首键
        """
        if not self.is_exist_table(tb_name): return -1
        if self.is_exist_PRI(tb_name) and not replace: return -1
        if self.is_exist_PRI(tb_name) and replace:self.del_primary(tb_name)

        sql = 'alter table %s  add primary key ( %s )' % (tb_name, pri_cols_names)
        result=self.__run(sql, commit=True, backvalue=False)
        return result[1]

    #3.214 删除首键
    def del_PRI(self, tb_name):
        return self.del_primary_key(tb_name)
    def del_primary_key(self, tb_name):
         '''
         Mysql语句:
         ALTER TABLE tb_name            #增加一个字段 多个命令之间用逗号分割
                DROP PRIMARY KEY        #删除首键
         '''
         if not (self.is_exist_table(tb_name) or self.is_exist_PRI_tb(tb_name)) :return -1
         if  self.is_exist_AI(tb_name):
             if self.del_AI(tb_name):return -1

         sql = 'alter table %s drop primary key '%(tb_name)
         result=self.__run(sql, commit=True, backvalue=False)
         return result[1]

    def drop_primary_key(self, tb_name):
        return self.del_primary_key(tb_name)

    def __get_key(self, tb_name):
        if not self.is_exist_table(tb_name): return None
        d = self.COLUMNS(tb_name, back_dict_dict=True)
        if not bool(d): return None
        return d['key']

    def get_key(self, tb_name):
        return self.__get_key(tb_name)
    def get_PRI(self, tb_name):
        """
        :param tb_name: str
        :return: [str]
        """
        d = self.__get_key(tb_name)
        if not(d):return None
        return [k for k in d if d[k] == 'PRI' ]
    #3.2.15 添加索引
    def set_UNI(self, tb_name, col_name, index_name=None):
        """
        :param tb_name: str
        :param index_name: str or None
        :param col_name: str
        :return: None
        """
        if index_name==None:index_name=''
        return self.set_index(tb_name, index_name, col_name, unique_idx=True)

    def del_UNI(self, tb_name, index_name='', col_name=''):
        """
        :param tb_name: str
        :param index_name: str
        :param cols_names: str
        :return: None
        """
        if not bool(self.is_exist_table(tb_name)):return -1
        if index_name==''and col_name=='':return -1
        if index_name=='':
            index_name=self.get_UNI_NAME(tb_name,col_name)
            if not bool(index_name):return -1
        return self.__DEL_INDEX(tb_name, index_name)

    def get_UNI(self, tb_name, col_name=''):
        """
        :param tb_name: str
        :param col_name: str
        :return:str or [str]; col_name存在返回UNI列名;col_name=''返回全部UNI列名
        """
        d = self.__get_key(tb_name)
        if col_name=='':
            return [k for k in d if d[k] == 'UNI'] if d else None
        else:
            lst= [k for k,v in d.items() if v == 'UNI' and k.lower()==col_name.lower()] if d else ''
            return lst[0] if lst else ''

    def get_UNI_NAME(self, tb_name, col_name='', back_UNI_Dict=True):
        """
        :param tb_name: str
        :param col_name: str;无指定返回全部UNI索引名称
        :param back_UNI_Dict:bool True
        :return:str,list,dict
                str:指定col_name返回指定列的索引名称
                list:返回 [UNI索引名称],dict:返回{列名:UNI索引名称}
        实例:
        print('2.show all index name=\n', db.get_indexs(tb_name, include_PRI=False))
        # {'date': 'date_UNIQUE', 'open': 'idx_m1809_open_high', 'high': 'idx_m1809_open_high'}
        """
        d = self.__get_key(tb_name)
        if not bool(d):return '' if col_name else None

        uni_lst=[k for k in d if d[k] == 'UNI']
        idx_dict=self.get_indexs(tb_name, include_PRI=False)
        if not(bool(uni_lst) or bool(idx_dict)):return '' if col_name else None

        if col_name:
            col_name=self.get_col_name(tb_name,col_name)
            if not(bool(col_name)):return ''
            return idx_dict[col_name] if (col_name in idx_dict.keys()) else ''
        else:
            if back_UNI_Dict:
                result={k:v for n,k,v in zip(uni_lst,idx_dict.keys(),idx_dict.values()) if n==k }
                return result if result else None
            else:
                result=[v for n, k, v in zip(uni_lst,idx_dict.keys(),idx_dict.values()) if n == k]
                return result if result else None

    def set_index(self, tb_name, index_name,cols_names='',unique_idx=False):
        """
        :param tb_name: str;
        :param index_name: str;可省略;pymysql默认名称是第一个列名;
               mysql默认名称类似'date_UNIQUE','idx_m1809_open_high'
        :param idx_cols_names: str;类似"id,name,...";最后无逗号
        :param unique: bool
        :return: None

        Mysql语句:
        ALTER TABLE tb_name#增加一个字段 多个命令之间用逗号分割
            ADD [UNIQUE]  INDEX [index_name] (col_name1(length),...)#添加索引
        注意:
            避免在同一列创建多个索引
        """
        if (not self.is_exist_table(tb_name)) or (cols_names==''):return -1
        if not self.all_exist_cols(tb_name,cols_names):return -1

        lst_names=cols_names.split(',')
        if bool([i for i in lst_names if self.is_exist_index(tb_name,i,False)]):return -1
        if bool([i for i in lst_names if self.is_exist_PRI(tb_name, i)]): return -1
        if unique_idx:
           if bool([i for i in lst_names if self.is_exist_UNI(tb_name, i)]): return -1

        unique_str='' if unique_idx==False else 'unique'
        sql = 'alter table %s add %s index %s ( %s ) '%(tb_name,unique_str,index_name,cols_names)
        result=self.__run(sql, commit=True, backvalue=False)
        return result[1]

    #3.216 删除索引
    def __DEL_INDEX(self, tb_name, index_name=''):
        sql = 'drop index %s ON %s' % (index_name,tb_name)
        result=self.__run(sql, commit=True, backvalue=False)
        return result[1]

    def del_index(self, tb_name, index_name='',col_name=''):
        """
        :param tb_name: str
        :param index_name: str;如为空,删除全部(除首键外)
        :param col_name: str;如果列上有索引删除
        :return: None

        DROP INDEX index_name on tb_name  #删除索引
        """
        if not self.is_exist_table(tb_name): return -1
        tb_idx=self.get_indexs(tb_name,include_PRI=False)
        if not bool(tb_idx):return -1

        if index_name:
            if not bool([i for i in tb_idx.values() if i.lower()==index_name.lower()]):return -1
            return self.__DEL_INDEX(tb_name,index_name)
        elif col_name:
            if not self.is_exist_index(tb_name,col_name,include_PRI=False):return -1
            col_name=self.get_col_name(tb_name,col_name)
            return self.__DEL_INDEX(tb_name,tb_idx[col_name])
        else:
            idx_set={i for i in tb_idx.values()}
            runerr= {-1 for i in idx_set if self.__DEL_INDEX(tb_name,i)==-1}
            return runerr[0] if runerr else None

    def is_exist_tb_cols(self, tb_name, col_names=''):
        tb=self.is_exist_table(tb_name)
        if tb:
            return (tb if col_names==''else self.all_exist_cols(tb_name,col_names))
        else:
            return tb

    #3.217 修改默认值:数字型可为数字,str
    def alter_default_value(self, tb_name, col_name='',default_value=''):
        '''
        说明:首键不能设置默认值
        Mysql语句:
        ALTER TABLE tb_name#增加一个字段 多个命令之间用逗号分割
               ALTER col_name {SET DEFAULT 0 |DROP DEFAULT} #修改默认值
        说明:本语句有些默认值不能修改,改用change
        '''
        if not self.is_exist_tb_cols(tb_name,col_name):return -1
        if self.is_exist_PRI(tb_name,col_name):return -1

        sql = "alter table %s alter %s set default '%s' " % (tb_name, col_name,default_value)
        result=self.__run(sql, commit=True, backvalue=False)
        return result[1]


    # 3.217 修改默认值:数字型可为数字,str
    def set_default_value(self, tb_name, col_name='', default_value=''):
        return self.alter_default_value(tb_name, col_name,default_value)
    def change_default_value(self, tb_name, col_name='', default_value=''):
        return self.alter_default_value(tb_name, col_name,default_value)
    #3.218 删除默认值
    def drop_default_value(self, tb_name, col_name=''):
        ''''
        Mysql语句:
        ALTER TABLE tb_name#增加一个字段 多个命令之间用逗号分割
               ALTER col_name {SET DEFAULT 0 |DROP DEFAULT} #修改默认值
        说明:本语句有些默认值不能修改,改用change
        '''
        if not self.is_exist_tb_cols(tb_name,col_name):return -1
        sql = 'alter table %s alter %s drop default ' % (tb_name, col_name)
        result=self.__run(sql, commit=True, backvalue=False)
        return result[1]

    def del_default_value(self, tb_name, col_name=''):
        return self.drop_default_value(tb_name, col_name)

    # 3.219 get默认值
    def get_default_values(self, tb_name, col_name='',back_dict_dict=False):
        d=self.COLUMNS(tb_name, col_name,back_dict_dict=back_dict_dict)
        return d['default'] if d else None
    # ==========================================================================
    # ===========================================================================
    def insert_data(self, tb_name,cols_names='',cols_values=[[]],repeat_value=False,
                    UpdataCols='',UpdataValues='',priority='',include_AI=True):
        '''
        MySql语句:
        INSERT[LOW_PRIORITY | DELAYED | HIGH_PRIORITY][IGNORE]#插入的优先级
                [INTO] tb_name[(字段1,...)]
                VALUES({值| DEFAULT},...),(...),
                [  ON DUPLICAE KEY UPDATE 字段名=表达式,...]#唯一键或首键重复时根据表达式修改数据

        用途:插入数据
        参数:
        tb_name:str;table name
        cols_names:str;
                  str:  类似 "id,name,...";最后无逗号
        cols_values:str list;
                  序列:类似[["123","Tom",...],[...],...]列名的长度和下面的赋值数量必须相等

        priority:  str{'LOW_PRIORITY' | 'DELAYED' | 'HIGH_PRIORITY']['IGNORE'}#插入的优先级
        
        repeat_value:bool;True当出现unique key,primary key重复时系统自动修改;默认修改全部
        UpdataCols:str;当指定repeat_value=True,要更新的列名
        UpdataValues:str;当指定repeat_value=True,要更新的数值
        警告:设置自增后insert写法:自增列数据输入0或null;不允许其他值
        
        mysql实例:
        insert into  m1809 (no,name,date) VALUES (7,'Bob2','2019-02-04'),(8,'Tom3','2019-02-05') on
               DUPLICATE KEY UPDATE name = VALUES(name);#当出现重复值时用新值替代旧值

        pymysql实例:
        con=pymysql.connect( host='localhost',user='root',password='root',database='new_futures')
        cursor.executemany("INSERT INTO m1809 (No,name) VALUES (%s,%s)",
                                                    [['11','Tom'],['12','Bob'],['13','Jim']])
        con.commit()

        说明:用多行插入,采用executemany函数。必须指定参数executemany=True

        '''
        if not self.is_exist_tb_cols(tb_name, cols_names): return -1  # 表名列名不存在退出
        cols_names_lst=cols_names.lower().split(',')
        n_cols_names=len(cols_names_lst)
        try:
            #参数不正确退出
            if n_cols_names<1:return -1
            if [-1 for v in cols_values if len(v)!=n_cols_names ]:return -1
            # 排除自增键
            if not include_AI:
                ai_col = self.get_AI(tb_name)
                if bool(ai_col) and bool([i for i in cols_names_lst if i.lower() == ai_col.lower()]):
                    return -1

            sql="INSERT INTO %s ( %s )" %(tb_name,cols_names)
            sql=sql+" VALUES ("+ ('%s,'*n_cols_names).rstrip(',') +  ")"

            if repeat_value:
                UpdataCols_lst=UpdataCols.split(',')
                UpdataValues_lst=UpdataValues.split(',')
                n=len(UpdataValues_lst)

                if not((len(UpdataCols_lst)==n) and  bool(UpdataValues_lst)):return -1
                lst= [' %s = %s '%(c,v) for c,v in zip(UpdataCols_lst,UpdataValues_lst)]
                if not lst:return -1
                sql = sql + " on DUPLICATE KEY UPDATE "+','.join(lst)

            result=self.__run(sql,commit=True,backvalue=False,executemany=True,args=cols_values)#提升速度
            return result[1]
        except Exception as  e:
            return -1

    #插入查询的数据
    def insert_select_data(self, tb_name,cols_names='',SELECT='',repeat_value=False,
                           UpdataCols='',UpdataValues='',include_AI=True,priority=''):
        '''
        MySql语句:
        INSERT[LOW_PRIORITY | HIGH_PRIORITY][IGNORE]#插入的优先级
                [INTO] tb_name[(字段1,...)]
                SELECT...
                [  ON DUPLICAE KEY UPDATE 字段名=表达式,...]#唯一键或首键重复时根据表达式修改数据
        用途:插入查询的数据
        参数:
        tb_name:str;table name
        cols_names:str; 类似 "id,name,...";最后无逗号
        priority:  str{'LOW_PRIORITY'| 'HIGH_PRIORITY']['IGNORE'}#插入的优先级

        repeat_value:bool;True当出现unique key,primary key重复时系统自动修改;
        UpdataCols:str;   当指定repeat_value=True,要更新的列名
        UpdataValues:str;当指定repeat_value=True,要更新的数值
        警告:设置自增后insert写法:自增列数据输入0或null;不允许其他值

        mysql实例:
        insert into  m1809 (no,name,date) SELECT no,name,date from m1812
        on DUPLICATE KEY UPDATE name = VALUES(name);#当出现重复值时用新值替代旧值
        '''
        if not self.is_exist_tb_cols(tb_name, cols_names): return -1  # 表名列名不存在退出
        cols_names_lst=cols_names.lower().split(',')
        try:
            #参数不正确退出
            if not cols_names_lst:return -1
            # 排除自增键
            if not include_AI:
                ai_col = self.get_AI(tb_name)
                if bool(ai_col) and bool([-1 for i in cols_names_lst if i == ai_col.lower()]):
                    return -1

            sql = "INSERT INTO %s ( %s ) " % (tb_name, cols_names)
            sql = sql + SELECT+" "

            if repeat_value:
                UpdataCols_lst = UpdataCols.split(',')
                UpdataValues_lst = UpdataValues.split(',')
                n = len(UpdataValues_lst)

                if not ((len(UpdataCols_lst) == n) and bool(UpdataValues_lst)): return -1
                lst = [' %s = %s ' % (c, v) for c, v in zip(UpdataCols_lst, UpdataValues_lst)]
                if not lst: return -1
                sql = sql + " on DUPLICATE KEY UPDATE " + ','.join(lst)

            result = self.__run(sql, commit=True, backvalue=False)
            return result[1]
        except Exception as  e:return -1

    # -------------------------------------------------------------------------
    #更新数据
    def update_data(self, tb_name,cols_names='',values='',where_condition='',limit_n=0,priority=''):
        '''
        MySql语句:
            UPDATE[LOW_PRIORITY| IGNORE] 数据表名#优先级顺序
                SET 字段1=值1【,字段2=值2,...】
               【WHERE 条件表达式】
               【ORDER BY...】#限定表中被修改的行的次序
               【LIMIT 行数】 #限定被修改的行数

        实例:
        update m1809 set No=22,name="Smith" where name="Tom" limit 20
        说明:
        本函数无限定唯一键首键自增限制,更新数据时要注意。
        '''
        if  not self.is_exist_table(tb_name):return -1
        try:
            cols_names_lst = cols_names.split(',')
            values_lst = values.split(',')
            n=len(cols_names_lst)
            if (n!=len(values_lst)) or (n<1):return -1

            lst=[(v1+ '= '+"'"+v2+"'") for v1,v2 in zip(cols_names_lst,values_lst)]
            cols_names_values=' , '.join(lst)
            where_con = " where " + where_condition if where_condition != '' else ''
            limit_con = ' limit ' + str(limit_n) if limit_n > 0 else ''
            sql = "UPDATE " + priority + " " + tb_name + " SET " + cols_names_values + where_con + limit_con

            result = self.__run(sql, commit=True, backvalue=False)
            return result[1]

        except Exception as  e:return -1

    #删除数据
    def del_data(self, tb_name,where_condition='',order_by_cols_desc='',limit_n=0,priority=''):
        """
        :param tb_name: str
        :param where_condition: str;
        :param limit_n: int >0
        :param priority: str;【LOW_PRIORITY|QUICK】【IGNORE】
        :return: None

        mysql语法:
        DELETE 【LOW_PRIORITY|QUICK】【IGNORE】FROM tb_name
                    【WHERE 条件表达式】
                    【ORDER BY col_name [DESC],col_name [desc],...】对结果排序
                    【LIMIT 行数】      #删除时会在事务日志中添加一条记录
        实例:
        delete from m1809 where id=11,user='Tom';
        """
        # tb_name,where_condition='',limit_n=0,priority=''
        if not self.is_exist_table(tb_name): return -1

        try:
            limit_str = " limit %s" % (str(int(limit_n))) if limit_n > 0 else ""

            sql="delete "+priority+" from %s where "%(tb_name)
            sql=sql+where_condition+" "+order_by_cols_desc+limit_str

            result = self.__run(sql, commit=True, backvalue=False)
            return result[1]

        except Exception as  e:
            return -1

    # 4.查询操作
    # =========================================================================
    def select(self,cols_names='',tb_names='',where='',group_by='',
               having='',limit=(),order_by_desc=False,order_by_cols='',
               distinct=False,back_list=False):
        """
        :param cols_names:str str;可为空默认全部
               类似"db1.tb1.id,db2.tb2.name,age,...;*;函数count(*);avg(vol)
        :param tb_names:str;
               类似"db1.tb1,db2.tb2,..."
        :param where_,having str;类似
               #逻辑运算符:id>1; id in (2,3,4) and date<'1988-12-1';
               #集合查询:  user not in(‘Tom’,’Bob’);
               #范围查询:  id not between 5 and 7;
               #模糊查询_,%匹配单,多字符:user not like ‘%Tom%’;user like ‘T_m’;
               #非空查询:name is not null; user=’Tom’and (name is null or id=<5);
        :param group_by:str;指定分组的列名;类似“id,name,...”
        :param limit:tuple(n1,n) n1:从n1+1行开始,显示n行;缺失显示全部;如(3,);(3,5);
        :param order_by_desc:bool;False查询结果排序默认升序
        :param distinct:bool True去除重复;默认False不去除重复
        :return:[{}]
               # [{'no': 0, 'name': '豆粕', 'date': datetime.date(2018, 7, 9)}

        select 【distinct】 列,… from tb_name1,..【where con 】【group by 列,…】【having con 】
               【limit n1,n】【order by asc[|desc]】;
        
        实例:
        1.5.查询  2019/2/9
        语法:
        select 列,… from tb_name where con group by 列,… having con limit count;
        
        1.单多表查询
        单表查询:select * from tb_name;                #查询所有字段
        多表查询:select tb_1.id,tb_1.user,tb_2.id,tb_2.name from tb_1,tb_2;
        视图查询:select * from (select no,name,open from m111) t where t.no>1;
        指定别名:select id,name as n,sex as s from tb_name where id>1; #name,sex字段别名n,s
        select * from tb_name p where p.talk=’php’;                   #p表的别名
        去除重复:select distinct user,password from tb_name;           #查询指定字段(去除重复)
        结果排序:select * from tb_name order by asc[|desc];
        限制查询:select *from tb_name limit 5;#前5行
        select * from 表 limit 2,5;           #从查询结果的第3行开始,显示5行
        select * from 表 limit 5 offset 2;    #从查询结果的第3行开始,显示5行
         
        2.条件查询
        select * from tb_name where id>1;                              #查询所有字段指定数据
        select * from tb_name where user not in(‘Tom’,’Bob’);     #集合查询;符型加单引号
        select * from tb_name where id in (2,3,4) and date<'1988-12-1';#集合查询 
         
        select * from tb_name where id not between 5 and 7; #范围查询
        select * from tb_name where user not like ‘%Tom%’ having user like ‘T_m’;# 模糊查询_,%匹配单,多字符
        select * from tb_name where name is not null;#非空查询
        select * from tb_name where user=’Tom’and (name is null or id=<5);  
         
        3.分组查询:
         
        用途:汇总统计多条结果(简单查询无法做到),常和聚合函数一起使用
        格式:slect 要显示的结果字段 from tb_name [where 条件] group by 分组字段 [ordey by 字段 asc[|desc]];
        参数:
        要显示的结果字段:
        id,user,count(id),sum(id),avg(id),min(id),max(id),group_concat(分组字段),…
        说明:
        group_concat(分组字段):将分组字段中的每个值都显示出来
        显示的结果类型类似'm1901,m1901,m1901,m1901'
        分组字段:字段1,字段2,…;先按字段1分组,当字段1的值相等时在按字段2分组
        
        实例:
        select 行业, avg(open) from tb_name group by 行业;#分组列一般按大类类别进行
        select 行业,分类,name,count(open),sum(open),avg(open),min(open),max(open) from tb_name group by 分类;
        select 行业,分类,name,count(open),avg(open) from tb_name where open is not null
                                                        group by name order by avg(open) desc;
        select 行业,分类,name,vol from tb_name  group by name,vol,行业;          #多个字段分组
        select 行业,分类,name,vol,group_concat(name) from tb_name  group by name;#name必为分组字段
        
        4.聚合函数 count,sum,min,max,avg,median,std,variance,stddev
        说明:min,max参数可为str,date;median可为date;
         
        select count(*) from tb_name;                #统计表中总数不用where更快
        select count(name) from tb_name;             #统计表中name总数=6  
        select sum(id) as sumvalue  from tb_name;#别名
         select@min_price:=MIN(price),@max_price:=MAX(price) tb_name;
        select * from new_futures.m111 order by rand() limit 5;           #随机取出5条数据
        select * from tb_namewhere datediff('minute',开始时间,getdate())>5;#延时查询
         
        5.子查询(子查询返回结果是1个值时可以比较运算符;当返回列表时用in代替):
         
        select * from tb_login where user in ( select user from tb_book);#user存在2个表中
        select id,books,row from tb_book where row>=
              (select row from tb_row where id=1);# 比较查询
        select * from tb_row where exists (
               select * from tb_book where id=27) and row>90;# 内查询返回True值外查询才进行

        select books,row from tb_book where id< any( select row from tb_row)
        select books,row from tb_book where id>=all( select row from tb_row);
           
        6.连接查询:
         
        6.1内连接查询
        select name,books from tb_login,tb_book where
                               tb_login.user=tb_book.user and row>5;
                               #name在表1中,books在表2中,user在两个表中
        
        6.2外连接查询left join;right join;
        select name,books from tb_login left join tb_book on tb_login.user=tb.book.user
        #name在表1中,books在表2中,user在两个表中;结果包含内连接数据+左表所有数据;并在右表相应列添加null值;
        
        6.3全外连接:full/cross  join不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。
         
        7.合并查询结果
        union [all]   将所有查询结果合并去除相同记录; 
        except [all]  查询结果包括所有在table1中但不在table2 中的行并消除所有重复行
        intersect [all] 查询结果包括所有在table1及table2 中的行并消除所有重复行        
        注:all 简单将结果合并不消除重复行;使用运算词的几个查询结果行必须是一致的

        select user from tb_1 union all  select user from tb_2;          #速度快于union
        (select a from tableA) except (select a from tableB) except (select a from tableC)
        #所有在TableA 中但不在 TableB和TableC 中的行并消除所有重复行
         
        8.正则表达式查询:
         
        select books from tb_book where books 
        regexp ‘^php’;  #字符串开头
        regexp ‘php$’;  #字符串结尾
        regexp ‘p.’;     #匹配任意一个字符
        regexp ‘[abc]’;  #匹配abc中任意一个字符
        regexp ‘[^a-z]’;  #匹配除字符集外任意一个字符
        regexp ‘s1|s2|s3’;#匹配任意一个字符串
        regexp ‘j*a’;    #匹配多个该字符a之前的字符j;j+a至少一个j
        regexp ‘a{3}’;   #匹配字符串连续出现3次
        regexp ‘a{2,4}’;  #匹配字符串最少2次,最多4次
        
        9.实例1:
        #查看MYSQL数据库中所有用户
        SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
         
        #查看数据库中具体某个用户的权限
        show grants for 'cactiuser'@'%';  
        select * from mysql.user where user='cactiuser' \G;
         
        #查看数据库指定表 
        SELECT TABLE_NAME FROM information_schema.tables where table_name = 'm1801' 
                               and table_schema = 'futures_test'; 
         
        #查看指定数据库.数据表.列
           select COLUMN_NAME from  information_schema.COLUMNS where table_name = 'm1801'
                               and table_schema = 'futures_test';
        实例2:
        1.查询数据表是否存在
        set @str_tb_name= (SELECT TABLE_NAME FROM information_schema.tables 
                                  where table_name = 'm111' and table_schema = 'new_futures');#1条结果
        set @a= (select if(@str_tb_name='m111','ok1','ng1') result);
        
        2.查询数据列是否存在
        #select (select COLUMN_NAME from  information_schema.COLUMNS 
                     where table_name = 'm111'  and table_schema = 'new_futures'
                     and  column_name='No') into @b;#多条结果

        set @str_col_name= (select COLUMN_NAME from  information_schema.COLUMNS
                     where table_name = 'm111'  and table_schema = 'new_futures'
                     and  column_name='No') ;#1条结果

        set @b= (select if(@str_col_name='no','ok2','ng2') result) ;

        3.显示结果:
        select @a as a,@b as b,@str_tb_name as tb,@str_col_name as col;
        "
        a    b  tb   col
        ok1 ok2 m111  No
        "

        """
        result=None
        distinct=' DISTINCT ' if distinct else ''
        cols_names=' * ' if cols_names=='' else cols_names
        where=' WHERE '+where if where else ''
        having = ' HAVING ' + having if having else ''
        group_by=' GROUP BY  '+group_by if group_by else ''
        if limit:
            str1=','+str(limit[1]) if len(limit)>1 else ''
            limit=' LIMIT ' +str(limit[0])+str1
        else:
            limit=''
        if order_by_cols:
            order_by=" ORDER BY "+order_by_cols
            order_by=order_by+ ' DESC'  if order_by_desc else " ASC"
        else:
            order_by=''
        try:
            sql="SELECT "+distinct+ " %s FROM %s"%(cols_names,tb_names)
            sql = sql+where+group_by+having+limit+order_by
            print('sssss=',sql)
            result= self.__run(sql,back_lst=back_list)#[0]
        except Exception as  e:
            print('e=',e)
        finally:
            #return (result,-1) if runok else (result,result[1])
            return result[0]
    # =======================================================================

猜你喜欢

转载自blog.csdn.net/tcy23456/article/details/88091981