3
votes

I'm writing a script that connects to MSSQL DB using pymssql module. I couldn't find a way to make the connect method to work using variables.

This works:

a = pymssql.connect(host='sqlserver', port=3183,user='admin',password='pass',database='master')

This does not (b1-5 are variables):

a = pymssql.connect(b1,b2,b3 b4,b5)
(Like shown in first example in www.pymssql.org/en/latest/pymssql_examples.html)

I'm getting this error:

File "pymssql.pyx", line 636 in pymssql. connect (pymssql. c:10178) pymssql.OperationalError: (20009, 'DB-Lib error message 20009,severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist\nNet-Lib error during Unknown error (10035)\n')

The database is fine, I can manually log in and the literal connection string works. my variables (b1-5) include no single nor double quotes. When I'm using single quotes I'm getting

Connection to database failed for an unknown reason.

Do you have an idea what could be the problem?

2

2 Answers

3
votes

You should write:

a = pymssql.connect(host=b1, port=b2,user=b3,password=b4,database=b5)

Where b1 is actually a HOST, b2 is a PORT, and so on...

-4
votes

Like this is ok!

Looking for a long time! Explicit call on it

  找半天!显式的调用下就可以了