Pymssql - connection - database name

Hi,

I am trying to connect to a MS sql database using pymssql, following is my line of code

import pymssql as ms

try:

conn = ms.connect(host='xyz\SQLEXPRESS',user='abc', password='abc', database='Testdb')

except ms.DatabaseError as e:
print('database error ', e.args)

and am always getting following error.
database error ((20009, b’DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (xyz\SQLEXPRESS)\n’),)

Notice that in above error message \ is appended, though i have given single slash.

Please advice solution.

1 Like

actual error message was database error ((20009, b’DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (xyz\\SQLEXPRESS)\n’),)

double \\ which got striped when submitting topic from the browser

The error message is quite literal, it means it cannot find to the server you specified. Possible causes are:

  • You specified the wrong host
  • The database server is not up
  • The host machine blocked the port used by SQL server for connection
  • The client’s OS blocked connection to host
1 Like

The server is very much there and operational, i make frequent connection with same connection string in C#, Asp.net every day.
My instance name is sqlexpress hence i have to append with server name as xyz\sqlexpress.

I think you missed something from my message. there is double backward slash in the string being displayed for the error when though i typed single backward slash.
I faced same issue in Node-Red as well

To avoid the \ issue, you could pass the argument for host as a raw string:

conn = ms.connect(host=r'xyz\SQLEXPRESS',user='abc', password='abc', database='Testdb')

or escape the backslash:

conn = ms.connect(host='xyz\\SQLEXPRESS',user='abc', password='abc', database='Testdb')

However, in general I would recommend using (or at least trying) / instead of \. I’m not 100% certain in the case of the pymyssql connect() function, but they’re typically treated the same for the purpose of defining paths.