简体   繁体   English

如何从 Javascript 中的 SQLite 数据库中获取表数据?

[英]How do i get table data from SQLite Database in Javascript?

I am working on a money system in a discord bot, i need to store data in a sqlite database, but i can't seem to read the data, only write.我正在使用 discord 机器人中的货币系统,我需要将数据存储在 sqlite 数据库中,但我似乎无法读取数据,只能写入。

Code:代码:

const sqlite3 = require("sqlite3");
const moneyDB = new sqlite3.Database("./databases/money.db");

...

if(cmd == "money"){
    const person = msg.mentions.users.first()
    if(!person) return;
    var info = moneyDB.exec(`SELECT * FROM money WHERE userID = "${person.id}"`);
    console.log(info);
};

I also tried using these: moneyDB.run moneyDB.get moneyDB.all moneyDB.each我也尝试使用这些: moneyDB.run moneyDB.get moneyDB.all moneyDB.each

They all output:他们都是 output:

Database { open: true, filename: './databases/money.db', mode: 65542 }

Why is this the output and not the actual database data?为什么这是 output 而不是实际的数据库数据? Is there a more efficient way to do this?有没有更有效的方法来做到这一点? I've already made a different command for adding and updating data on the database, but can't seem to read anything.我已经为在数据库中添加和更新数据制作了不同的命令,但似乎无法读取任何内容。

these functions of the sqlite3 module return the database for chaining. sqlite3 模块的这些函数返回数据库以进行链接。

you need to pass a function as second argument into db.all您需要将 function 作为第二个参数传递给 db.all

const sqlite3 = require("sqlite3");
const moneyDB = new sqlite3.Database("./databases/money.db");

...
if (cmd == "money") {
    const person = msg.mentions.users.first()
    if(!person) return;
    moneyDB.all(`SELECT * FROM money WHERE userID = "${person.id}"`, (err, info) =>{
            console.log(err, info)
    });
};

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

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