1

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

4
  • at the time you a connection you know the type, and can select the querys Commented Jul 31, 2020 at 19:29
  • The issue with that is when you pass the connection object to another function. You'd then need to pass another arg to describe the DBMS name which is not preferable. An attribute that is attached to the connection would be best. Commented Jul 31, 2020 at 19:40
  • This sounds like an XY problem. You should already know the DB-API to use and what operations to run. Please post example code for your use case to illustrate your real issue. Commented Jul 31, 2020 at 20:21
  • Not really, @nbk did really well both in understanding this problem and then provided a solution that satisfies the use case. Commented Jul 31, 2020 at 20:58

1 Answer 1

2

Something like?:

type(con)                                                                                                                                                                  
psycopg2.extensions.connection

type(con)                                                                                                                                                                  
sqlite3.Connection

Or shorter yet:

con.__class__                                                                                                                                                             
psycopg2.extensions.connection

con.__class__                                                                                                                                                             
sqlite3.Connection
Sign up to request clarification or add additional context in comments.

7 Comments

Answers require some exlpantation
The code is the explanation. No point in adding verbiage that does no more then repeat what the code shows.
no answer needs to explain whar the code does, as your first example check the type of the connection variabe that can be checked
Be useful, write your own answer instead of critiquing an answer the questioner already understands.
It's a good solution to get the module name. Preferably, I'm looking to get the DBMS name without the need to map module/class/type to DBMS string.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.