DallasHoff / sqlocal

SQLocal makes it easy to run SQLite3 in the browser, backed by the origin private file system.
https://sqlocal.dallashoffman.com
MIT License
212 stars 8 forks source link

Unable to parameterize table/column name in query #4

Closed MigFerreira closed 11 months ago

MigFerreira commented 11 months ago

Have the following base query to drop table:

async drop () {
      return DB.sql`DROP TABLE IF EXISTS ${this.tableName}`;
};

which is creating following console error:

Error: sqlite3 result code 1: near "?": syntax error
    SQLite3Error http://127.0.0.1:5173/node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-bundler-friendly.mjs?v=83ec1d7f:5714
    toss3 http://127.0.0.1:5173/node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-bundler-friendly.mjs?v=83ec1d7f:10398
    checkSqlite3Rc http://127.0.0.1:5173/node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-bundler-friendly.mjs?v=83ec1d7f:10417
    checkRc http://127.0.0.1:5173/node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-bundler-friendly.mjs?v=83ec1d7f:10712
    exec http://127.0.0.1:5173/node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-bundler-friendly.mjs?v=83ec1d7f:10842
    execQuery http://127.0.0.1:5173/node_modules/sqlocal/dist/worker.mjs?type=module&worker_file:47
    flushQueue http://127.0.0.1:5173/node_modules/sqlocal/dist/worker.mjs?type=module&worker_file:92
    init http://127.0.0.1:5173/node_modules/sqlocal/dist/worker.mjs?type=module&worker_file:124

checked the worker when db.exec is executed and confirmed the object is:

method: "all"
params: Array [ "Accounts" ]
queryKey: "snTm0m_khR34swnpiudaC"
sql: "DROP TABLE IF EXISTS ?"
type: "query"

Seems to be an issue with binding arguments to query. If I directly replace variable with text the query works fine.

DallasHoff commented 11 months ago

This is a restriction imposed by SQLite. You cannot parametrize a table or column name, per the SQLite documentation.