python - 使用 pyodbc 导致错误 : Data source name not found and no default driver specified

标签 python sql-server sqlalchemy odbc pyodbc

我正在使用 pyodbc 连接 SQL Server。我创建了这样的连接字符串:

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是我在Sql Server中创建的数据库名称。

有关更多信息,我有 Windows 8.1 64 位,安装了 python 版本 3.5.1 32 位,并从 http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyodbc 下载了 pyodbc (pyodbc-3.0.10-cp35-none-win32.whl)。 但是当我尝试连接时会导致此错误:

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

我还测试了 ODBC 数据源中的连接,并且成功。

ODBC Connection

最佳答案

relevant section of the SQLAlchemy documentation 中所述:

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

因此,您需要将驱动程序名称添加到连接字符串中:

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

关于python - 使用 pyodbc 导致错误 : Data source name not found and no default driver specified,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37161574/

相关文章:

python - 为什么我们使用 numpy.argmax() 从 numpy 预测数组中返回索引?

python - 使用另一列的滚动值的数据框百分位

SQL Server 2018如何将信息分组到不同的列中

sql-server - 如何进行数据库模式的版本控制?

python - SqlAlchemy 没有定义这样的多态身份

python - ZeroMQ 对于数据库事务来说太快了

python - 无法深度复制同时定义了 __init__ 和 __new__ 的类

Python - Django 从表中选择值 "IN"- 基数为 10 的 int() 的错误文字无效 : ','

Sql聚合函数获取列表

python - 关闭 session 后使用SQLAlchemy实例