So I an integrating my Discord.JS
bot with SQLite3
and I am adding a function where users can store their ID and username within a table. Currently, what I have will only create a new row in the table, if the ID and name of the user does not already exist within a row of the table, and creates a new row if the data doesn't exist. What I want to know is if there is a way to throw something out of the query if the row does exist, and for me to be able to catch it, and then do something else.
I have tried if..else
statements, but I want to know if there is a simpler way to achieve this.
This is what I have currently, that functions as described above.
let userC = message.mentions.members.first()
db.serialize(() => {
db.run('CREATE TABLE IF NOT EXISTS user (id TEXT, name TEXT)');
db.run(`INSERT INTO user (id, name) SELECT '${userC.id}', '${userC.user.username}' WHERE NOT EXISTS(SELECT 1 FROM user WHERE id = '${userC.id}' AND name = '${userC.user.username}')`)
});
message.reply('Added the user to the database.');
Ideally, if the row does exist, message.reply('Added the user to the database.');
will not execute, and instead will continue with message.reply('That user already exists within the database');
But if the row doesn't exist, it inserts the row and data, and only continues with message.reply('Added the user to the database.');