Home Backend Development Python Tutorial Python encapsulates DBUtils and pymysql instances

Python encapsulates DBUtils and pymysql instances

Jul 23, 2017 pm 02:13 PM
python3 optimization

I wrote a basedao.py in the previous article about Python encapsulating DBUtils and pymysql. In recent days, I have reorganized my thoughts and optimized basedao.py. Currently, there are not many methods supported, and improvements and additions will be made in the future. .

Main functions:

1. Query a single object:

Required parameters: table name, filter conditions

2. Query multiple objects:
Required parameters: table name, filter condition

3. Query by primary key:
Required parameters: table name, value

4. Paging query:
Required Parameters: table name, page number, number of records per page, filter conditions

The specific code is as follows:

  1 import json, os, sys, time  2   3 import pymysql  4 from DBUtils import PooledDB  5   6 class BaseDao(object):  7     """  8     简便的数据库操作基类  9     """ 10     __config = {}                   # 数据库连接配置 11     __conn = None                   # 数据库连接 12     __cursor = None                 # 数据库游标 13     __database = None               # 用于临时村塾查询数据库 14     __tableName = None              # 用于临时存储查询表名 15     __fields = []                   # 用于临时存储查询表的字段列表 16     __primaryKey_dict = {}          # 用于存储配置中的数据库中所有表的主键 17  18     def __init__(self, creator=pymysql, host="localhost", user=None, password="", database=None, port=3306, charset="utf8"): 19         if host is None: 20             raise Exception("Parameter [host] is None.") 21         if user is None: 22             raise Exception("Parameter [user] is None.") 23         if password is None: 24             raise Exception("Parameter [password] is None.") 25         if database is None: 26             raise Exception("Parameter [database] is None.") 27         if port is None: 28             raise Exception("Parameter [port] is None.") 29         self.__config = dict({ 30             "creator" : creator, "charset":charset, 31             "host":host, "port":port, 
 32             "user":user, "password":password, "database":database 33         }) 34         self.__conn = PooledDB.connect(**self.__config) 35         self.__cursor = self.__conn.cursor() 36         self.__database = self.__config["database"] 37         self.__init_primaryKey() 38         print(get_time(), "数据库连接初始化成功。") 39          40     def __del__(self): 41         '重写类被清除时调用的方法' 42         if self.__cursor: 43             self.__cursor.close() 44             print(get_time(), "游标关闭") 45         if self.__conn: 46             self.__conn.close() 47             print(get_time(), "连接关闭") 48  49     def select_one(self, tableName=None, filters={}): 50         ''' 51         查询单个对象 52         @tableName 表名 53         @filters 过滤条件 54         @return 返回字典集合,集合中以表字段作为 key,字段值作为 value 55         ''' 56         self.__check_params(tableName) 57         sql = self.__query_util(filters) 58         self.__cursor.execute(sql) 59         result = self.__cursor.fetchone() 60         return self.__parse_result(result) 61  62     def select_pk(self, tableName=None, primaryKey=None): 63         ''' 64         按主键查询 65         @tableName 表名 66         @primaryKey 主键值 67         ''' 68         self.__check_params(tableName) 69         filters = {} 70         filters.setdefault(str(self.__primaryKey_dict[tableName]), primaryKey) 71         sql = self.__query_util(filters) 72         self.__cursor.execute(sql) 73         result = self.__cursor.fetchone() 74         return self.__parse_result(result) 75          76     def select_all(self, tableName=None, filters={}): 77         ''' 78         查询所有 79         @tableName 表名 80         @filters 过滤条件 81         @return 返回字典集合,集合中以表字段作为 key,字段值作为 value 82         ''' 83         self.__check_params(tableName) 84         sql = self.__query_util(filters) 85         self.__cursor.execute(sql) 86         results = self.__cursor.fetchall() 87         return self.__parse_results(results) 88  89     def count(self, tableName=None): 90         ''' 91         统计记录数 92         ''' 93         self.__check_params(tableName) 94         sql = "SELECT count(*) FROM %s"%(self.__tableName) 95         self.__cursor.execute(sql) 96         result = self.__cursor.fetchone() 97         return result[0] 98  99     def select_page(self, tableName=None, pageNum=1, limit=10, filters={}):100         '''101         分页查询102         @tableName 表名103         @return 返回字典集合,集合中以表字段作为 key,字段值作为 value104         '''105         self.__check_params(tableName)106         totalCount = self.count()107         if totalCount / limit == 0 :108             totalPage = totalCount / limit109         else:110             totalPage = totalCount // limit + 1111         if pageNum > totalPage:112             print("最大页数为%d"%totalPage)113             pageNum = totalPage114         elif pageNum < 1:115             print("页数不能小于1")116             pageNum = 1117         beginindex = (pageNum-1) * limit118         filters.setdefault("_limit_", (beginindex, limit))119         sql = self.__query_util(filters)120         self.__cursor.execute(sql)121         results = self.__cursor.fetchall()122         return self.__parse_results(results)123 124     def __parse_result(self, result):125         &#39;用于解析单个查询结果,返回字典对象&#39;126         obj = {}127         for k,v in zip(self.__fields, result):128             obj[k] = v129         return obj130 131     def __parse_results(self, results):132         &#39;用于解析多个查询结果,返回字典列表对象&#39;133         objs = []134         for result in results:135             obj = self.__parse_result(result)136             objs.append(obj)137         return objs138 139     def __init_primaryKey(self):140         &#39;根据配置中的数据库读取该数据库中所有表的主键集合&#39;141         sql = """SELECT TABLE_NAME, COLUMN_NAME142                 FROM  Information_schema.columns143                 WHERE COLUMN_KEY=&#39;PRI&#39; AND TABLE_SCHEMA=&#39;%s&#39;"""%(self.__database)144         self.__cursor.execute(sql)145         results = self.__cursor.fetchall()146         for result in results:147             self.__primaryKey_dict[result[0]] = result[1]148 149     def __query_fields(self, tableName=None, database=None):150         &#39;查询表的字段列表, 将查询出来的字段列表存入 __fields 中&#39;151         sql = """SELECT column_name152                 FROM  Information_schema.columns153                 WHERE table_Name = &#39;%s&#39; AND TABLE_SCHEMA=&#39;%s&#39;"""%(tableName, database)154         self.__cursor.execute(sql)155         fields_tuple = self.__cursor.fetchall()156         self.__fields = [fields[0] for fields in fields_tuple]157 158     def __query_util(self, filters=None):159         """160         SQL 语句拼接方法161         @filters 过滤条件162         """163         sql = r&#39;SELECT #{FIELDS} FROM #{TABLE_NAME} WHERE 1=1 #{FILTERS}&#39;164         # 拼接查询表165         sql = sql.replace("#{TABLE_NAME}", self.__tableName)166         # 拼接查询字段167         self.__query_fields(self.__tableName, self.__database)168         FIELDS = ""169         for field in self.__fields:170             FIELDS += field + ", "171         FIELDS = FIELDS[0: len(FIELDS)-2]172         sql = sql.replace("#{FIELDS}", FIELDS)173         # 拼接查询条件(待优化)174         if filters is None:175             sql = sql.replace("#{FILTERS}", "")176         else:177             FILTERS =  ""178             if not isinstance(filters, dict):179                 raise Exception("Parameter [filters] must be dict type. ")180             isPage = False181             if filters.get("_limit_"):182                 isPage = True183                 beginindex, limit = filters.get("_limit_")184             for k, v in filters.items():185                 if k.startswith("_in_"):                # 拼接 in186                     FILTERS += "AND %s IN (" %(k[4:])187                     values = v.split(",")188                     for value in values:189                         FILTERS += "%s,"%value190                     FILTERS = FILTERS[0:len(FILTERS)-1] + ") "191                 elif k.startswith("_nein_"):            # 拼接 not in192                     FILTERS += "AND %s NOT IN (" %(k[4:])193                     values = v.split(",")194                     for value in values:195                         FILTERS += "%s,"%value196                     FILTERS = FILTERS[0:len(FILTERS)-1] + ") "197                 elif k.startswith("_like_"):            # 拼接 like198                     FILTERS += "AND %s like &#39;%%%s%%&#39; " %(k[6:], v)199                 elif k.startswith("_ne_"):              # 拼接不等于200                     FILTERS += "AND %s != &#39;%s&#39; " %(k[4:], v)201                 elif k.startswith("_lt_"):              # 拼接小于202                     FILTERS += "AND %s < &#39;%s&#39; " %(k[4:], v)203                 elif k.startswith("_le_"):              # 拼接小于等于204                     FILTERS += "AND %s <= &#39;%s&#39; " %(k[4:], v)205                 elif k.startswith("_gt_"):              # 拼接大于206                     FILTERS += "AND %s > '%s' " %(k[4:], v)207                 elif k.startswith("_ge_"):              # 拼接大于等于208                     FILTERS += "AND %s >= '%s' " %(k[4:], v)209                 elif k in self.__fields:                # 拼接等于210                     FILTERS += "AND %s = '%s' "%(k, v)211             sql = sql.replace("#{FILTERS}", FILTERS)212             if isPage:213                 sql += "LIMIT %d,%d"%(beginindex, limit)214 215         print(get_time(), sql)216         return sql217 218     def __check_params(self, tableName):219         '''220         检查参数221         '''222         if tableName:223             self.__tableName = tableName224         else:225             if self.__tableName is None:226                 raise Exception("Parameter [tableName] is None.")227 228 def get_time():229     return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())230 231 if __name__ == "__main__":232     config = {233         # "creator": pymysql,234         # "host" : "127.0.0.1", 235         "user" : "root", 
236         "password" : "root",237         "database" : "test", 
238         # "port" : 3306,239         # "charset" : 'utf8'240     }241     base = BaseDao(**config)242     ########################################################################243     # user = base.select_one("user")244     # print(user)245     ########################################################################246     # users = base.select_all("user")247     # print(users)248     ########################################################################249     # filter1 = {250     #     "sex":0,251     #     "_in_id":"1,2,3,4,5",252     #     "_like_name":"zhang",253     #     "_ne_name":"wangwu"254     # }255     # user_filters = base.select_all(tableName="user", filters=filter1)256     # print(user_filters)257     ########################################################################258     # menu = base.select_one(tableName="menu")259     # print(menu)260     ########################################################################261     # user_pk = base.select_pk("user", 2)262     # print(user_pk)263     ########################################################################264     # filter2 = {265     #     "_in_id":"1,2,3,4",266     #     "_like_name":"test"267     # }268     # user_limit = base.select_page("user", 2, 10, filter2)  #未实现269     # print(user_limit)270     ########################################################################
Copy after login
View Code

  Several specific examples have been given in the code for your reference.

The above is the detailed content of Python encapsulates DBUtils and pymysql instances. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to optimize settings and improve performance after receiving a new Win11 computer? How to optimize settings and improve performance after receiving a new Win11 computer? Mar 03, 2024 pm 09:01 PM

How do we set up and optimize performance after receiving a new computer? Users can directly open Privacy and Security, and then click General (Advertising ID, Local Content, Application Launch, Setting Recommendations, Productivity Tools or directly open Local Group Policy Just use the editor to operate it. Let me introduce to you in detail how to optimize settings and improve performance after receiving a new Win11 computer. How to optimize settings and improve performance after receiving a new Win11 computer. One: 1. Press the [Win+i] key combination to open Settings, then click [Privacy and Security] on the left, and click [General (Advertising ID, Local Content, App Launch, Setting Suggestions, Productivity) under Windows Permissions on the right Tools)】.Method 2

In-depth interpretation: Why is Laravel as slow as a snail? In-depth interpretation: Why is Laravel as slow as a snail? Mar 07, 2024 am 09:54 AM

Laravel is a popular PHP development framework, but it is sometimes criticized for being as slow as a snail. What exactly causes Laravel's unsatisfactory speed? This article will provide an in-depth explanation of the reasons why Laravel is as slow as a snail from multiple aspects, and combine it with specific code examples to help readers gain a deeper understanding of this problem. 1. ORM query performance issues In Laravel, ORM (Object Relational Mapping) is a very powerful feature that allows

Decoding Laravel performance bottlenecks: Optimization techniques fully revealed! Decoding Laravel performance bottlenecks: Optimization techniques fully revealed! Mar 06, 2024 pm 02:33 PM

Decoding Laravel performance bottlenecks: Optimization techniques fully revealed! Laravel, as a popular PHP framework, provides developers with rich functions and a convenient development experience. However, as the size of the project increases and the number of visits increases, we may face the challenge of performance bottlenecks. This article will delve into Laravel performance optimization techniques to help developers discover and solve potential performance problems. 1. Database query optimization using Eloquent delayed loading When using Eloquent to query the database, avoid

C++ program optimization: time complexity reduction techniques C++ program optimization: time complexity reduction techniques Jun 01, 2024 am 11:19 AM

Time complexity measures the execution time of an algorithm relative to the size of the input. Tips for reducing the time complexity of C++ programs include: choosing appropriate containers (such as vector, list) to optimize data storage and management. Utilize efficient algorithms such as quick sort to reduce computation time. Eliminate multiple operations to reduce double counting. Use conditional branches to avoid unnecessary calculations. Optimize linear search by using faster algorithms such as binary search.

Discussion on Golang's gc optimization strategy Discussion on Golang's gc optimization strategy Mar 06, 2024 pm 02:39 PM

Golang's garbage collection (GC) has always been a hot topic among developers. As a fast programming language, Golang's built-in garbage collector can manage memory very well, but as the size of the program increases, some performance problems sometimes occur. This article will explore Golang’s GC optimization strategies and provide some specific code examples. Garbage collection in Golang Golang's garbage collector is based on concurrent mark-sweep (concurrentmark-s

Laravel performance bottleneck revealed: optimization solution revealed! Laravel performance bottleneck revealed: optimization solution revealed! Mar 07, 2024 pm 01:30 PM

Laravel performance bottleneck revealed: optimization solution revealed! With the development of Internet technology, the performance optimization of websites and applications has become increasingly important. As a popular PHP framework, Laravel may face performance bottlenecks during the development process. This article will explore the performance problems that Laravel applications may encounter, and provide some optimization solutions and specific code examples so that developers can better solve these problems. 1. Database query optimization Database query is one of the common performance bottlenecks in Web applications. exist

How to optimize the startup items of WIN7 system How to optimize the startup items of WIN7 system Mar 26, 2024 pm 06:20 PM

1. Press the key combination (win key + R) on the desktop to open the run window, then enter [regedit] and press Enter to confirm. 2. After opening the Registry Editor, we click to expand [HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionExplorer], and then see if there is a Serialize item in the directory. If not, we can right-click Explorer, create a new item, and name it Serialize. 3. Then click Serialize, then right-click the blank space in the right pane, create a new DWORD (32) bit value, and name it Star

What are some ways to resolve inefficiencies in PHP functions? What are some ways to resolve inefficiencies in PHP functions? May 02, 2024 pm 01:48 PM

Five ways to optimize PHP function efficiency: avoid unnecessary copying of variables. Use references to avoid variable copying. Avoid repeated function calls. Inline simple functions. Optimizing loops using arrays.

See all articles