-1

How to extract the query string from a SQLite object in nodejs?

For example if we have this:

import sqlite3 from 'sqlite3';
import { open } from 'sqlite';

const db = await open({
  filename: 'test.db',
  driver: sqlite3.Database
});

var ret = await db.get('SELECT * FROM `test` WHERE id=? AND foo=?', [100, 'bar']);

How can we achieve this:

SELECT * FROM `test` WHERE id="100" AND foo="bar"

I expect something like this to happen:

db.getQueryString();
5
  • ret has the query result, so what exactly is your problem? are you searching for this stackoverflow.com/questions/37776205/… Commented May 1, 2022 at 16:08
  • @nbk as I said, I need to the query built by sqlite as string. Commented May 1, 2022 at 17:11
  • that is simplestring replacement and the query does that automaticall, so i don't understand what do want with a string or does it come somehow from the datavase. this is very unclear Commented May 1, 2022 at 17:22
  • 1
    The C API has a way to do it but I suspect the node interface doesn't export a fraction of the functionality of the C version. Commented May 2, 2022 at 5:21
  • @nbk Of course, placement is not simple, otherwise we would have simply used foo="bar" instead of "foo=?", ["bar"]. sqlite has an internal control mechanism to escape some characters to make the query secure, I need the same built-in query to insert into the log files when it fails. Commented May 2, 2022 at 9:07

1 Answer 1

-1

es6 string replacement is supporet by nodesjs.

var my_id = 100
var my_var = 'bar';
var s = `SELECT * FROM `test` WHERE id="${my_id}" AND foo="${my_var}" `;
console.log(s);

But you should prepare your statements to make the secure like https://stackoverflow.com/a/49328621/5193536

Depending if ydo want the strings in the log to be used, for testing, you must add single quotes to the sting above, which the query makes automatically

Sign up to request clarification or add additional context in comments.

2 Comments

This method does not guarantee that it will not have problems with special characters and does not cover security.
that is why you use the prepared statement and not a sting replacement, but you stated you want to write in a log file, so it is the only way to do it

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.