-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
54 lines (48 loc) · 1.35 KB
/
database.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const sqlite3 = require('sqlite3').verbose();
class Database {
constructor(db_file_path) {
this.db = new sqlite3.Database(db_file_path, (err) => {
if (err) {
console.log('Error when creating the database', err)
} else {
console.log('Database created!')
}
})
}
createTable = () => {
this.db.run("CREATE TABLE IF NOT EXISTS user(id INTEGER PRIMARY KEY, first_name TEXT, last_name TEXT, date_of_birth TEXT, place_of_birth TEXT, code_postal TEXT, city TEXT, address TEXT)", (err) => {if (err){
console.log("Failed to create table");
}});
}
getUser = (user_id, cb) => {
return this.db.get("SELECT * FROM user WHERE id=?", user_id, function(err, row) {
if (err){
console.log('User not found')
return null
}
cb(row)
return row
});
}
insertUser = (user_id, callback) => {
this.db.run('INSERT INTO user (id) VALUES (?)', user_id, (err) => {
if (err) {
console.log("User already exist");
callback()
}
else
{
callback()
}
});
}
updateUser = (user_id, value, field_name, callback) => {
const sql = `UPDATE user SET ${field_name} = ? WHERE id = ?`;
this.db.run(sql, [value, user_id], (err) => {
if (err) {
console.log("Update Failed");
}
});
}
}
module.exports=Database