Search code examples
node.jssqlitenode-sqlite3

I can't get sqlite3 insert to work on nodejs


I can't get the following code to work. The SQL statement works when I test it with the sqlite binaries but trying to run it via the nodejs sqlite3 library always result in the following error. Can someone who have used the library before please help me?

[Error: SQLITE_RANGE: column index out of range
Emitted 'error' event on Statement instance at:
] {
  errno: 25,
  code: 'SQLITE_RANGE'
}
db.serialize(() => {
            db.run("CREATE TABLE IF NOT EXISTS account(id INTEGER PRIMARY KEY, firstname TEXT, lastname TEXT, password TEXT, email TEXT UNIQUE)");
            db.run("INSERT INTO account(firstname, lastname, password, email) VALUES(@firstname, @lastname, @password, @email)", {firstname, lastname, password, email});
            response.send('Successfully registered account');
            response.end();
        });

Solution

  • Since you are not passing the primary key in the INSERT clause, you either need to update the primary key to auto-increment, or pass it into the INSERT clause.

    db.run("CREATE TABLE IF NOT EXISTS account(id INTEGER PRIMARY KEY AUTO_INCREMENT, firstname TEXT, lastname TEXT, password TEXT, email TEXT UNIQUE)");