How to call the __main__ function directly to see how to call this utility class.

5 from conf. Settings import * 6 7 8 class MysqlConnection(object): 9 """ 12 13 def __init__(self, config): 14 # Connect to the database 15 self.connection = pymysql.connect(**config) 16 self.connection.autocommit(True) 17 Self. Cursor = self.connection.cursor() 18 19 def QueryAll(self, SQL): 20 "" 22 :param SQL: 23 :return: 26 self.reconnect () 27 28 self.cursor.execute(SQL) 29 return self.cursor.fetchall() 30 31 def QueryMany(self, SQL, n): 32 "" 32 :param SQL: 35 :return: 38 self.reconnect () 39 40 self.cursor.execute(SQL) 41 return self.cursor.fetchmany(n) 42 43 def QueryOne(self, SQL): 44 "" 45 Query some data 46 :param SQL: 47 :return: SQL > select * from self.cursor.execute(SQL) 53 return self.cursor.fetchone() 54 55 # return Self.cursor. Fetchone () 56 57 def reConnect(self): 58 "" 63 self.connection.ping() 64 except: 65 self.connection() 66 67 def Operate(self, sql, params=None, DML=True): 68 ", "" 69 database operations: add and delete 70 DML: insert/update/delete 71 DDL: CREATE TABLE/VIEW/INDEX/SYN/CLUSTER 72", "" 73 try: 76 76 with self.connection.cursor() as cursor: 78 cursor.execute(sql, params) 79 80 self.connection.commit() 81 82 except Exception as e: 83 if DML: 85 self.connection.rollback() 86 print(e) 87 88 def __del__(self): 89 """ 90 MysqlConnection instance object is released when this method is used to close cursor and connection 91 """ 92 self.cursor.close() 93 self.connection.close() 94 95 96 if __name__ == "__main__": 97 MYSQL_HOST, 'charset': CHARSET, 'db': DB, 'user': USER, 'port': MYSQL_PORT, 'password': PASSWORD} 99 msc = MysqlConnection(config) 100 sql = "delete from users where username ='%s'" % "123456" 101 102 print(msc.Operate(sql))Copy the code