As I pursue my python adventures, I of course wanted to learn how to do one of the things I do most - pull data out of MySQL. It seems the simplest way to start doing that is Python is to use MySQL-Python. So, after a downloading the package from SourceForge, and following the build instructions (tip: keep all this junk in a “src” folder in your user directory), I wound up with this loveliness:
error:
command ‘gcc’ failed with exit status 1
How annoying - I recall seeing this error when installing the Django database bindings (more on that some other time), and it’s uber stupid as gcc, a compiler, is indeed installed on OS X (as running a “gcc -V” in the terminal will tell you). Turns out, a little hacking is needed to get this running, and it’s pretty easy, thanks to Red Elephants’ excellent MySQL-Python and Apple OSX 10.5 (Leopard) article. If that still doesn’t work for you, try this:
- Open the site.cfg file
- Uncomment line 13, and change it to read:
mysql_config = /usr/local/mysql/bin/mysql_config - Save it, delete the build directory (rm -r build)
- Run: python setup.py clean
- Try again, starting from Step 5 of the Red Elephant article
Hope that does it for you. Once you have that up and running, you can dive in with this old, but handy, devshed.com article - MySQL Connectivity With Python (actually, this page seems to have the same info in a more readable format). There is one difference I’ve noted so far - to get the last insert id, you create a “cursor” object (as show in the article), and run (assuming you’re working at the interactive prompt):
>> cursor.lastrowid
If you see an “L” - which just means “long number,” (maybe because it’s an “int” field?) you can get rid of it with a print statement:
>> print cursor.lastrowid
And don’t forget the handy “dir” function, which tell you everything an object can do:
>> dir(cursor)
Also note that MySQL-Python has some handy support for transactions and rollbacks (page 7 of the devshed article). Hope this helps alleviate some headaches ;)
Leave a comment