AP257
AP257

Reputation: 701

Python: use mysqldb to import a MySQL table as a dictionary?

Anyone know how I can use mysqldb to turn a MySQL table, with lots of rows, into a list of dictionary objects in Python?

I mean turning a set of MySQL rows, with columns 'a', 'b', and 'c', into a Python object that that looks like this:

data = [ { 'a':'A', 'b':(2, 4), 'c':3.0 }, { 'a':'Q', 'b':(1, 4), 'c':5.0 }, { 'a':'T', 'b':(2, 8), 'c':6.1 } ]

Thanks :)

Upvotes: 36

Views: 33521

Answers (5)

DaWe
DaWe

Reputation: 1702

I think with mysql.connector is much more easier to convert a select to a dict than MySQLdb, and also more Python version supported:

cursor = conn.cursor(dictionary=True)

Detailed example:

import mysql.connector # pip install mysql-connector-python

conn = mysql.connector.connect(host="localhost", user="user", passwd="pass", database="dbname")
cursor = conn.cursor(dictionary=True)
sql = "SELECT * FROM `table` WHERE 1"
cursor.execute(sql)
rows = cursor.fetchall()
for row in rows:
    row["col"]

Upvotes: 7

Renan Moura
Renan Moura

Reputation: 55

using PYTHON >= 3.6; here it works by doing this:

OBS.: here I'm using 2 databases types ORACLE 12g and MYSQL 5.6 ; and the main server is the ORACLE, mysql is used only by some parts of the software ... then ... the code of my connection class:

here is comming from shellscript code... then i just bring a string to show here...

myp3 = 'XXXXXXXXXXXXXXXXXXXXXXXXXX PASSWD MYSQL XXXXXXXXXXXXXXXXXXXXXXXXXXX'

import cx_Oracle
import MySQLdb
import MySQLdb.cursors

class oracle(object):
    def __init__(self, serverORACLE=3, MYSQL='N', serverMYSQL=3):
        ## ORACLE connection!
        try:

        if serverORACLE == 1:
            self.db = cx_Oracle.connect('sys/[xxxxPASSWORDxxxxx]@[xxxxxIP/HOSTxxxxxx]:1521/DATABASE_X')
            print('ORACLE [ system/[xxxxPASSWORDxxxxx]@[xxxxxIP/HOSTxxxxxx]:1521 ] ===> CONNECTED')

        if serverORACLE == 2:
            self.db = cx_Oracle.connect('system/[xxxxPASSWORDxxxxx]@[xxxxxIP/HOSTxxxxxx]:1521/DATABASE_X')
            print('ORACLE [ system/[xxxxPASSWORDxxxxx]@[xxxxxIP/HOSTxxxxxx]:1521 ] ===> CONNECTED')

        if serverORACLE == 3:
            self.db = cx_Oracle.connect('userLEVEL1/[xxxxPASSWORDxxxxx]@[xxxxxIP/HOSTxxxxxx]:1521/DATABASE_X')
            print('ORACLE [ userLEVEL1/[xxxxPASSWORDxxxxx]@[xxxxxIP/HOSTxxxxxx]:1521 ] ===> CONNECTED')

        if serverORACLE == 4:
            self.db = cx_Oracle.connect('userLEVEL2/[xxxxPASSWORDxxxxx]@[xxxxxIP/HOSTxxxxxx]:1521/DATABASE_X')
            print('ORACLE [ userLEVEL2/[xxxxPASSWORDxxxxx]@[xxxxxIP/HOSTxxxxxx]:1521 ] ===> CONNECTED')
            
        self.cursor = self.db.cursor()
        
    except Exception as e:
        count = 0
        S1 = ''
        for W in str(e):
            S1+=W
            count+=1 
            if count >= 40:
                S1+=' \n'
                count = 0
        print('\n\n ORACLE DATABASE ===> CONECTION FAILED!', 
                    '\n error - module: ', S1)

    ##conexao MYSQL
    if MYSQL=='S': 
        try:
            if serverMYSQL == 1:
                self.dbMySQL = MySQLdb.connect(user='root', host='XXXXXX HOST XXXXX', use_unicode=True, cursorclass=MySQLdb.cursors.DictCursor,
                                                charset='utf8', port=3306, passwd=myp3, db='XXXX')
                print('MySQL [ root / XXXXXX HOST XXXXX:3306 ] ===> CONNECTED')

            if serverMYSQL == 2:
                self.dbMySQL = MySQLdb.connect(user='XXXX USER XXXXX', host='XXXXXX HOST XXXXX', use_unicode=True, cursorclass=MySQLdb.cursors.DictCursor,
                                                charset='utf8', port=3306, passwd=myp3, db='XXXX')
                print('MySQL [ XXXX USER XXXXX / XXXXXX HOST XXXXX:3306 ] ===> CONNECTED')

            self.cursorMYSQL = self.dbMySQL.cursor()
        
        except Exception as e:
            count = 0
            S1 = ''
            for W in str(e):
                S1+=W
                count+=1 
                if count >= 40:
                    S1+=' \n'
                    count = 0
            print('\n\n MYSQL DATABASE ===> CONECTION FAILED!', 
                    '\n error - module: ', S1)

"""

Upvotes: 0

elPastor
elPastor

Reputation: 8966

This is an ancient post, and while this answer isn't exactly what the OP was looking for, it is much along the same lines. Instead of generating a list of dictionaries, it generates a dictionary of lists:

Also thought I'd provide the full code for generating the dictionary:

import MySQLdb
import MySQLdb.cursors

dict_serv = MySQLdb.connect(host = 'localhost', user = 'root', passwd = 'mypassword', cursorclass = MySQLdb.cursors.DictCursor)

with dict_serv as c:
    c.execute("USE mydb")
    c.execute("SELECT col1, col2 FROM mytable WHERE id = 'X123'")

    # Save the dictionary to a separate variable
    init_dict = c.fetchall()

    # This line builds the column headers
    sql_cols = [ col[0] for col in c.description ]

    # This is a list comprehension within a dictionary comprehension
    # which builds the full dictionary in a single line.  
    sql_dict = { k : [ d[k] for d in init_dict ] for k in sql_cols }

Which yields:

{ 'col1': ['apple','banana'], 'col2':['red','yellow'] }

Upvotes: 0

FelixEnescu
FelixEnescu

Reputation: 5102

If you need to use more cursors and only one needs to be MySQLdb.cursors.DictCursor you can do:

import MySQLdb
db = MySQLdb.connect(host='...', db='...', user='...t', passwd='...')

list_cursor = db.cursor()
dict_cursor = db.cursor(MySQLdb.cursors.DictCursor)

Upvotes: 24

Thomas Wouters
Thomas Wouters

Reputation: 133425

MySQLdb has a separate cursor class for this, the DictCursor. You can pass the cursor class you want to use to MySQLdb.connect():

import MySQLdb.cursors
MySQLdb.connect(host='...', cursorclass=MySQLdb.cursors.DictCursor)

Upvotes: 76

Related Questions