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