def explicitly_connection_pool(): print("\n== {}() ==".format(inspect.stack()[0][3])) connectioPool = ExplicitlyConnectionPool.get_instance() print("ConnectionPool {}".format(connectioPool)) connection = connectioPool.get_connection() print("Connection {}".format(connection)) connection.close()
def data_backup(self, table_name): filename = table_name + '.txt' try: conn = ExplicitlyConnectionPool.get_instance().get_connection() cursor = conn.cursor() source_path = self.source_dir + filename if os.path.exists(source_path): os.remove(source_path) backup_sql = "SELECT * FROM {} INTO OUTFILE '{}' {}".format( table_name, source_path, BackupRestore.OPTION) cursor.execute(backup_sql) """ if not os.path.exists(self.data_dir): os.makedirs(os.path.join('data')) shutil.move(source_path, self.data_dir + '/' + filename) # 파일이 존재하면 overwrite """ print(table_name, "backup complete!") except Error as err: print(err) print(table_name, "backup fail!") finally: if conn.is_connected(): cursor.close() conn.close()
def data_backup(self, table_name): filename = table_name + '.txt' try: conn = ExplicitlyConnectionPool.get_instance().get_connection() cursor = conn.cursor() source_path = self.source_dir + filename if os.path.exists(source_path): os.remove(source_path) cursor.execute("USE {}".format(self._db['database_name'])) backup_sql = "SELECT * FROM {} INTO OUTFILE '{}' {}".format( table_name, source_path, DBInitService.OPTION) cursor.execute(backup_sql) if not os.path.exists(self.data_dir): data_dir_path = os.path.join('data') print(data_dir_path) os.makedirs(data_dir_path) print("data_dir_path {} create data folder ".format( data_dir_path)) shutil.move(source_path, self.data_dir + '/' + filename) # 파일이 존재하면 overwrite print(table_name, "backup complete!") except Error as err: print(err) finally: if conn.is_connected(): cursor.close() conn.close()
def data_restore(self, table_name): filename = table_name + '.txt' try: conn = ExplicitlyConnectionPool.get_instance().get_connection() cursor = conn.cursor() cursor.execute("USE {}".format(self._db['database_name'])) data_path = os.path.abspath(self.data_dir + filename).replace( '\\', '/') if not os.path.exists(data_path): print("파일 '{}' 이 존재하지 않음".format(data_path)) return restore_sql = "LOAD DATA LOCAL INFILE '{}' INTO TABLE {} {}".format( data_path, table_name, DBInitService.OPTION) print("restore_sql \n {}".format(restore_sql)) res = cursor.execute(restore_sql) conn.commit() print(table_name, "restore complete!", " res = ", res) except Error as err: print(err) finally: if conn.is_connected(): cursor.close() conn.close()
def __create_user(self): try: conn = ExplicitlyConnectionPool.get_instance().get_connection() cursor = conn.cursor() print("Creating user: "******"OK") except Error as err: print(err) finally: cursor.close() conn.close()
def create_database(): try: conn = ExplicitlyConnectionPool.get_instance().get_connection() cursor = conn.cursor() cursor.execute( "CREATE DATABASE {} DEFAULT CHARACTER SET 'utf8'".format(DB_NAME)) print("CREATE DATABASE {}".format(DB_NAME)) except Error as err: if err.errno == errorcode.ER_DB_CREATE_EXISTS: cursor.execute("DROP DATABASE {} ".format(DB_NAME)) print("DROP DATABASE {}".format(DB_NAME)) cursor.execute( "CREATE DATABASE {} DEFAULT CHARACTER SET 'utf8'".format( DB_NAME)) print("CREATE DATABASE {}".format(DB_NAME)) else: print(err.msg) finally: if conn.is_connected(): cursor.close() conn.close()
def __create_table(self): try: conn = ExplicitlyConnectionPool.get_instance().get_connection() cursor = conn.cursor() cursor.execute("USE {}".format(self._db['database_name'])) for table_name, table_sql in self._db['sql'].items(): try: print("Creating table {}: ".format(table_name), end='') cursor.execute(table_sql) except Error as err: if err.errno == errorcode.ER_TABLE_EXISTS_ERROR: print("already exists.") else: print(err.msg) else: print("OK") except Error as err: print(err) finally: cursor.close() conn.close()
def data_restore(self, table_name): filename = table_name + '.txt' try: conn = ExplicitlyConnectionPool.get_instance().get_connection() cursor = conn.cursor() data_path = os.path.abspath(self.source_dir + filename).replace( '\\', '/') if not os.path.exists(data_path): print("file '{}' does not exist.".format(data_path)) return restore_sql = "LOAD DATA INFILE '{}' INTO TABLE {} {}".format( data_path, table_name, BackupRestore.OPTION) cursor.execute(restore_sql) conn.commit() print(table_name, "restore complete!") except Error as err: print(err) print(table_name, "restore fail!") finally: if conn.is_connected(): cursor.close() conn.close()
def __create_database(self): try: sql = read_ddl_file() conn = ExplicitlyConnectionPool.get_instance().get_connection() cursor = conn.cursor() cursor.execute( "CREATE DATABASE {} DEFAULT CHARACTER SET 'utf8'".format( self._db['database_name'])) print("CREATE DATABASE {}".format(self._db['database_name'])) except Error as err: if err.errno == errorcode.ER_DB_CREATE_EXISTS: cursor.execute("DROP DATABASE {} ".format( self._db['database_name'])) print("DROP DATABASE {}".format(self._db['database_name'])) cursor.execute( "CREATE DATABASE {} DEFAULT CHARACTER SET 'utf8'".format( self._db['database_name'])) print("CREATE DATABASE {}".format(self._db['database_name'])) else: print(err.msg) finally: cursor.close() conn.close()
def create_table(): try: conn = ExplicitlyConnectionPool.get_instance().get_connection() cursor = conn.cursor() cursor.execute("USE {}".format(DB_NAME)) for table_name in TABLES: table_description = TABLES[table_name] print('table_description', table_description) try: print("Creating table {}: ".format(table_name), end='') cursor.execute(table_description) except Error as err: if err.errno == errorcode.ER_TABLE_EXISTS_ERROR: print("already exists.") else: print(err.msg) else: print("OK") except Error as err: print(err) finally: if conn.is_connected(): cursor.close() conn.close()
def explicitly_connection_pool(): print("== {}() ==".format(inspect.stack()[0][3])) connectionPool = ExplicitlyConnectionPool.get_instance() connection = connectionPool.get_connection() print(type(connection), connection) connection.close()