Binding ORDER BY #1132
-
Currently this syntax doesn't work and doesn't cause any error. const selectAllImageQueryAsc = db.prepare(
"SELECT * From image ORDER BY @sortKey ASC;",
);
const result = selectAllImageQueryAsc.all({ sortKey: sort?.key || "id" });
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
SQLite only allows binding parameters for data values. A table/column name is not a data value, so it doesn't work the way you expect. This isn't an issue with The number of tables/columns in your database is theoretically static (i.e., it doesn't change rapidly while an application is running; it only changes at intentional, predictable moments). Therefore, the optimal solution is to create one prepared statement per column that you might want to order by, and then choose the correct prepared statement dynamically (without running Alternatively, if you want to just run |
Beta Was this translation helpful? Give feedback.
SQLite only allows binding parameters for data values. A table/column name is not a data value, so it doesn't work the way you expect. This isn't an issue with
better-sqlite3
, it's just how SQLite is designed.The number of tables/columns in your database is theoretically static (i.e., it doesn't change rapidly while an application is running; it only changes at intentional, predictable moments). Therefore, the optimal solution is to create one prepared statement per column that you might want to order by, and then choose the correct prepared statement dynamically (without running
db.prepare()
for each query).Alternatively, if you want to just run
db.prepare()
for each query, there's no …