Using pyodbc cause error: Data source name not found and no default driver specified

余生长醉 提交于 2019-11-30 09:05:29

问题


I'm using pyodbc to connect SQL Server. I had created connection string like this:

from sqlalchemy import Table, Column, databases, Integer, String, ForeignKey, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import session
engine = create_engine('mssql+pyodbc://sa:123@localhost/TrainQuizDB')
engine.connect()

TrainQuizDB is database name that I created in Sql Server.

For more information I have windows 8.1 64bit and I installed python version 3.5.1 32bit and I downloaded pyodbc from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyodbc (pyodbc-3.0.10-cp35-none-win32.whl). But when I try to connect it cause this error:

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

Also I have tested the connection In ODBC Data sources and it was successful.


回答1:


As noted in the relevant section of the SQLAlchemy documentation:

Hostname-based connections are not preferred, however are supported. The ODBC driver name must be explicitly specified

So, you need to add the driver name to your connection string:

engine = create_engine('mssql+pyodbc://sa:123@localhost/TrainQuizDB?driver=ODBC+Driver+17+for+SQL+Server')


来源:https://stackoverflow.com/questions/37161574/using-pyodbc-cause-error-data-source-name-not-found-and-no-default-driver-speci

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!