Arc Forumnew | comments | leaders | submitlogin
3 points by sacado 6036 days ago | link | parent

OK, here we go :

  ; Implementation note : mzscheme's #f can be used as the C NULL pointer
  (= NULL* #f)

  (= callbacks* nil)
  (def sqlite3-callback args t)
  (let cb (ffi-callback sqlite3-callback (list cptr cint cptr cptr) cint)
    (push cb callbacks*) cb)

  ; Here, we defined cb as a callback of type cptr cint cptr cptr :
  ; no cstring here, as we have char** and not char *

  (def topcb () (let cb (pop callbacks*) (push cb callbacks*) cb))

  (w/ffi "libsqlite3" ;in mac os x "sudo port install sqlite3"
    (cdef sqlite3-open "sqlite3_open" cint (cstring cptr))
    (cdef sqlite3-exec "sqlite3_exec" cint (cptr cstring cfptr cptr cptr))
    ; Here again, cptr instead of cstring for error : we need a ptr to string
  (cdef sqlite3-close "sqlite3_close" cint (cptr)))

  ; Last warning : 2nd arg to sqlite3-open is a sqlite3**, i.e.
  ;  it returns the address of the actual sqlite3* used by sqlite3-exex & sqlite3-close

  (= &sqlite3 (cmalloc 4)) ; address of the actual sqlite3*
  (= sqltxt "create table bar (id integer primary key, text varchar(255));")
  (= sqlerrormsg (cmalloc 4))
  (= sqltxtptr NULL*)

  (sqlite3-open "test" &sqlite3)
  (= sqlite3 (cpref &sqlite3 cptr)) ; dereference to have an sqlite3*
  (sqlite3-exec sqlite3 sqltxt (topcb) sqltxtptr sqlerrormsg)
  (sqlite3-close sqlite3)
Not tried with an actual callback function, though, but on my machine it works perfectly...


2 points by jmatt 6036 days ago | link

Thanks for the help. I'll test this out tonight.

Good to know that C NULL == mzscheme's #f.

-----