How to use the "IN" condition, mattn/go-SQLite3

384 views
Skip to first unread message

Stéphane phenetas

unread,
Aug 12, 2015, 4:17:06 PM8/12/15
to golang-nuts
Hello,

I am using this package https://github.com/mattn/go-sqlite3, and I would like to search a table with a "IN" SQL condition. And to populate a Slice with the output.
I am not sure how to do that, but I think the sql condition "IN" might be helpful.

sqlCommand := fmt.Sprintf("SELECT this, that FROM table WHERE theID='1'")
rows, err := db.Query(sqlCommand)

And now I would like to search again in "rows" to populate my Slice, like that :

slice[1][3] = rows."where 'this'=slice[][2]"
slice[2][3] = rows."where 'this'=slice[][2]"
slice[3][3] = rows."where 'this'=slice[][2]"
slice[4][3] = rows."where 'this'=slice[][2]"


etc...

Tell me if it is too confuse/not clear, I will try to re-phrase it.

Thank you

Daniel Theophanes

unread,
Aug 12, 2015, 5:21:55 PM8/12/15
to golang-nuts
You'll need to construct the SQL in a loop, escaping any strings.

Stéphane phenetas

unread,
Aug 12, 2015, 5:33:02 PM8/12/15
to golang-nuts
Thanks for the answer. This is what I currently do. I have a for loop, looping as many time as they are elements in the Slice. And at each iteration, I go check the database and pick up one result.

It is working fine, but my concern is that this is a lot of traffic to and from the database :/

So this is why I wanted to get everything from the database only once, store it locally and then work on the results locally.

Daniel Theophanes

unread,
Aug 12, 2015, 6:54:45 PM8/12/15
to Stéphane phenetas, golang-nuts
Let me clarify:

text := &bytes.Buffer{}
text.WriteString(`select * from T1 where ID in (`
for i, id := range list {
    if i != 0 {
        text.WriteString(`, `)
    }
    text.WriteString(fmt.Sprintf("%d", id)
}
text.WriteString(`);`)
// query( text.String() )

Single query.


--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/J_Ye4Dl653U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stéphane phenetas

unread,
Aug 12, 2015, 7:37:02 PM8/12/15
to golang-nuts, phen...@gmail.com
Thank you. If I understand it correctly, once the query is executed I will get all corresponding rows, in a certain order.

And then, how to use the result. Let say I get 3 rows as a result. And I want to get the value of "column A" in the row where "column B" = something ?

Daniel Theophanes

unread,
Aug 12, 2015, 7:39:03 PM8/12/15
to Stéphane phenetas, golang-nuts
Get the result, call .Next() in a loop. run .Scan(&val).


On Wed, Aug 12, 2015 at 4:37 PM Stéphane phenetas <phen...@gmail.com> wrote:
Thank you. If I understand it correctly, once the query is executed I will get all corresponding rows, in a certain order.

And then, how to use the result. Let say I get 3 rows as a result. And I want to get the value of "column A" in the row where "column B" = something ?

--

Stéphane phenetas

unread,
Aug 12, 2015, 8:58:13 PM8/12/15
to golang-nuts, phen...@gmail.com
Thank you very much Daniel !

The solution was not, afterthought, so complicated, but as a beginner I did not think about it. It works great.
Reply all
Reply to author
Forward
0 new messages