0

I have the following information about the remote server.

  • IP address
  • Database user name
  • Database password
  • Database name

I can even connect to the remote server using azure data studio, running on my Laptop, which is running on Ubuntu 20.04.

However, this is my requirement

  • Connect to the MSSQL database programmatically from python
  • Simply write a pandas dataframe as a table.

I tried using pyodbc, but whenever I try pyodbc.connect(), I get an error saying

InterfaceError: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found and no default driver specified (0) (SQLDriverConnect)')

Is there some other library I should use instead of pyodbc? Here is my sample code.

#!/usr/bin/env python3
# encoding: utf-8    
import pyodbc
credential='DRIVER=ODBC Driver 18 for SQL Server;SERVER=192.168.101.56;DATABASE=DEMAND_FORECAST;ENCRYPT=yes;UID=della;PWD=strong;Trusted_Connection=yes;'
pyodbc.connect(str=credential) # Throwing error
# InterfaceError: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found and no default 
# driver specified (0) (SQLDriverConnect)')

pyodbc.drivers()
# ['ODBC Driver 18 for SQL Server']
7
  • Sounds like you haven't installed the drivers; have you followed the documentation? Commented Dec 1, 2022 at 9:53
  • 1
    That connection string doesn't look right; you can't provide a username and password and use a trusted connection; it's either SQL Authentication or Windows Authentication, not both. Commented Dec 1, 2022 at 10:06
  • 1
    Also, per the documentation, it should be DRIVER={ODBC Driver 18 for SQL Server}. Commented Dec 1, 2022 at 10:08
  • 1
    "So can the password be called windows authentication then?" I don't understand this sentence. Commented Dec 1, 2022 at 10:18
  • 1
    Parametrising a connection string is possible, yes, just concatenate your parameters to the connection string (like shown in the documentation). How you parametrise those parts is up to you and a very different question to what you're asking; which appears to be typographical errors (the missing braces ({}).) Commented Dec 1, 2022 at 10:20

1 Answer 1

1

Usually you need to do something like: Porbably you are missing the "Driver={SQL Server} or you need to change it to something else

cnxn = pyodbc.connect("Driver={SQL Server};" 
                                  "Server=" your server + ";"
                                  "Database=" your db+ ";"
                                  "UID=" +your uid + ";"
                                  "PWD=" + your PWD
                                  )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.