How can I pass the values ​​of a configuration variable to the pyopbc connection command?

I have a .ini (configuration file) where I mentioned the server name, database name, username and password with which I can connect my application to MSSQL

self.db = pyodbc.connect('driver={SQL Server};server=homeserver;database=testdb;uid=home;pwd=1234')

the corresponding data mentioned above, the connect statement is now in config.ini `self.configwrite = ConfigParser.RawConfigParser () configread = SafeConfigParser () configread.read ('config.ini')

 driver = configread.get('DataBase Settings','Driver') server = str(configread.get('DataBase Settings','Server')) db = str(configread.get('DataBase Settings','Database')) user = str(configread.get('DataBase Settings','Username')) password = str(configread.get('DataBase Settings','Password'))' 

How can I pass these variables in pyopbc connection statement

I am tired of this self.db = pyodbc.connect('driver={Driver};server=server;database=db;uid=user;pwd=password') receiving an error.

+2
source share
2 answers
 self.db = pyodbc.connect('driver={%s};server=%s;database=%s;uid=%s;pwd=%s' % ( driver, server, db, user, password ) ) 

% s is used to include variables in a string

variables are placed in a string in the order they are after%

+4
source

Other options for the connect function:

 # using keywords for SQL Server authentication self.db = pyodbc.connect(driver=driver, server=server, database=db, user=user, password=password) # using keywords for Windows authentication self.db = pyodbc.connect(driver=driver, server=server, database=db, trusted_connection='yes') 
+6
source

Source: https://habr.com/ru/post/1502690/


All Articles