Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: how to know which database I am connecting to?

Tags:

python

sql

I have a very odd problem. I am able to access a database because someone installed the right ODBC drivers on my computer, but I dont know

  • what type of ODBC drivers are installed
  • which flavor of SQL is it

All I can do is

import pyodbc
import pandas as pd
import numpy as np

cnxn = pyodbc.connect('DSN=MYDSN')
sql = "SELECT * FROM MASTER.PRICES"
cursor.execute(sql)
data = cursor.fetchone()

which returns some data.

But thats all I know. Which SQL command can I run to know more about this mysterious database?

Thanks!

like image 915
ℕʘʘḆḽḘ Avatar asked Mar 08 '26 23:03

ℕʘʘḆḽḘ


1 Answers

pyodbc provides the getinfo method.

>>> cnxn.getinfo(pyodbc.SQL_DBMS_NAME)
'MySQL'
>>> cnxn.getinfo(pyodbc.SQL_DBMS_VER)
'10.1.21-MariaDB'
>>> cnxn.getinfo(pyodbc.SQL_DRIVER_NAME)
'libmyodbc5w.so'

Here are just a few examples, you can find all the available constants in the documentation.

like image 64
julienc Avatar answered Mar 11 '26 13:03

julienc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!