简体   繁体   中英

getting error in establishing a database connection (pyodbc)

I am trying to use pyodbc with sql server. However I am getting the following error:

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

This is my connection code

odbc_conn_str= 'DRIVER= {Microsoft Access Driver (*.mdb)}; DBQ=%s' %db_file
conn= pyodbc.connect(odbc_conn_str)

You are saying you are trying to connect to an SQL server but using a Microsoft Access Driver. Only use a Microsoft Access Driver if connecting to an Access Database/File.

Try the following, this is why I use to connect to a Microsoft SQL Server: sql_connection = pyodbc.connect('DRIVER={SQL Server};SERVER=IPADDRESS\\SQLSERVERINSTANCE;DATABASE=DBNAME;UID=USERNAME;PWD=PASSWORD;Trusted_Connection=no;')

Microsoft have a doc on setting up pyodbc for Microsoft SQL Server: https://docs.microsoft.com/en-us/sql/connect/python/pyodbc/python-sql-driver-pyodbc?view=sql-server-ver15

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM