对于许多人共享的python脚本,我需要一个检测用户驱动程序的解决方案,以便我可以动态构建连接字符串。我目前正在使用os环境COMPUTERNAME,如:
if os.environ['COMPUTERNAME'] == 'MattsDesktop':
print("You are using: Oracle 12")
self.DataConnectOracle = 'DRIVER={Oracle in 12cR1client32bit};SERVER=Xyz;DBQ=DEVenv;UID=AllDev;PWD=Secret2Secret'
但是每当用户进行更改时都必须更新。好像我应该能够获得他们的驱动程序信息而不必维护这些信息。
答案 0 :(得分:1)
虽然它不会为您提供驱动程序的确切版本,但pypyodbc.drivers()
会为您提供Windows上可用的驱动程序的名称。这应该足够了,因为你只需要驱动程序名称(而不是版本)来构造连接字符串。
例如,在我的机器上
import pypyodbc
drivers_list = sorted(pypyodbc.drivers())
for driver_name in drivers_list:
print(driver_name)
打印
Driver da Microsoft para arquivos texto (*.txt; *.csv)
Driver do Microsoft Access (*.mdb)
Driver do Microsoft Excel(*.xls)
Driver do Microsoft Paradox (*.db )
Driver do Microsoft dBase (*.dbf)
Driver para o Microsoft Visual FoxPro
Microsoft Access Driver (*.mdb)
Microsoft Access Driver (*.mdb, *.accdb)
Microsoft Access Text Driver (*.txt, *.csv)
Microsoft Access dBASE Driver (*.dbf, *.ndx, *.mdx)
Microsoft Access-Treiber (*.mdb)
Microsoft Excel Driver (*.xls)
Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)
Microsoft Excel-Treiber (*.xls)
Microsoft FoxPro VFP Driver (*.dbf)
Microsoft ODBC for Oracle
Microsoft Paradox Driver (*.db )
Microsoft Paradox-Treiber (*.db )
Microsoft Text Driver (*.txt; *.csv)
Microsoft Text-Treiber (*.txt; *.csv)
Microsoft Visual FoxPro Driver
Microsoft Visual FoxPro-Treiber
Microsoft dBase Driver (*.dbf)
Microsoft dBase VFP Driver (*.dbf)
Microsoft dBase-Treiber (*.dbf)
MySQL ODBC 5.3 ANSI Driver
MySQL ODBC 5.3 Unicode Driver
ODBC Driver 11 for SQL Server
SQL Server
SQL Server Native Client 11.0
SQLite3 ODBC Driver
有关Oracle驱动程序的列表,我可以使用
oracle_list = [x for x in pypyodbc.drivers() if "Oracle" in x]