· 6 years ago · Mar 17, 2019, 10:30 AM
1const db = new sqlite3.Database("database.db", err => {
2 if (err) {
3 console.log(`can;t connected tp database, status: ${err}`);
4 } else {
5 console.log(`gotcha, succesfully connect to the database`);
6 }
7});
8
9class DATABASE {
10 static run(sql, params = [], callback) {
11 db.run(sql, params, function(err) {
12 if (err) {
13 throw err;
14 console.log(`something wrong with this ${sql}`);
15 console.log(err);
16 callback(err);
17 } else {
18 callback({this.lastID}) // this is could be null also
19 console.log("sucesfully fo this ", sql);
20 }
21 });
22 }
23}
24
25module.exports = DATABASE;
26
27const DATABASE = require("./setup.js");
28const sqlite3 = require("sqlite3").verbose();
29const db = new sqlite3.Database("database.db", err => {
30 if (err) {
31 console.log(`can;t connected tp database, status: ${err}`);
32 } else {
33 console.log(`gotcha, succesfully connect to the database`);
34 }
35});
36
37db.serialize(() => {
38 const departement = `CREATE TABLE IF NOT EXISTS departement(
39 id INTEGER PRIMARY KEY AUTOINCREMENT,
40 name TEXT NOT NULL,
41 city TEXT
42 )`;
43
44 const employee = `CREATE TABLE IF NOT EXISTS employee(
45 id INTEGER PRIMARY KEY AUTOINCREMENT,
46 firstName TEXT NOT NULL,
47 lastName TEXT,
48 gender TEXT NOT NULL CHECK (gender IN ('Male', 'Female')),
49 email TEXT NOT NULL UNIQUE ,
50 phone TEXT,
51 dept_id INTEGER,
52 FOREIGN KEY (dept_id) REFERENCES departement(id)
53 )`;
54 DATABASE.run(departement);
55 DATABASE.run(employee);
56
57 // , err => {
58 // if (err) throw new Erro(`bugs here`);
59 // else console.log("sucess.....");
60 // });
61});