12

So I am currently trying to make a query in Node.js:

// friends is an array object
db.all('SELECT email ' +
       'FROM users' +
       'WHERE email in ?', friends, function(err, rows) {
           if (!err) {

I know that you can pass in an array of parameters for every '?' symbol, but is it possible to use the IN operator in this case? If not, should I do string concatenation or prepared statements?

3 Answers 3

18
db.all('SELECT email ' +
       'FROM users' +
       'WHERE email in ( ' + friends.map(function(){ return '?' }).join(',') + ' )', 
       friends, 
       function(err, rows) {
           if (!err) {
Sign up to request clarification or add additional context in comments.

Comments

0

// friends is an array object
db.all(`SELECT email FROM users WHERE email in ${ friends.map(() => "?").join(",") }`,
    friends, (err, rows) => {
    if (!err) {}
})

Comments

0

The answers looks more complicated than this recommended SQLite solution:

db.all('SELECT email ' +
       'FROM users' +
       'WHERE email IN (SELECT value FROM json_each(?))', 
       JSON.stringify(friends), 
       function(err, rows) {
           if (!err) {

Actually, the existence of function:

SELECT value FROM json_each(?)

Could be that reason, why SQlite still doesn't support passing array as parameter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.