2

I'm having difficulties trying to connect to a SQL Server instance in pypyodbc. The server name has a backslash in it eg "servername\something", instead of just the servername.

In pypyodbc, i've tried all different variations,

Server=servername\something
Server=servername\\something
Server=[servername\something]
Server=[servername\\something]

But none of them seem to work. I get the following error message

pypyodbc.DatabaseError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied.')

How can I pass the name of the server?

I can connect to other servers fine as long as they don't have the backslash

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
vksf
  • 25
  • 4

1 Answers1

0

As indicated in the Microsoft ODBC driver documentation, for the Server= argument in your connection string

To specify a named instance of SQL Server, append \InstanceName.

So the question really is "How to get a single backslash into a Python string?"

One way is to use raw string literals (r""). This works for me:

conn_str = (
    r"Driver={ODBC Driver 11 for SQL Server};"
    r"Server=PANORAMA\SQLEXPRESS;"
    r"Database=myDb;"
    r"Trusted_Connection=yes;"
    )
conn = pypyodbc.connect(conn_str)
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418