how do I insert a Js array in sqlite database in node

Viewed 426

I am new to this field and just started my first project and I am using sqlite3 as a database

// can insert this array as a row
var myArray = ["bob","16","student","Bosh University","football",]

this is my code... i have created the table already

let infoTable = new sqlite3.Database('./all/infoTable.db', (err) => {
  if (err) console.error(err.message)
});

infoTable.run(`CREATE TABLE IF NOT EXISTS infoTable (name TEXT, age INTEGER, status TEXT, workPlace TEXT , intrest TEXT)`)

can I insert this array as a row into the dataBase???

1 Answers

You could try something like that:

var myArray = ["bob","16","student","Bosh University","football"];
infoTable.run('\
INSERT INTO \
  infoTable(name TEXT, age INTEGER, status TEXT, workPlace TEXT , intrest TEXT) \
VALUES \
  (?, ?, ?, ?, ?)', ...myArray);

They have a good API Reference.

Related