Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 899
TAS Avatar asked May 07 '26 10:05

TAS


1 Answers

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();
like image 136
TAS Avatar answered May 08 '26 23:05

TAS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!