Podbc connection error when trying to connect to the database on localhost

I have a local database on my machine called "Test", which contains a table called "Tags". I can access this database and query from this table through SQL Server 2008 Management Studio.

However, when using pyodbc, I constantly run into problems.

Using this:

conn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost:1433;DATABASE=Test')

gives an error:

pyodbc.Error: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]Invalid connection. (14) (SQLDriverConnectW); [01000] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Invalid Instance()). (14)')

(with or without port)

Attempting an alternate connection string:

conn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost\Test,1433')

does not give an error, but then:

cur = conn.cursor()
cur.execute("SELECT * FROM Tags")

gives an error:

pyodbc.ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'Tags'. (208) (SQLExecDirectW)")

Why could this be?

Thank.

+5
source share
4 answers

I tried changing your request to

SELECT * FROM Test.dbo.Tags

and he worked.

+4
source

. ( Windows):

conn = pyodbc.connect('Trusted_Connection=yes', driver = '{SQL Server}',
                      server = 'localhost', database = 'Test')
cursor = conn.cursor()
# assuming that Tags table is in dbo schema
cursor.execute("SELECT * FROM dbo.Tags")
+1

conn = pyodbc.connect ('DRIVER = {SQL Server}; SERVER = localhost: 1433; DATABASE = Test')

This connection is missing an instance name and the port should not be written like this.

my connection is this:

cn=pyodbc.connect('DRIVER={SQL Server};SERVER=localhost\SQLEXPRESS;PORT=1433;DATABASE=ybdb;UID=sa;PWD=*****')

enter image description here

0
source

Try replacing "localhost" with "(local)" or ".". This solution solved the problem for me.

0
source

All Articles