I am trying to query MSSQL server 2012 using Python as my scripting language on a raspberry Pi3.
I have the need to create an application that will query MSSQL server and return some values that need to be displayed on a H.M.I. I chose the Raspberry Pi platform to develop this solution using Python as the programming language. I created the script using PyCharm on a Windows 7 PC and all worked well. When I moved it to the Raspberry Platform it didn't work.
I am using pyODBC to do the connecting and querying and FreeTDS as the driver. I used the following procedure to set this up:
sudo apt-get install freetds-dev freetds-bin unixodbc-dev tdsodbc
pip3 install pyODBC
Configured the /etc/freetds.conf file as follows
[NAME]
host = ipAddress
port = 1433
tds version = 7.4
instance = dbInstanceName
I then went to the command line and tested the connection with: tsql -S NAME -U username. The command line then prompts with Password: so I typed the password in and I got the following:
locale is "enGB.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1>
As there are no errors present I can only assume that this has worked?
I then set up the /etc/odbcinst.ini file as follows:
[FreeTDS]
Driver = /usr/lib/arm-linux-gnueabihf/odbc/libtdsodbc.so
I then setup the /etc/odbc.ini file as follows:
[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
I then tested this with the isql function in the command line: isql NAME1 user password and I got the following prompt:
+-------------------------------------------------+
| Connected!
|
| sql-statement
| help [tablename]
| quit
|
+-------------------------------------------------+
SQL>
so I typed in select getDate() and the date and time returned.
However within Python I still can't get a connection, I typed the following into the interpreter:
import pyodbc
conn = pyodbc.connect('DRIVER=FreeTDS;SERVER=NAME;DATABASE=dbName;UID=user;PWD=password')
Then when I type this cu = conn.cursor() I get an error:
AttributeError: 'NoneType' object has no attribute cursor