Online application of list generation
It is recommended to read firstPython list generation formula and dictionary generation formula, let’s look at this article again.
Optimization method of select statement
In [1]: import MySQLdb as mysql In [2]: conn=(user='root',host='127.0.0.1',passwd='123456',db='lyz',charset='utf8') In [3]: (True) In [4]: cur = () In [5]: info = ['id','name','name_cn','email','mobile'] In [6]: sql = 'select %s from users'%','.join(info) In [7]: sql Out[7]: 'select id,name,name_cn,email,mobile from users' In [8]: (sql) Out[8]: 2L In [9]: res = () # fetchall() obtains a nested tuple In [10]: res Out[10]: ((1L, u'liuyongzhan', u'\u5218\u6c38\u6808', u'1665439369@', u'2147483647'), (6L, u'lyz', u'1111', u'1111', u'1111'))
Advanced usage:
Turn nested tuples into a list with nested dictionaries in the list
In [11]: ha = [dict((k,row[i]) for i,k in enumerate(info)) for row in res] In [12]: ha Out[12]: [{'email': u'1665439369@', 'id': 1L, 'mobile': u'2147483647', 'name': u'liuyongzhan', 'name_cn': u'\u5218\u6c38\u6808'}, {'email': u'1111', 'id': 6L, 'mobile': u'1111', 'name': u'lyz', 'name_cn': u'1111'}]
Optimization method of insert statement
High-level writing method:
data = dict() # Or the front-end can be directly passed in json format, and it can be written as data=request.get_json()ZiChan = ['hostname','cpu','mem','exdate','author','note'] sql='insert into zichan (%s) values (%s)'%(','.join(ZiChan),','.join(['"%s"'%data[x][0] for x in ZiChan]))
It's also possible:
data = {'name':'lyz','age':26,'mobile':15768216871,'email':'1665439369@'} key,values = [],[] for k,v in (): (k) ('"%s"'%v) sql = 'insert into users (%s) values (%s)'%(','.join(key),','.join(values)) Execution results: 'insert into users (mobile,age,name,email) values ("15768216871","26","lyz","1665439369@")'
How to optimize update statements
General writing:
ida = ('id') hostname = ('hostname') cpu = ('cpu') mem = ('mem') exdate = ('exdate') author = ('author') note = ('note') sql = 'update zichan set hostname ="%s",cpu ="%s",mem ="%s",exdate ="%s",author ="%s",note ="%s" where id=%s'%(hostname,cpu,mem,exdate,author,note,ida)
High-level writing method:
Method 1:
data = dict() ida = ('id',None)[0] ZiChan = ['hostname','cpu','mem','exdate','author','note'] arr = ["%s='%s'" %(k,data[k][0]) for k in ZiChan] sql = 'update zichan set %s where id=%s'%(','.join(arr),ida)
Method 2: The writing method of method 2 is the best, so if the front-end passes the field, the corresponding field will be updated. The fields will not be written to death.
Method 1 mainly means that the data transmitted from the front-end jquery is not optimized. If the front-end is transmitted in json format, and it contains an id key and a data key, the id will only correspond to one id, and the value of data is a dictionary. In the dictionary, each asset information field is used as the key, the following method can be used:
data = request.get_json() ida = ('id',None) data = ('data',None) arr = ["%s='%s'"%(k,data[k] for k in data)] # The result is: ["'hostname'='xxx'","'cpu'='xxx'",......]sql = 'update zichan set %s where id=%s'%(','.join(arr),ida)
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.