1. 程式人生 > >node sqlite 基本操作

node sqlite 基本操作

   var db;
    function connectDb(dbPath, callback) {
        db = new sqlite.Database(dbPath, function(err) {
            if (typeof callback == "function")
                callback(err);
        })
    }
    //example:sql = "insert into student (name, age, sex) values (?, ?, ?)",obj = ["name", age, "sex"]
    function addToDb(sql, obj, callback) {
        db.run(sql, obj, function(err) {
            callback(err);
        })
    }
    // example:sql="delete from student where name = ?",opt=["id"](where後的=和==沒有區別)
    function delFromDb(sql, opt, callback) {
        db.run(sql, opt, function(err) {
            callback(err);
        })
    }

    //example:sql = update student set name = ? where name = ?, obj = ["newname"],
    function updateDb(sql, obj, callback) {
        db.run(sql, obj, function(err) {
            callback(err);
        })
    }

    function selectSingalFromDb(sql, opt, callback) { //查詢第一條資訊
        db.get(sql, opt, function(err, obj) {
            callback(err, obj);
        })
    }