3

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?

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

5 Comments

That was actually the first thing I tried. Got the same error as the first one I mentioned.
I look through pymssql manual, the port variable should be string, is it string in your case?
It is. But again, no quotes of any kind.
please show part of code where are you assign values to variables
b1="sqlserver", b2="3183", b3="admin", b4="pass", b5 = "master"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.