---
title: Poor Man's MySQL API for Gambit-C
date: '2006-06-11T19:00:00Z'
categories:
- Technology
tags:
- API
- Gambit
- hacks
- Lisp
- MySQL
- Scheme
summary: A lazy man's MySQL API for Gambit-C Scheme, wrapping the `mysql` and `mysqladmin` command-line utilities.
original:
  url: http://bendiken.net/2006/06/11/poor-mans-mysql-api-for-gambit
  archive: https://web.archive.org/web/20080518020750/http://bendiken.net/2006/06/11/poor-mans-mysql-api-for-gambit
---

Lately I’ve been familiarizing myself with
[Gambit-C](https://web.archive.org/web/20060611/http://www.iro.umontreal.ca/~gambit/).
This has mostly been preparatory work in anticipation of the imminent release of
[Termite](https://web.archive.org/web/20060611/http://toute.ca/), [Guillaume
Germain](https://web.archive.org/web/20060611/http://www.iro.umontreal.ca/~germaing/'s)
distributed computing system inspired by
[Erlang](https://web.archive.org/web/20060611/http://www.erlang.org/) and
implemented using Gambit’s serializable continuations and lightweight processes,
scalable up to millions of concurrent threads.

Gambit is definitely one of the more interesting
[Scheme](/tags/scheme)
implementations out there, not to mention among the fastest; its creator Marc
Feeley has been described as a [performance
junkie](https://web.archive.org/web/20060611/http://lists.nongnu.org/archive/html/chicken-users/2006-03/msg00175.html),
and according to the [Gambit
manual](https://web.archive.org/web/20060611/http://www.iro.umontreal.ca/~gambit/doc/gambit-c_1.html#SEC1),
*“with appropriate declarations in the source code the executable programs
generated by the compiler run roughly as fast as equivalent C programs.”*

However, despite its technical excellence, Gambit doesn’t, as of yet, have much
of a standard library.

Yesterday I needed to interface Gambit with
[MySQL](https://web.archive.org/web/20060611/http://www.mysql.com/), and
couldn’t find any existing binding to let me do that (though an SQLite 2.x
interface [does
exist](https://web.archive.org/web/20060611/http://mailman.iro.umontreal.ca/pipermail/gambit-list/2005-January/000082.html)).
As I wasn’t familiar enough with Gambit’s FFI system, I took the über-simple,
lazy man’s approach: I created a trivial wrapper API on top of the
[`mysql`](https://web.archive.org/web/20060611/http://dev.mysql.com/doc/refman/5.0/en/mysql.html)
and
[`mysqladmin`](https://web.archive.org/web/20060611/http://dev.mysql.com/doc/refman/5.0/en/mysqladmin.html)
command line utilities.

I now present here the result: the [Poor Man’s MySQL API for
Gambit-C](https://web.archive.org/web/20060611/http://bendiken.net/files/gambit/gambit-mysql-wrapper.tar.bz2).

Most of the generally useful functions in the [MySQL C
API](https://web.archive.org/web/20060611/http://dev.mysql.com/doc/refman/5.0/en/c-api-function-overview.html)
are implemented, though not necessarily yet tested. The wrapper functions
generally take the same parameters as defined in MySQL’s C API, with the obvious
Lispy changes.

If you are using Gambit-C 4.0 on a Unix-like system and have the aforementioned
two MySQL utilities in your `PATH`, you should be able to use this wrapper
without much trouble. My own configuration is Gambit-C 4.0 beta 17 and MySQL 4.1
running on a Mac OS X 10.4 system.

Here’s a short usage example:

```scheme
(define *user*   "root")
(define *passwd* "root")

(let* ((conn   (mysql-connect user: *user* password: *passwd*
                              db: "mysql"))
       (query  "SELECT host, user, password FROM user")
       (result (mysql-query conn query)))
  (while (mysql-fetch-row result)
    (lambda (row)
      (pretty-print row)))
  (mysql-close conn))
```

You can download the code here:\
[http://bendiken.net/files/gambit/gambit-mysql-wrapper.tar.bz2](https://web.archive.org/web/20060611/http://bendiken.net/files/gambit/gambit-mysql-wrapper.tar.bz2)

This wrapper will hopefully serve as a useful interim solution for Gambit
hackers till a proper MySQL FFI becomes available. The same approach could also
easily be adapted to e.g. get some communication going from Gambit to
[PostgreSQL](https://web.archive.org/web/20060611/http://www.postgresql.org/) —
or any other database system providing similar command-line utilities, for that
matter.
