Skip to content
Advertisement

SQLite3 Table Only Holds 1 Row

So, I have 2 tables, that I’ve added per the documentation, but something weird happens whenever I try to add a row to them. The data is written into the .db file, but whenever I query it, it only returns the first row. I can still update, and read these rows normally, but any data I write to them, just doesn’t update the table. It’s only the first row that get’s written.

I’ve looked over all my statements, and I know they’re right, since the first statement adds it to the table, but the second doesn’t. I’m using the base configuration of sqlite3 from npm, per the docs, so I don’t know what I’m doing wrong.

I don’t have any errors, and the expected result, is that I’m able to add as many rows as I can

db.serialize(function() {
 db.run("CREATE TABLE users (userId int, user varchar(255))");
 db.run("CREATE TABLE notes (userId int, uuid varchar(50), name varchar(255), noteData varchar(1024), file BLOB(21845))")
});

db.run("INSERT INTO users (userId, user) VALUES (0, 'User')")
db.run(`INSERT INTO notes (userId, uuid, name, noteData) VALUES (0, 'uuid', 'First Note','This will be readable.')`)

//This statement will add the data to the file, but the query won't read it.
db.run(`INSERT INTO notes (userId, uuid, name, noteData) VALUES (1, 'uuid2', 'First Note','This will not show.')`)
db.get("SELECT * FROM notes",[],(err,row)=>{console.log(row)})

Also, this is not an asynchronous problem. In the example, I added the last line, but it’s not actually in my code. I’m requesting it minutes later, and I can confirm the text is in the database, it just decides not to read it.

Advertisement

Answer

You have to use db.all(). db.get() only returns the first row.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement