使用node.js检查SQLite3数据库中是否包含数据

时间:2018-01-26 17:37:02

标签: javascript node.js sqlite discord.js

所以我想用node.js检查我的数据库中是否有某种文本数据

if(db.run("message.author.username IN (participations|name)")) {
    db.run("INSERT INTO participations(name, info) VALUES (?, ?)", [message.author.username, message.content]);
    message.author.send("Teilnahme bestätigt!");
} else {
    message.author.send("Du nimmst schon teil!");
};

if()..部分无效,其他没有if()的部分也无效。 有没有办法正确使用它?

1 个答案:

答案 0 :(得分:0)

数据库调用通常是承诺...这意味着你需要提供一个回调函数,然后在那里检查结果:

db.run("message.author.username IN (participations|name)", (err, rows) => {
    if(rows.length > 0) {
      console.log("we have data!");
      db.run("INSERT INTO participations(name, info) VALUES (?, ?)", [message.author.username, message.content]);
    } else {
      console.log("no data!");
    }
});

此外,它似乎是您的选择"除非有我不知道的SQLite3框架的神奇之处,否则它不是一个有效的SQL。通常,确定行是否存在的SQL格式为:

SELECT * FROM <table.name> WHERE username IN (<your values>)