I am pretty new to SQL databases and in particular to the better-SQLite3 database. I have managed to insert, update, and delete data in a way I desire. All of it running in the background of a self-hosted website.
I find that my update function looks a bit "overdone", especially when looking at the array when executing updateDb([[ , ]]) at the end. However, I have not found another way to get this to work. Information on the internet is not that dense, that's why I am asking.
DATABASE
id | query | age
------------------------------
1 | some string | some string
2 | some string | some string
3 | some string | some string
As said, all is working as expected. I am just curious whether there is a more efficient way for the updateDb function. Ideally, I would supply an array of arrays to updateDb if I want to update more than one database entry.
const createDb = () => {
db.prepare(`
CREATE TABLE IF NOT EXISTS wolfram (
id INTEGER PRIMARY KEY AUTOINCREMENT,
query TEXT NOT NULL,
age TEXT NOT NULL
)
`).run();
}
async function updateData() {
'use server'
const update = db.prepare(`UPDATE database SET query = ? WHERE age = ?`);
const updateDb = db.transaction((database) => {
for (const [query, age] of database) {
update.run([query, age])
}
});
updateDb([['nameJK','age1']]);
}