[racket] racket/db: fetching multiple rows with multiple columns in a "for" loop

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Tue Feb 5 12:17:25 EST 2013

On 02/05/2013 11:57 AM, Dmitry Pavlov wrote:
> Hello,
>
> Suppose I have the following code:
>
> (define conn (sqlite3-connect #:database "mydb.db" ))
>
> (for (((x y) (in-query c "SELECT X, Y FROM DATA")))
>      (displayln x) (displayln y))
>
> in-query returns a sequence that gives two values
> at one call, and the "for" loop binds those values
> to x and y respectively, and everything works fine.
>
> Suppose then that I use an SELECT statement instrumented
> in runtime, and that I do not know beforehand how many
> values am I selecting. How do I get a list or a vector
> of my values per each row?
>
> It seems that I can not bind unknown number of
> values in a "for" loop, can I?
>
> Ideally, I would like a sequence that returns a list
> (or a vector) of column values at each call.
> query-row is close to what I need, but it can be used
> only if I have only one row, so no sequence.
>
> Any hints will be much appreciated.

I think you want 'query-rows' (plural), which produces a list of vectors.

If you want the cursor behavior of 'in-query', you could also try 
wrapping the 'in-query' sequence with 'in-values-sequence'.

Ryan


Posted on the users mailing list.