使用 Python、FreeTDS 和 pyodbc 从 Raspberry Pi 3 查询 MSSQL 服务器 201



我正在尝试使用Python作为我在Raspberry Pi3上的脚本语言查询MSSQL server 2012。

我需要创建一个应用程序来查询MSSQL服务器并返回一些需要在H.M.I上显示的值。我选择了Raspberry Pi平台来开发这个解决方案,使用Python作为编程语言。我在Windows 7 PC上使用PyCharm创建了脚本,并且运行良好。当我把它移到树莓平台时,它不起作用。

我正在使用pyODBC进行连接和查询,并使用FreeTDS作为驱动程序。我使用以下过程进行设置:

sudo apt-get install freetds-dev freetds-bin unixodbc-dev tdsodbc
pip3 install pyODBC

配置了/etc/freetds.conf 文件,如下所示

[NAME]
host = ipAddress
port = 1433
tds version = 7.4
instance = dbInstanceName

然后我转到命令行并测试了连接:tsql -S NAME -U username.然后命令行提示Password:所以我输入了密码,我得到了以下内容:

locale is "enGB.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1>

由于没有错误,我只能假设这已经奏效了?

然后,我设置了/etc/odbcinst.ini 文件,如下所示:

[FreeTDS]
Driver = /usr/lib/arm-linux-gnueabihf/odbc/libtdsodbc.so

然后我按如下方式设置/etc/odbc.ini 文件:

[NAME1]
Driver = /usr/lib/arm-linux-gnueabihf/odbc/libtdsodbc.so
Description = MSSQL Server
Trace = No
Server = ipAddress
Database = dbName
Port = 1433
TDS_Version = 7.4

然后我在命令行中使用 isql 函数对此进行了测试:isql NAME1 user password,我得到了以下提示:

+-------------------------------------------------+
| Connected!
|
| sql-statement
| help [tablename] 
| quit
|
+-------------------------------------------------+
SQL> 

所以我输入了select getDate(),并返回了日期和时间。

但是在 Python 中我仍然无法获得连接,我在解释器中键入了以下内容:

import pyodbc
conn = pyodbc.connect('DRIVER=FreeTDS;SERVER=NAME;DATABASE=dbName;UID=user;PWD=password')

然后当我输入这个cu = conn.cursor()时,我收到一个错误:

AttributeError: 'NoneType' object has no attribute cursor

在 Raspberry pi2 和 Python 3 上使用 Raspbian 和 MS Sql server 2008 进行测试

确保您的APT-Get库和Python版本是最新的">

sudo apt-get dist-upgrade
Sudo apt-get install python3

运行以下命令以安装要求

sudo apt-get install unixodbc
sudo apt-get install unixodbc-dev
sudo apt-get install freetds-dev
sudo apt-get install tdsodbc
sudo apt-get install freetds-bin 

在终端中,现在运行:(使用"pip3",因为由于某些错误,pyodbc 不会为 pip(python 2(安装(

sudo pip3 install pyodbc
sudo apt-get install python-pyodbc

像这样更改 freeTDS.conf

sudo nano /etc/freetds/freetds.conf
Add a block like this :
[sqlserver]
host = 182.172.2.2    # Remote Sql Server's IP addr
port = 1433           # this is default
tds version = 7.0     # this is by the time i post this
instance = Test1      # your Database name 

然后设置/etc/odbcinst.ini 文件,如下所示:

[FreeTDS]
Description = FreeTDS unixODBC Driver
Driver = /usr/lib/arm-linux-gnueabihf/odbc/libtdsodbc.so
Setup = /usr/lib/arm-linux-gnueabihf/odbc/libtdsodbc.so
UsageCount = 1

然后设置/etc/odbc.ini 文件,如下所示:

[NAME1]
Driver = /usr/lib/arm-linux-gnueabihf/odbc/libtdsodbc.so
Description = MSSQL Server
Trace = No
Server = Server2      # IP or host name of the Sql Server
Database = Test1      # DataBase Name
Port = 1433           # This is default
TDS_Version = 7.4

现在使用此命令测试连接(使用第二个命令,您应该获得对 Sql 服务器的命令行访问权限

tsql -S sqlserver -U username
isql NAME1 user 'password'

最后是代码部分:

import pyodbc
conn = pyodbc.connect('DRIVER={FreeTDS};Server=Server2;PORT=1433;DATABASE=Test1;UID=user;PWD=pass;TDS_Version=7.2;')
cursor = conn.cursor()
cursor.execute("Select * from Table1")
for row in cursor.fetchall():
print (row)

最后,如果没有任何效果,请尝试以下操作:

sudo dpkg-reconfigure tdsodbc

尝试将 RPi 3 连接到我在 SQL Server 中的数据库,以捕获 GPIO 状态/值。

我已经使用了一个名为Webiopi的Web服务器,python用于执行宏,并定义GPIO函数/值。

可能吗?

If(possible == yes)
{
happiness = happiness + 10;
return view(success)
}
else
{
happines = 0;
return view(keep_searching);
}

问题出在连接字符串上。下面是 FreeTDS 的完整连接字符串示例:

conn = pyodbc.connect(
'DRIVER={FreeTDS};SERVER=yourfqdn.com;PORT=1433;DATABASE=your_db;UID=your_username;PWD=your_pw;TDS_Version=7.4;'
)

尝试将{FreeTDS}放在大括号中并显式添加TDS_Version。我还选择在我的 Python 连接字符串中使用 FQDN,并将用户名/密码设置为环境变量,以将配置保存在一个位置。

最新更新