Hi Guys I'm New to Python Programming but i have got to this stage ok, but have hit a wall which i hope you can help me with. What i am trying to do is import a CSV file to a DB, This part is working. The problem i am currently having is getting the DB connections to import its setting from a config file. Python Version 3.3.4 Using modules pyodbc, csv, configparser. Here is the Code. ************************************ import pyodbc, csv, configparser #Read Connection Configuration file for DB config = configparser.ConfigParser() config.read('DB.cfg') config.Sections() ['DB'] connStr = ( r''Driver=config.get('DB', 'Driver');'' + r''Server=config.get('DB', 'UID');'' + r''Database=config.get('DB', 'Database');'' + r''UID=config.get('DB', 'UID');'' + r''PWD=config.get('DB', 'PWD');'' ) db = pyodbc.connect(connStr) ************************************ The error i receive is. ************************ r''Driver=config.get('DB', 'Driver');'' + ^ SyntaxError: invalid syntax ************************* Thanks in advance.
How about: Code: connStr = ( "Driver="+ config.get('DB', 'Driver') + ";Server=" + config.get('DB', 'UID') + ";Database=" + config.get('DB', 'Database') + ";UID="+ config.get('DB', 'UID') + ";PWD=" + config.get('DB', 'PWD') )
Thanks Badders didn't work new error was bad syntax for "Driver=" i have a look this weekend. I think the issue is the way i am calling the sections from with the pyodbc modules script.
Your double quotes should be actual double quote chars (") not pairs of single quotes ('') - they may look very similar but they do very different things!
Ok have resolved this so thanks to you all. So for anyone interest. Here is the Code and the Config file. **CODE** import pyodbc, csv, configparser #Read Connection Configuration file for DB config = configparser.ConfigParser() config.read('DB.cfg') config.sections() [] connStr = ( r" Driver="+ config.get('Database', 'Driver') + r" Server="+ config.get('Database', 'Server') + r" Database="+ config.get('Database', 'Database') + r" UID="+ config.get('Database', 'UID') + r" PWD="+ config.get('Database', 'PWD') + r" trusted_connection="+ config.get('Database', 'trusted_connection') ) #print(connStr) Put in to check string value, before executing. db = pyodbc.connect(connStr) **DB.CFG** CONFIG file [Database] Driver= {SQL SERVER}; Server= IP/DNS,1433\INSTANCE; Database= DATABASENAME; UID= USERNAME; PWD= PASSWORD; trusted_connection= no;