mkeneqa / Pymysquery

Python database wrapper class for the pymysql module
MIT License
2 stars 1 forks source link

Add Dictionary Cursor Option #5

Open mkeneqa opened 3 years ago

mkeneqa commented 3 years ago

Have returned rows be in dictionary form.

http://zetcode.com/python/pymysql/

eg:

#!/usr/bin/python

import pymysql
import pymysql.cursors

con = pymysql.connect(host='localhost',
        user='user7',
        password='s$cret',
        db='testdb',
        charset='utf8mb4',
        cursorclass=pymysql.cursors.DictCursor)

try:

    with con.cursor() as cur:

        cur.execute('SELECT * FROM cities')

        rows = cur.fetchall()

        for row in rows:
            print(row['id'], row['name'])

finally:

    con.close()
mkeneqa commented 3 years ago
    def Connect(self, use_cursor_dict=False):
        if use_cursor_dict:
            self.USE_CRSR_DICT = True
            return pymysql.connect(
                host=self.HOST,
                user=self.DBUSR,
                password=self.DBPSWD,
                db=self.DBNAME,
                charset='utf8mb4',
                cursorclass=pymysql.cursors.DictCursor
            )
        else:
            self.USE_CRSR_DICT = False
            return pymysql.connect(
                host=self.HOST,
                user=self.DBUSR,
                password=self.DBPSWD,
                db=self.DBNAME,
                charset='utf8mb4'
            )