TAS
TAS

Reputation: 156

Adding a row with multiple columns using Node.js & sqllite3

Using Node.JS with the SQLlite3 module I created a table: 'users' in my database with 2 columns: 'name' and 'surname'.

// Importing database
var fs = require('fs');
var file = 'test.db';
var exists = fs.existsSync(file);
var sqlite3 = require("sqlite3").verbose();
let db = new sqlite3.Database(file)

//inserting first user
db.run('INSERT INTO users VALUES(?, ?)', ['"John", "Doe"'])
db.close();

Instead of adding the value 'John' to 'name' and 'Doe' to surname, it adds the value ' "John", "Doe" ' to name and null to surname.

Upvotes: 3

Views: 2426

Answers (1)

TAS
TAS

Reputation: 156

Parameter of db.run should have been passed as an array when using multiple placeholders instead of a string. Solution:

db.run('INSERT INTO users VALUES(?, ?)', ['John', 'Doe'])
db.close();

Upvotes: 4

Related Questions