Skip to content
Permalink
9861415159
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
49 lines (43 sloc) 1.25 KB
const sqlite = require('sqlite-async')
const config = require('../config.json')
module.exports = class Answer {
//CONSTRUCTOR FOR THE DB
constructor(dbName = ':memory:'){
return (async() => {
this.db = await sqlite.open(dbName)
const sql = 'CREATE TABLE IF NOT EXISTS user_answer (\
id INTEGER PRIMARY KEY AUTOINCREMENT,\
answer TEXT\
);'
await this.db.run(sql)
return this
})()
}
//INSERT ANSWERS IN DB
async quiz(answers) {
try{
console.log('ADD answer')
const sql = `INSERT INTO user_answer(answer)\
VALUES("${answers.answer}")`
console.log(sql)
await this.db.run(sql)
} catch(err){
console.log(err.message)
throw err
}
}
//SELECT ALL ANSWERS IN DB
async all() {
try{
console.log('ALL answers')
const sql = 'SELECT id, answer FROM user_answer'
console.log(sql)
const answert = await this.db.all(sql)
console.log(answert)
return answert
} catch(err){
console.log(err.message)
throw err
}
}
}