简体   繁体   English

如何从nodejs中的SQLite object中提取查询字符串?

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

How to extract the query string from a SQLite object in nodejs ?如何从 nodejs 中的SQLite 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();

es6 string replacement is supporet by node.js. node.js 支持 es6 字符串替换。

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但是你应该准备你的陈述来保证安全,比如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根据 ydo 是否希望使用日志中的字符串,为了测试,您必须在上面的字符串中添加单引号,这是查询自动生成的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM