Python, UnixODBC, MDBTools and Microsoft Access on Fedora 12

This is funny to me in light of my last post. I have installed UnixODBC, I installed the gui tool to manage it and then I installed MDBTools to allow me to connect to a JET database. (This is all on my Fedora 12 machine by the way for those just joining us.) The funny part is that the time I spent getting the gui tool was wasted as it wont work with the driver anyway, at least not the way I’ve set it up. Not sure why, not sure that I care. Here is what I did to get it to work. First I checked for the driver ( /usr/lib/libmdbodbc.so.0 in my case ). It was there so I edited /etc/odbcinst.ini and added the following entry.

[MDBToolsODBC]
Description = MDB Tools ODBC
Driver = /usr/lib/libmdbodbc.so.0
Setup =
FileUsage =
CPTimeout =
CPReuse =



That takes care of telling UnixODBC about the driver. I wonder if there is a file from MDBTools that I can use for the setup part. Not sure, may look into it at some point. The next step was creating a DSN. That involved adding the following lines to /etc/odbc.ini (Which existed but was empty because I hadn’t created any entries yet.)

[lintry]
Description = Microsoft Access Try DB
Driver = MDBToolsODBC
Database = /home/jr.peck/Documents/lintry.mdb
Servername = localhost
Username =
Password =
port = 5432




Here is what a connection in Python could look like.

import pyodbc


sql = 'Select * from foo'

conn = pyodbc.connect('DSN=lintry')
cursor = conn.cursor()
cursor.execute(sql)
rows = cursor.fetchall()

for record in rows:
    print record.FirstName + ' ' + record.LastName
  
cursor.close()
conn.close()



In my database the table foo contains three columns and the first two are FirstName and LastName. So anyone messing this will obviously need to change it to match their table name, column names, etc. This last piece of getting things to work on Linux wasn’t really necessary but it didn’t take all that long to figure out and I’m glad I did, in case I do need it down the road. I imagine this would work just the same for any other programming language that could take advantage of UnixODBC.

Building A Model for a QTreeView

On my little PyQt email program I wanted to be able to generate a list of who would get the emails. I wanted to see this before I actually sent them. I wanted a name and an email to go with it. This means getting the data into a model and then having the view widget load the model. It took me forever to figure out how to do this, as I’m a bit slow. But now that it is done, I’d better document it a bit because I’ll forget and need to do it again later.

Here’s what actually building the model and loading it looks like:

def mpreview(self):
        query = 'Select Greeting, User2 from Contact where len(User2) > 3 order by Greeting'
        dbase=r'C:\Documents and Settings\JR Peck\My Documents\mpd_stuff\linkedtnt.mdb'
        newdata = mpddata.MpdData(query, dbase)

        total = len(newdata.rows)
        row=0
        model = QtGui.QStandardItemModel(total,2)
        
        model.setHorizontalHeaderItem(0,QtGui.QStandardItem('Greeting'))
        model.setHorizontalHeaderItem(1,QtGui.QStandardItem('Email'))
        
        for record in newdata.rows:
                model.setItem(row,0,QtGui.QStandardItem(record.Greeting))
                model.setItem(row,1,QtGui.QStandardItem(record.User2))
                row = row + 1
                
        self.ui.tblRecip.setModel(model)

The reference for QTableView was helpful. It just took me a while to understand what it would take to build a QStandardItemModel. Once I got that figured out, then it was easy.

Netbeans Python Templates

Ran into Netbeans bug 163029 today. Wish I hadn’t spent a couple hours trying to figure out why it wouldn’t work.

Netbeans has a nifty template system and I decided that I would take advantage of it. I found a handy tutorial to take it a step further and create a template module that could be distributed. Even if no one else ever used this, it would make it easy for me to put the template in on any new install, system, etc. So I went through the steps and things worked nicely, then I decided to change the header to include the license. I went to set my default license and that is where things stopped. I was supposed to be able to add a line to nbproject/project.properties that would cover this. I did, but the template kept loading the default. I thought it was me, but it isn’t.

There is a work-around but it takes place at the user level and requires altering the template in a way that isn’t going to work for anyone else unless they’ve monkeyed with their settings too. Hopefully it will get fixed soon.

Interesting Text Editors

There is an editor called Ommwriter that looks very interesting. I’d try it out, but at the moment it is only available for Macs. But there is a project Marave that is essentially working to build a similar editor using PyQt. Which means it is cross platform.

I downloaded it yesterday and have played with it just a bit. The Google Code site doesn’t have a ton of information but there is more background and info in this blog post about it.

Basically the goal (and this is communicated in a much more elegant way on the Ommwriter site) is to allow the writer to write free from distraction. So the app launches full screen and covers up the entire desktop. The few controls that are there disappear when one is typing. They are shooting for a back to the typewriter days type of experience so it makes clicking sounds when typing. There can also be background music. I turned that off right away. I don’t really want either.

This is a brand new project so there are some rough edges. I emailed in a problem yesterday and it was fixed last night – did an svn update this morning and I’m good. There are no binaries available – just source. Still – installing Python and PyQt is easy – and then it’s easy to run.

I’m partially interested in this because I want to learn PyQt better, but it looks to be a really useful tool as well. Here’s a screen shot with the menu items showing.

marave editor

Python with Netbeans 6.5

This tutorial shows Python developers how to work with Python in the NetBeans IDE 6.5. It covers how to set up which Python runtime you want to use, how to create a new project, how to edit files, and how to run your programs. Use the NetBeans Python Editor for code completion, instant rename, quick fixes, catching unresolved or unused names, and import management. NetBeans also supports an interactive console and a debugger for Python.