Python Select Mysql Date conversion string, converted to dictionary
Python read the data format is not normal JSON, read out, directly is the dictionary
Python 2.7
ImportMySQLdb fromMySQLdbImportConverters as Covconv=cov.conversions.copy () conv[246] = float#convert decimals to floatsCONV[10] = str#convert dates to StringsCONV[11] = str#convert Timedelta_or_none to StringsCONV[12] = str#convert Datetime_or_none to Strings#conv[15] = str # convert Datetime_or_none to StringsCONV[7] = str#convert Datetime_or_none to Strings#conv[8] = str # convert Datetime_or_none to StringsConn=MySQLdb.connect (Host='Dbhost', Port=3306, the user='Root', passwd='', DB='MySQL', CharSet='UTF8', Conv=conv) cur= Conn.cursor (Cursorclass = mysqldb.cursors. Dictcursor) Cur.execute ("SELECT * from Proxies_priv") Test_data=Cur.fetchall ()Printtest_datacur.close () conn.close ( )
Python 3.+ Tornado
#!/usr/bin/env python from __future__ Importprint_functionImportPymysql fromTornadoImportIoloop, Gen fromTornado_mysqlImportPools fromPymysqlImportConverters as CoV#From mysqldb import converters as CoVFrom tornado_mysql.cursors import dictcursorpools. DEBUG=TrueCONV =cov.conversions.copy () conv[246] = float#convert decimals to floatsCONV[10] = str#convert dates to StringsCONV[11] = str#convert Timedelta_or_none to StringsCONV[12] = str#convert Datetime_or_none to Strings#conv[15] = str # convert Datetime_or_none to StringsCONV[7] = str#convert Datetime_or_none to StringsPOOL=Pools. Pool (Dict (host='Dbhost', port=3306, user='Root', passwd='', db='MySQL', CharSet='UTF8', Conv=conv,cursorclass= dictcursor), Max_idle_connections=1, Max_recycle_sec=3) @gen. Coroutinedefworker (n): cur=yieldPool.execute ("SELECT * from Proxies_priv limit") Print(Cur.fetchall ()) @gen. CoroutinedefMain (): Workers= [Worker (i) forIinchRange (1)] yieldWorkersioloop. Ioloop.current (). Run_sync (Main)Print(Pool._opened_conns)
Output results
[{'User':'Root','Host':'localhost','Proxied_host':"','with_grant': 1,'Proxied_user':"','Timestamp':'2017-05-04 09:07:49','Grantor':"'}, {'User':'Root','Host':'pe-stats.wondershare.cn','Proxied_host':"','with_grant': 1,'Proxied_user':"','Timestamp':'2017-05-04 09:07:49','Grantor':"'}]
Python Mysql Select Dict