1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
| class DBUtil: __pool = None def __init__(self, host, user, password, database, port=3306, charset="utf8"): self.host = host self.port = int(port) self.user = user self.password = str(password) self.database = database self.charset = charset self.conn = self.__get_connection() self.cursor = self.conn.cursor()
def __get_connection(self): if DBUtil.__pool is None: __pool = PooledDB( creator=pymysql, mincached=1, maxcached=20, host=self.host, port=self.port, user=self.user, passwd=self.password, db=self.database, use_unicode=False, charset=self.charset, ) return __pool.connection()
def begin(self): """ 开启事务 """ self.conn.autocommit(0)
def end(self, option="commit"): """ 结束事务 """ if option == "commit": self.conn.commit() else: self.conn.rollback()
def connect(self): """ 直接连接数据库 :return conn: pymysql连接 """ try: conn = pymysql.connect( host=self.host, user=self.user, password=self.password, database=self.database, port=self.port, use_unicode=True, charset=self.charset, cursor=DictCursor, ) return conn except Exception as e: logging.error("Error connecting to mysql server.") raise
def close(self): try: self.cursor.close() self.conn.close() except Exception as e: logging.error("Error closing connection to mysql server")
def get_one(self, sql): try: self.cursor.execute(sql) res = self.cursor.fetchone() self.close() return res except Exception as e: raise
def get_all(self, sql): try: self.cursor.execute(sql) res = self.cursor.fetchall() self.close() return res except Exception as e: raise
def get_all_obj(self, sql, table_name, *args): resList = [] fields_list = [] try: if len(args) > 0: for item in args: fields_list.append(item) else: fields_sql = ( "select COLUMN_NAME from information_schema.COLUMNS where table_name = '%s' and table_schema = '%s'" % (table_name, self.conn_name) ) fields = self.get_all(fields_sql) for item in fields: fields_list.append(item[0])
res = self.get_all(sql) for item in res: obj = {} count = 0 for x in item: obj[fields_list[count]] = x count += 1 resList.append(obj) return resList except Exception as e: raise
def insert(self, sql, params=None): """ 插入操作 :return count: 影响的行数 """ return self.__edit(sql, params)
def update(self, sql, params=None): """ 更新操作 :return count: 影响的行数 """ return self.__edit(sql, params)
def delete(self, sql, params=None): """ 删除操作 :return count: 影响的行数 """ return self.__edit(sql, params)
def __edit(self, sql, params=None): max_attempts = 3 attempt = 0 count = 0 while attempt < max_attempts: try: self.conn = self.__get_connection() self.cursor = self.conn.cursor() if params is None: count = self.cursor.execute(sql) else: count = self.cursor.execute(sql, params) self.conn.commit() self.close() except Exception as e: logging.error(e) self.conn.rollback() count = 0 return count
def execute(self, sql, params=None): max_attempts = 3 attempt = 0 while attempt < max_attempts: try: self.conn = self.__get_connection() self.cursor = self.conn.cursor() if params is None: result = self.cursor.execute(sql) else: result = self.cursor.execute(sql, params) self.conn.commit() self.close() return result except Exception as e: attempt += 1 logging.warning(f"retry: {attempt}") logging.exception(e)
def truncate(self, table): sql = f"truncate table {table}" self.execute(sql)
def executemany(self, sql, data): max_attempts = 3 attempt = 0 while attempt < max_attempts: try: self.conn = self.__get_connection() self.cursor = self.conn.cursor() result = self.cursor.executemany(sql, data) self.conn.commit() self.close() return result except Exception as e: attempt += 1 logging.warning(f"retry: {attempt}") logging.exception(e)
config_filepath = os.path.dirname(os.path.dirname(__file__)) + "/config/dev.ini" config = Config(config_filepath).get_content("MYSQL") conn = DBUtil( host=config["HOST"], port=int(config["PORT"]), database=config["DATABASE"], user=config["USER"], password=config["PASSWORD"], charset=config["CHARSET"], )
def get_connection(): return conn
if __name__ == "__main__": db = get_connection() cursor = db.cursor
cursor.execute("SELECT VERSION()")
data = cursor.fetchone() print("Database version : %s " % data)
db.close()
|