CLSQL is a free database interface library for Common Lisp. It supports multiple databases, including MySQL.
MySQL's C API has a very handy feature, exposed by most MySQL access libraries, which gives you the ID of the last record inserted on your connection. It is, of course, dependent on your table having an auto-incrementing ID field.
It's not obvious from the documentation (in fact, none of CLSQL's backend-specific functionality is terribly well-documented), but you can get this data in CLSQL, too. Just do this:
(clsql-mysql::mysql-insert-id (clsql-mysql::database-mysql-ptr clsql:*default-database*))
Of course, if you're using an explicit DB connection handle, substitute that. There are a couple of other handy functions exposed which you can use the same technique to get at, too.
Thanks! I just started toying around with CLSQL in the last couple of days, and this turned out to be handy since I frequently want the id of the row I just inserted. I suppose I'll have to poke around the backend-specific code in CLSQL to find out what other gems might be lurking in there...
ReplyDelete