[plt-scheme] Re: SQLite error

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Wed Jul 22 06:42:30 EDT 2009

2009/7/22 Thomas Chust <chust at web.de>:
> I'm not sure whether it would really be worth the effort, but you
> should in principle be able to tell the difference by using the pzTail
> output parameter of the SQLite API function that compiles the
> statement: If the tail pointer passed out is identical to the SQL
> source pointer passed in, nothing has been compiled, if it's
> different, at least one statement has been consumed by the parser.
>
> Probably it is not completely trivial to implement that logic, though,
> because the SQL source pointer passed to the SQLite API call is not
> directly visible to Scheme code but rather some buffer allocated by
> the FFI.

Fortunately, the tail is already available. It is used in the current
error message:

(define (prepare db sql)
 (let*-values
     ([(stmt prep-status tail)
       (sqlite3_prepare_v2 (db-handle db) sql)]
      [(the-stmt) (wrap-finalizer (make-statement db stmt)
                                  finalize)])
   (when (not (zero? (string-length tail)))
     (sqlite-error "You should only prepare one statement at a time! ~s" tail))
   (when (handle-status db prep-status)
     (if stmt
         the-stmt
         ;; the pointer is null; SQLite didn't raise an
         ;; error but should have!
         (sqlite-error "sqlite3_prepare_v2 returned a NULL pointer")))))

Yet another way to get the "prepare one statement at a time" error,
is to prepare a CREATE TABLES statement with an already existing table
name.

--
Jens Axel Søgaard


Posted on the users mailing list.