#!/usr/bin/env python # -*- coding: utf-8 -*- # Software: PyCharm # __author__ == "HAI" # FileName: pymysql自己定义方法.py # Date : 2018/10/8 # Time : 19:01 """pymysql""" import pymysql def decrotor(fn, *args): """ :param fn: :param args: :return: """ def wapper(self, *args, **kwargs): """ :param self: :param args: :param kwargs: :return: """ self._get_couser() n = fn(self, *args, **kwargs) self._close() return n return wapper class MysqlConnect(object): """ 我的sql类 """ def __init__(self, db, host='127.0.0.1', password='123456', port=3306, user='root', *args, **kwargs): """ :param db: :param host: :param password: :param port: :param user: :param args: :param kwargs: """ self.conn = pymysql.connect(host=host, db=db, password=password, port=port, user=user, *args, **kwargs) @decrotor def table_data_count(self, table_name): """ 返回数据总个数 :param table_name: :return: """ query = 'select count(1) as totall from %s ;' % table_name self.cur.execute(query) return self.cur.fetchone() @decrotor def get_list(self, query,offset=0, maxnum=1000, *args): try: offset = int(offset) maxnum = int(maxnum) except ValueError: pass else: query = query.replace(';', '') + '\tlimit {maxnum} offset {offset}'.format(maxnum=maxnum, offset=offset) self.cur.execute(query, *args) result = self.cur.fetchall() return result @decrotor def get_one(self, query, *args): """ Execute a query :param str query: Query to execute. :param args: parameters used with query. (optional) :type args: tuple, list or dict """ query = query.replace(';', '') + ' limit 1 offset 0;' self.cur.execute(query, *args) return self.cur.fetchone() @decrotor def multiple_modify(self, query, *args): # type: (str, list) -> int """ 多条插入语句 self.cursor.executemany('insert into bd(id,name)values(%s,%s)',[(1,'alex'),(2,'eric')]) Run several data against one query :param query: query to execute on server :param args: Sequence of sequences or mappings. It is used as parameter. :return: Number of rows affected, if any. This method improves performance on multiple-row INSERT and REPLACE. Otherwise it is equivalent to looping over args with execute(). """ self.cur.executemany(query, *args) self.conn.commit() @decrotor def modify(self, query, *args): """ 'insert into test(tests, coumt) VALUES (%s, %s);', ['hello I want to go to bed', 2] :param query: :param args: :return: None """ self.cur.execute(query, *args) self.conn.commit() @decrotor def create(self, query, *args): """ 'insert into test(tests, coumt) VALUES (%s, %s);', ['hello I want to go to bed', 2] :param query: :param args: :return: last auto_increment ID """ self.cur.execute(query, *args) self.conn.commit() return self.cur.lastrowid def _get_couser(self, flag=True,): """ :param flag: True 以字典返回 """ self.cur = self.conn.cursor(pymysql.cursors.DictCursor) if flag else self.conn.cursor() def _close(self): """ 关闭游标 """ self.cur.close() def __del__(self): try: self.conn.close() except pymysql.err.Error: pass if __name__ == '__main__': my = MysqlConnect('blogs') print(my.table_data_count('test'))
我的MYsq方法
最新推荐文章于 2024-01-30 22:55:53 发布