Python Mysql Select Dict

Source: Internet
Author: User
Tags python mysql

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.