Search code examples
pythonpymysql

pymysql - name of default cursorclass


This is probably a stupid question but I cannot find the information in the documentation for pymysql. What is the pymysql default cursorclass? When I do not specify a cursor class on connection to the database my queries return a list for each row in the response.

When I specify pymysql.cursors.DictCursor I get a dictionary response. I would like to be able to change between them for different connections within a script.

I've written a little function with a context manager to yield the cursor but it requires me to specify the name of the cursorclass each time. I know I can get around this, but knowing the name of the default cursorclass would be nice.

from contextlib import contextmanager
import pymysql

@contextmanager                                                                                                                                                                                                                               
def openDb(host=DB_HOST, database=DB_DATABASE,                                                                                                                                                                                     
           user=DB_USER, cursor=DB_CURSOR):                                                                                                                                                                                        
    """                                                                                                                                                                                                                                       
    Simple context manager for opening a db connection                                                                                                                                                                                        
    """                                                                                                                                                                                                                                       
    with pymysql.connect(host=host, database=database, user=user,                                                                                                                                                                             
                         cursorclass=cursor) as cur:                                                                                                                                                                                          
        yield cur   

I could probably write this as:

@contextmanager                                                                                                                                                                                                                               
def openDb(host=DB_HOST, database=DB_DATABASE,                                                                                                                                                                                     
           user=DB_USER, cursor=None):                                                                                                                                                                                        
    """                                                                                                                                                                                                                                       
    Simple context manager for opening a db connection                                                                                                                                                                                        
    """
    if cursor:                                                                                                                                                                                                                                   
        with pymysql.connect(host=host, database=database, user=user,                                                                                                                                                                             
                             cursorclass=cursor) as cur:                                                                                                                                                                                          
            yield cur
    else:
        with pymysql.connect(host=host, database=database, user=user) as cur:                                                                                                                                                                                                                                                                                                                                      
            yield cur  

and let it default to whatever the default cursorclass is, but I would prefer to be explicit.


Solution

  • Of course as soon as I post this I find the answer in via:

    >>> import pymysql                                                                                                                                                                                                                            
    >>> help(pymysql.cursors)
    
    Help on module pymysql.cursors in pymysql:
    
    NAME
        pymysql.cursors - # -*- coding: utf-8 -*-
    
    CLASSES
        builtins.object
            Cursor
                SSCursor
            DictCursorMixin
                DictCursor(DictCursorMixin, Cursor)
                SSDictCursor(DictCursorMixin, SSCursor)
    

    pymysql.cursors.Cursor is the answer. Documentation...