mysql - How do I do IN queries in Go? -


i'd run query in go using "database/sql" package:

select id, name users id in (1,2,3,4); 

how use variable substitution in query command? e.g., want (which of course doesn't work):

db.query("select id, name users id in (?)", []int{1,2,3,4}) 

@volker suggests in comments database drivers may able expand slice you. driver can handle slice elements individually, however:

db.query("select id, name users id in (?, ?, ?, ?)", 1, 2, 3, 4) 

of course, you'd need know how many elements going in slice when write that, isn't case. generating correct number of question marks simple enough , gives general solution:

ints := []interface{}{1,2,3,4} marks := strings.repeat("?,", len(ints) - 1) + "?" db.query("select id, name users id in (" + marks + ")", ints...) 

if slice might empty, you'll have handle somehow.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Socket.connect doesn't throw exception in Android -

iphone - How do I keep MDScrollView from truncating my row headers and making my cells look bad? -