Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pymssql connection string using variables

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?

like image 247
John Ellis Avatar asked Feb 11 '26 10:02

John Ellis


1 Answers

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...

like image 183
svfat Avatar answered Feb 13 '26 22:02

svfat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!