Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.9 kB
2
Indexable
Never

/*
*	Query data from the database with sqlite3.
*
*	Example is showing: "Select all courses in the program TIEMM"
*/

const sqlite3 = require('sqlite3').verbose();

function filter(program = "TIEMM", credit_interval = [1, 20], language = 0, location = 'KTH Campus', p1 = 1, p2=1, p3 = 0, p4 = 0){

  //preferences = [program: str = 'TIEMM', credit_interval: list = [1, 20], language: int = [0,1], grading_scale: int = [0, 1]
  //               location: str = 'KTH Campus']

  //preferences = ['TIEMM', [1, 20], 1, 0, 'KTH Campus']

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

  db.all(
     `SELECT General.code, programs.type
             FROM GENERAL
             LEFT JOIN programs
             on GENERAL.id = programs.id
             LEFT JOIN periods
             on GENERAL.id = periods.id
             WHERE programs.program = ? AND periods.p1 = ?`, //AND General.credits >= ? AND General.credits <= ? AND General.language = ? AND periods.p1 = ? AND periods.p2 = ?`,
     [program, p1],//, credit_interval[0], credit_interval[1], language, location,, p2],
     function(err, rows) { console.log(rows)});
  };

filter(program = 'TIMTM', p1 = 0)

/*

  db.run(`SELECT General.code, programs.type
             FROM GENERAL
             LEFT JOIN programs
             on GENERAL.id = programs.id
             LEFT JOIN periods
             on GENERAL.id = periods.id
             

             WHERE programs.program = ? program

             AND General.credits >= ? credit_interval[0]
             AND General.credits <= ? credit_interval[1]
             AND General.language = ? language 
             AND General.location = ? location
             AND periods.p1 = ? p1
             AND periods.p2 = ? p2`, [program, credit_interval[0], credit_interval[1], language, location, p1, p2]);

*/