【问题标题】:Check if data is included in SQLite3 database with node.js使用 node.js 检查数据是否包含在 SQLite3 数据库中
【发布时间】:2018-07-06 02:32:33
【问题描述】:

所以我想用 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() 的其他所有部分也起作用。 有没有办法正确使用它?

【问题讨论】:

    标签: javascript node.js sqlite discord.js


    【解决方案1】:

    数据库调用一般都是promise...意思是你需要提供一个回调函数,然后在那里查看结果:

    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!");
        }
    });
    

    此外,您的“选择”似乎不是真正有效的 SQL,除非我不知道 SQLite3 框架有什么魔力。通常,判断行是否存在的 SQL 格式为:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多