pythonmysqlsqlpostgresqlpython-db-api

How to determine if Python DB-API connection object is of a certain DBMS (e.g., PostgreSQL, MySQL)


Let the driver be a DB-API driver (e.g., psycopg2, pymysql) and you can get the connection object via connnection = driver.connect(...). How do I check what kind of DBMS the connection object is connected to: (1) At best the DBMS name or (2) the module name.

Use Case:

I need to make special queries that are of different SQL syntax (e.g., COPY clause for PostgreSQL vs using bulk INSERT for MySQL).


Solution

  • Something like?:

    type(con)                                                                                                                                                                  
    psycopg2.extensions.connection
    
    type(con)                                                                                                                                                                  
    sqlite3.Connection
    

    Or shorter yet:

    con.__class__                                                                                                                                                             
    psycopg2.extensions.connection
    
    con.__class__                                                                                                                                                             
    sqlite3.Connection