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

From: Dmitry Pavlov (dpavlov at ipa.nw.ru)
Date: Tue Feb 5 12:27:33 EST 2013

Thanks Pierpaolo, I did not know about that useful trick with
call-with-values, missed it sometimes.

Ryan, thanks! in-values-sequence did the trick perfectly.

Regards,

Dmitry



On 02/05/2013 09:17 PM, Ryan Culpepper wrote:
> 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
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>
>


Posted on the users mailing list.