var sqlite3 = require('sqlite3').verbose();
let dbName = 'my.sqlite';
var db = new sqlite3.Database(dbName);

db.serialize(() => {
let sql = `CREATE TABLE IF NOT EXISTS articles
(id integer primary key, title, content TEXT)
`;
db.run(sql);
});


class Article {

constructor(){
console.log('------------begin----------');
}

static all(cb){
db.all(`SELECT * FROM articles`, cb);
}

static find(id, cb){
db.get(`SELECT * FROM articles WHERE id = ? `, id, cb);
}

static create(data, cb) {
const sql = `INSERT INTO articles (title, content) VALUES (?, ?) `;
db.run(sql, data.title, data.content, cb);
}

static delete(id, cb){
if (!id) return cb(new Error('请输入id'));
db.run(`DELETE FROM articles WHERE id = ?`, id, cb);
}

}


let data = {
title: 'Nodejs 实战进阶dasdas',
content: 'nodejs dassdasdsa....'
}

/**
* [创建数据]
* @param {[type]} data [description]
* @param {[type]} (err, data [description]
* @return {[type]} [description]
*/
Article.create(data, (err, data) => {
if(!err) console.log(data);
});

/**
* [查询所有数据]
* @param {[type]} (err,data [description]
* @return {[type]} [description]
*/
Article.all((err,data) => {
console.log(data);
});

/**
* [根据条件查询数据]
* @param {[type]} 3 [description]
* @param {[type]} (err,data [description]
* @return {[type]} [description]
*/
Article.find(3, (err,data) => {
if(!err) console.log(data);
});

/**
* [删除符合条件的数据]
* @param {[type]} 2 [description]
* @param {[type]} (err,data [description]
* @return {[type]} [description]
*/
Article.delete(2, (err,data) => {
if(!err) console.log('del success');
});