Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

285
Views
NodeJS Sqlite WHERE IN Query Stament with dynamic Parameter (?)

I use dynamic parameters for my SQLITE statments in node. For example SELECT * FROM table WHERE table.id = ?. This all works quite well. But with a WHERE IN query I always get no results. Which indicates that it is misinterpreting the dynamic parameter. Here is my code:

    getModelsByBrandId: (ids_array) => {
        const ids = ids_array.toString();
        const sql = "SELECT * FROM models WHERE brand_id IN (?)";
        const  params = [ids];

        return new Promise((resolve, reject) => {    
            db.all(sql, params, async (err, rows) => {      
                if (err) {
                  reject(err);
                } else {
                    resolve(rows)
                }
            });
        });                
    }, 

I had already tried passing the array but also a string (array.toString()). Unfortunately, neither of these returned any results.

Question: What am I doing wrong and what do I have to do to make the WHERE IN query work?

Thanks in advance! Max

almost 4 years ago · Santiago Trujillo
2 answers
Answer question

0

getModelsByBrandId: (ids_array) => {
    const placeholders = ids_array.map(() => '?').join(',');
    const params = ids_array;
    const sql = "SELECT * " 
              + "FROM models "
              + "WHERE brand_id IN (" + placeholders + ")";

    return new Promise((resolve, reject) => {    
        db.all(sql, params, async (err, rows) => {      
            if (err) {
              reject(err);
            } else {
              resolve(rows)
            }
        });
    });                
}, 

Should send a query like

getModelsByBrandId([1, 2, 3]);
// sql = "SELECT * FROM models WHERE brand_id IN (?,?,?)"
// params = [1, 2, 3]

** EDIT **

I needed somewhat the same feature, where I was dealing with raw queries. So, instead of writing everything, and as a personal exercise, I wrote a template function that handled SQL parameters like these.

import { sql } from "./sql.template";

const brand_ids = [1, 2, 3];

const [ query, params ] = sql`
  SELECT *
  FROM models
  WHERE brand_id IN (${brand_ids})
`;

console.log({ query, params });
// { query:"SELECT * FROM models WHERE brand_id IN (?,?,?)", params:[1, 2, 3] }

Source available here.

almost 4 years ago · Santiago Trujillo Report

0

What you should not do!

Is to write the statement without "dynamic parameter" like:

const sql = "SELECT * FROM models WHERE brand_id IN (" + ids_array.toString() + ")";
const  params = [];

Why? See the comments below...

almost 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!