assert table.startswith('CREATE TABLE') table_name = _get_table_name(table) if table_name.endswith('01'): replace_table = table.replace(table_name, '%s') for i in range(100): sharding_table_name = '%s_%02d' % (table_name[:-3], i) try: cursor.execute(replace_table % sharding_table_name) print '%s create success' % sharding_table_name except Exception as e: if e.args[0] != 1050: raise e print '%s exsit' % sharding_table_name elif table_name.endswith('1'): replace_table = table.replace(table_name, '%s') for i in range(10): sharding_table_name = '%s_%s' % (table_name[:-2], i) try: cursor.execute(replace_table % sharding_table_name) print '%s create success' % sharding_table_name except Exception as e: if e.args[0] != 1050: raise e print '%s exsit' % sharding_table_name else: try: cursor.execute(table) print '%s create success' % table_name except Exception as e: if e.args[0] != 1050: raise e print '%s exsit' % table_name db.close()
def gets_all_tables(): with open(os.path.dirname(os.path.realpath(__file__)) + '/fuxi.sql', 'r') as f: content = f.read() tables = content.split('\n\n') for table in tables: create_table(table)