node-sqlite3 icon indicating copy to clipboard operation
node-sqlite3 copied to clipboard

Add ability for sqlite to handle array parameters

Open pleerock opened this issue 8 years ago • 5 comments

I want IN operator to work correctly with array params, for example:

SELECT * FROM table WHERE table.id IN ($1)

I would like $1 to be:

$1 = [1, 2, 3, 4] or $1 = ['firstId', 'secondId', 'thridId', '...']

So, it will generate query like SELECT * FROM table WHERE table.id IN (1, 2, 3, 4) where all id from the array are escaped.

Mysql driver handles such parameters very nicely.

pleerock avatar Jan 12 '17 06:01 pleerock

also in #527 and #628 and #373 and #777

kroggen avatar Jan 12 '17 07:01 kroggen

good

xxi-arch avatar Apr 25 '18 10:04 xxi-arch

As a workaround you could do the following:

const ids = [1, 2, 3, 4];
const placeholders = ids.map(() => "?").join(",");
db.run(`SELECT * FROM table WHERE table.id IN (${placeholders})`, ids);

bernie43 avatar Sep 07 '20 22:09 bernie43

As a workaround you could do the following:

const ids = [1, 2, 3, 4];
const placeholders = ids.map(() => "?").join(",");
db.run(`SELECT * FROM table WHERE table.id IN (${placeholders})`, ids);

The issue we ran into with that was:

SQLITE_ERROR: too many SQL variables

or something along those lines.

https://github.com/typeorm/typeorm/issues/190

imnotjames avatar Jul 08 '21 04:07 imnotjames

The issue we ran into with that was:

SQLITE_ERROR: too many SQL variables

or something along those lines.

typeorm/typeorm#190

Try with :

const ids = [1, 2, 3, 4];
const placeholders = ids.map(() => "?").join(",");
db.run(`SELECT * FROM table WHERE table.id IN (${placeholders})`, ...ids);

jorisboffard avatar Oct 03 '24 07:10 jorisboffard