Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dictionary Cursor Option #5

Open
mkeneqa opened this issue Dec 4, 2020 · 1 comment
Open

Add Dictionary Cursor Option #5

mkeneqa opened this issue Dec 4, 2020 · 1 comment
Labels
enhancement New feature or request

Comments

@mkeneqa
Copy link
Owner

mkeneqa commented Dec 4, 2020

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 mkeneqa added the enhancement New feature or request label Dec 4, 2020
@mkeneqa
Copy link
Owner Author

mkeneqa commented Dec 8, 2020

    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'
            )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant