简体   繁体   中英

Check if data is included in SQLite3 database with node.js

So i want to check if some kind of text data is in my database with 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!");
};

The if().. part isnt working, everything else without if() is working too. Is there a way to use that properly?

Database calls are generally promises... meaning you need to provide a callback function, then check the results there:

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

Also, it appears your "select" isn't actaully valid SQL, unless there's some magic of the SQLite3 framework that I'm unaware of. Generally, the SQL to determine if rows exist would be in the format of:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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