使用 pyodbc 导致错误:未找到数据源名称且未指定默认驱动程序

Using pyodbc cause error: Data source name not found and no default driver specified(使用 pyodbc 导致错误:未找到数据源名称且未指定默认驱动程序)

本文介绍了使用 pyodbc 导致错误:未找到数据源名称且未指定默认驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

from sqlalchemy import Table, Column, databases, Integer, String, ForeignKey, create_engine从 sqlalchemy.ext.declarative 导入 declarative_base从 sqlalchemy.orm 导入会话engine = create_engine('mssql+pyodbc://sa:123@localhost/TrainQuizDB')引擎连接()

TrainQuizDB 是我在 Sql Server 中创建的数据库名称.

有关更多信息,我有 Windows 8.1 64 位,我安装了 Python 3.5.1 32 位版本,我从

解决方案

如 SQLAlchemy 文档的相关部分:

<块引用>

基于主机名的连接不是首选,但支持.必须明确指定 ODBC 驱动程序名称

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

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

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.

解决方案

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')

这篇关于使用 pyodbc 导致错误:未找到数据源名称且未指定默认驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 pyodbc 导致错误:未找到数据源名称且未指定默认驱动程序

基础教程推荐