2

I have a script which connects to a database. I'd like to pull out the server,user,password,db from the script and put it into a config file. I'm successfully pulling in the values from the config file. The problem I'm having is the pymssql.connect fails when I have variables in the syntax. I pasted my sample code below. Is this possible?

###database connection  
config = configparser.ConfigParser()  
config.read('test.config')  
server = config['DEFAULT']['SQLServer']  
db = config['DEFAULT']['Database']  
user = config['DEFAULT']['User']  
password = config['DEFAULT']['Password']  

###this works  
####conn = pymssql.connect(host='Server1', user='Joe',password='MyPass', database='MyDB')

###this doesn't
try:
    conn = pymssql.connect(host=server, user=user,password=password, database=db)
except Exception as e:
    print(str(e))
    sys.exit()
Ethan Bierlein
  • 3,353
  • 4
  • 28
  • 42
VonHugenstein
  • 109
  • 2
  • 10
  • Run this in a shell and ensure that the values you've read from the config file are accurate. – Celeo Sep 30 '14 at 18:31
  • 1
    The values are what I expect them to be. When I run it from the console I get this: "'Server1'", "'Joe'", "'MyPass'", "'MyDB'". Would those extra quotes cause a problem? – VonHugenstein Sep 30 '14 at 18:43
  • Could you paste the full error? – Celeo Sep 30 '14 at 18:45
  • From the console: >>> conn = pymssql.connect(server,user,password,db) Traceback (most recent call last): File "pymssql.pyx", line 595, in pymssql.connect (pymssql.c:9251) File "_mssql.pyx", line 1829, in _mssql.connect (_mssql.c:19198) File "_mssql.pyx", line 597, in _mssql.MSSQLConnection.__init__ (_mssql.c:5868) _mssql.MSSQLDriverException: Connection to the database failed for an unknown reason. – VonHugenstein Sep 30 '14 at 18:47
  • During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 1, in conn = pymssql.connect(server,user,password,db) File "pymssql.pyx", line 602, in pymssql.connect (pymssql.c:9370) pymssql.InterfaceError: Connection to the database failed for an unknown reason. – VonHugenstein Sep 30 '14 at 18:48
  • I found [one](http://stackoverflow.com/questions/4150524/unable-to-connect-to-sql-server-via-pymssql) [two](http://stackoverflow.com/questions/17662680/cannot-connect-to-mssql-db-using-pymssql) questions that have the same error you do; hopefully they help. – Celeo Sep 30 '14 at 18:55
  • I don't believe those apply. I am running on windows, and not running Free TDS – VonHugenstein Sep 30 '14 at 19:03
  • Trailing blanks in the config file? – uselpa Sep 30 '14 at 19:48

1 Answers1

0

This is how I retrieved connection properties and connected sql server database

**App.ini file**

[CoreContext]
host=sservername.database.windows.net
user=dbuser@servername
password=password
database=DeltaXCore



**Connection.py file**

appConfig = ConfigParser.ConfigParser()
appConfig.read("App.ini")
ConnectionString.HOST = appConfig.get("CoreContext", "host")
CoreConnectionString.USER = appConfig.get("CoreContext", "user")
CoreConnectionString.PASSWORD = appConfig.get("CoreContext", "password")
CoreConnectionString.DATABASE = appConfig.get("CoreContext", "database")



pymssql.connect(host=objdeltaxConnectionString.HOST, user=objdeltaxConnectionString.USER,password=objdeltaxConnectionString.PASSWORD, database=objdeltaxConnectionString.DATABASE)
Akshay Joy
  • 1,765
  • 1
  • 14
  • 23