SQLAlchemy 无法连接到 mssql 数据库 [英] SQLAlchemy can't connect to an mssql database

查看:40
本文介绍了SQLAlchemy 无法连接到 mssql 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的简单测试脚本.只是试图做一个基本的选择语句.在教程中找到了基本部分.

Here's my simple test script. Just trying to do a basic select statement. Found the basic bits on a tutorial.

from sqlalchemy import *

db = create_engine('mssql+pyodbc://user:pass@ip_address/database_name')    

db.echo = True 
metadata = MetaData(db)

users = Table('member', metadata, autoload=True)

def run(stmt):
    rs = stmt.execute()
    for row in rs:
        print row

s = users.select(users.c.fname == 'Bill')
run(s)

经过一个小时的四处搜索并尝试了一些解决方案后,我离解决它的距离并不比开始时更近.希望我只是在某处犯了一个简单的错误,但我找不到它...

After an hour of searching around and trying a few solutions, I'm no closer to solving it than when I started. Hopefully I've just made a simple error somewhere, but I'm unable to find it...

这是我遇到的错误

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

任何帮助将不胜感激!

推荐答案

如果未在 URL 中指定,mssql+pyodbc 方言的默认驱动程序将是SQL Server"[1].这意味着您需要在/etc/unixODBC/odbcinst.ini 中有一个这样的部分:

If not specified in the URL, the default driver for the mssql+pyodbc dialect would be "SQL Server" [1]. That means you need to have a section that reads like this in /etc/unixODBC/odbcinst.ini:

[SQL Server]
Driver=/path/to/library.so

它在 Windows 上自动"工作,因为如果您打开 Administrator Tools -> Data Sources (ODBC),您很可能会找到一个名为SQL Server"的条目"在驱动程序标签下.

It works "automatically" on Windows, because if you open Administrator Tools -> Data Sources (ODBC), you would most likely find an entry named "SQL Server" under the Drivers tab.

在 Linux 上,您可以使用 FreeTDS 驱动程序,也可以使用 Microsoft 的官方驱动程序(我推荐这个).

On Linux, you can either use the FreeTDS driver, or the official driver from Microsoft (I recommend this).

安装驱动程序后,您应该在/etc/unixODBC/odbcinst.ini 中有这样的内容:

After installing the driver, you should have something like this in /etc/unixODBC/odbcinst.ini:

[FreeTDS]
Driver=/usr/lib/libtdsodbc.so
Threading=1

[ODBC Driver 11 for SQL Server]
Description=Microsoft ODBC Driver 11 for SQL Server
Driver=/opt/microsoft/msodbcsql/lib64/libmsodbcsql-11.0.so.2270.0
Threading=1
UsageCount=1

然后,您只需向 URL 添加一个 driver 查询字符串参数,其值与部分名称匹配.

Then, you just have to add a driver query string parameter to the URL, with value that matches the section name.

带有 FreeTDS 的示例 URL:

Sample URL with FreeTDS:

mssql+pyodbc://user:pass@ip_address/database_name?driver=FreeTDS

带有官方驱动程序的示例 URL:

Sample URL with the official driver:

mssql+pyodbc://user:pass@ip_address/database_name?driver=ODBC+Driver+11+for+SQL+Server

[1] //bitbucket.org/sqlalchemy/sqlalchemy/src/aa3a8f016f3e4396d125b18b0510abdf72aa8af2/lib/sqlalchemy/dialects/mssql/pyodbc.py?at=default#cl-236

这篇关于SQLAlchemy 无法连接到 mssql 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆