[Larceny-users] sqlite3 FFI

Peter Keller psilord at cs.wisc.edu
Sat May 24 02:06:23 EDT 2008


On Wed, May 21, 2008 at 08:30:38PM -0400, Ray Racine wrote:
> I have used the FFI to do a bit of binding to libusb for USB device
> listing and status, as well as a near complete binding to the IBM MQ CLI
> client library[1] and a number of miscellaneous Linux O/S apis.  

Ok, so here is a question:

I'm learning to wrap just a couple functions at first, sqlite3_open() and
sqlite2_close(). The C prototypes look like this:

int sqlite3_open(
  const char *filename,   /* Database filename (UTF-8) */
  sqlite3 **ppDb          /* OUT: SQLite db handle */
);

int sqlite3_close(sqlite3 *);

So far, I have this code:

         (foreign-file "/usr/lib/libsqlite3.so")

         (define sqlite3
           (lambda ()
             (make-bytevector sizeof:pointer 0)))

         (define sqlite3-open
           (let ((_sqlite3_open
                   (foreign-procedure "sqlite3_open" '(string boxed) 'int)))
             (lambda (path db)
               (_sqlite3_open path db))))

         (define sqlite3-close
           (let ((_sqlite3_close
                   (foreign-procedure "sqlite3_close" '(boxed) 'int)))
             (lambda (db)
               (_sqlite3_close db))))


When running it on an example, it looks like this:

> (define db (sqlite3))

> db
#vu8(0 0 0 0)

; the db is created on disk and the return code is SQLITE_OK
> (sqlite3-open "/tmp/test.db" db)
0

; Seems filled in with stuff...
> db
#vu8(160 48 95 9)

; this fails with SQLITE_MISUSE
; lsof says the file descriptor for /tmp/test.db is still open. No good...
> (sqlite3-close db)
21

Unfortunately, I've already done something wrong....

I have a feeling that I'm not understanding how boxed is to be used in the
above codebase. In C, I'd pass the address of a db pointer to sqlite3_open,
but I don't see the analogue of that in the FFI.

Any advice?

Thank you.

-pete



More information about the Larceny-users mailing list