SQL Lite

Oct 4, 2022

SQL Lite

What is SQL Lite?

Small DB that is running with your application on your computer.

Easy to use and can run locally easily.

DB is actually stored locally, In our assignments you might see it in a db file in your repository.

Connect to the Database

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

Query DB

let sql = `SELECT DISTINCT Name name FROM playlists
           ORDER BY name`;

db.all(sql, [], (err, rows) => {
  if (err) {
    throw err;
  }
  rows.forEach((row) => {
    console.log(row.name);
  });
});

Insert DB

  let params = ['Joe','Smith'];
  let sql = "INSERT INTO Users(FirstName, LastName) VALUES($1,$2) returning UserId";

  db.get(sql, params, (err,  data) => {
    console.log(data);
  });