Tuesday, June 4, 2013

Getting Python To Work With Filemaker

Getting Python To Work With Filemaker

Okay, so getting python to work with Filemaker can be a bit of a pain, mainly due to the lack of information. These instructions are for python 2.7 of OS X 10.8.3 and "pyodbc-3.0.6-py2.7-macosx-10.8-intel" but they have worked on other OS X and python versions.

Prerequisites

You need a working knowledge of python, installing python packages and creating filemaker databases. The order for some steps is arbitrary. For extra information you can locate the ODBC JDBC Guide for Filemaker 12. Also I and others have posted information on the pyodbc wiki on connection strings. I was informed recently that pyodbc crashed with a segfault 11 when it was run and I vaguely remember running into this problem myself but cannot remember the solution. If you run into this problem please let me know (add a comment or email me) and the steps you followed, particularly if you find a solution. I know it segfaulted once I upgraded my OS X Server to the latest version but I cannot remember how I solved it. Good luck!

Step 1 - Install Filemaker and pyodbc

So the first step for me was to install Filemaker and then to install pyodbc. I used pip (or easy_install) to install pyodbc: pip install pyodbc

Step 2 - Locate and Install ODBC Driver

The ODBC client driver was located on the package image as you can see down on the left hand side of Finder. Locate the xDBC folder and inside that locate the ODBC Client Driver Installer and install it.


Step 3 - Create A Sample Filemaker Database and Populate With Some Data

I created a simple database with an auto incrementing ID field and a name field and then entered some names.

Step 4 - Create A Username And Password For The Database

Later on, when trying to connect to the database the username and password seem to be required in the connection string so you may as well make them now.


Step 5 - Turn ODBC/JDBC Sharing On In Filemaker

I turn on sharing as well as check the "All Users" option for my database. This is database specific so if you use a different database this step will need to be repeated.


Step 6 - Install The ODBC Administrator Tool For OS X


Once it is installed you will find it in the Utilities folder that is located in your Applications folder.

Step 7 - Add And Configure Your ODBC Driver For Filemaker

Open the ODBC Administrator tool and add your connection in either the user or system DSN tab. Follow the prompts and use the screenshots as a reference.









 Step 8 - Run A Python Script To Test If It Works

Okay, so now you should be able to test it. Here is a script I used to test "Testing" database. You can see in the connection string where the DSN is the name of the database that you setup in the ODBC Administrator tool and the UID and PWD are the username and password you setup in Filemaker. If you don't close your connection then you may have trouble shutting filemaker down.

import pyodbc

pyodbc.pooling = True

CONNECTION_STRING = "UseRemoteConnection=0;Pooling=No;PoolAllAsText=0; ApplicationUsingThreads=1;FetchChunkSize=100;FileOpenCache=0;MaxTextLength=255;DSN=Testing;UID=Michael;PWD=testing;"

connection = pyodbc.connect(CONNECTION_STRING)
cursor = connection.cursor()
rows = cursor.execute("select * from Testing")

for row in rows:
    print row

connection.close()