Connecting to localdb from pyodbc

Viewed 3071

Tried all sorts of variations to connect from python pyodbc to my local SQL Server

enter image description here

Also tried doing the \ escape

'Server=(LocalDB)\\MSSQLLocalDB;'
'UID=domain\\uname;'

but still get this error

I am able to connect to the same localdb from SSMS.

this is the error message screenshot

enter image description here

2 Answers

First you should choose between providing uid/password and Trusted_Connection I would suggest you to construct the connection string as follows:

"DRIVER={SQL Server};SERVER=localhost;DATABASE=TestDB;UID=$user;PWD=$password"

Furthermore, if localhost does not solve your issue you can use (local) also and for a named instance localhost\$instance_name, you do not have to put 2 backslashes. Last I would strongly suggest you to upgrade your driver to the latest version. As you can easily set it in the connection string as follows:

"DRIVER={ODBC Driver 17 for SQL Server};SERVER=localhost;DATABASE=TestDB;UID=$user;PWD=$password"

In addition you can check this link

Related