Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyodbc connection error when trying to connect to DB on localhost

Tags:

python

sql

pyodbc

I have a local DB on my machine called 'Test' which contains a table called 'Tags'. I am able to access this DB and query from this table through SQL Server management studio 2008.

However, when using pyodbc I keep running into problems.

Using this:

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

yields the 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 specifying the port)

Trying an alternative connection string:

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

yields no error, but then:

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

yields the error:

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

Why could this be?

like image 900
tafi Avatar asked Oct 02 '11 16:10

tafi


People also ask

Can Pyodbc connect to MySQL?

Connecting to MySQL from Python using ODBC Driver for MySQLFirst we import the pyodbc module, then create a connection to the database, insert a new row and read the contents of the EMP table while printing each row to the Python interactive console.

What is the alternative to Pyodbc?

Top Alternatives to pyodbcThe AWS SDK for Python. Pytest: simple powerful testing with Python. Python wrapper for the Cloudflare v4 API. Powerful data structures for data analysis, time series, and statistics.

Can we connect to Oracle database using Pyodbc?

Oracle database storage comes with ODBC support, but in order to connect to Oracle from Windows, macOS, or Linux, you must install ODBC driver for Oracle, otherwise the pyodbc module will not be able to establish a connection to the database.


1 Answers

I tried changing your query to

SELECT * FROM Test.dbo.Tags

and it worked.

like image 152
Wayne Avatar answered Oct 03 '22 21:10

Wayne