Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

sqlite-simple

dengrc15MIT1.2.11

常用sql封装成方法,只关注数据

sqlite3,sqlite-simple

readme

sqlite-simple

database

  • pushTable([...options])
  • ready()
  • transaction(callback[, serialize])

table

  • insert(data[, isResult])
  • delete(data)
  • update(data[, isResult])
  • select(fields='*')
  • put(data[, fields][, dataHandler])
  • bulkInsert(array[, isResult])
  • bulkDelete(array)
  • bulkUpdate(array[, isResult])
  • bulkPut(array[, fields][, dataHandler])
  • get(where[, fields])
  • each(where[, fields])

创建数据库、表

注意: 如果需要在 insert、put 操作后获取返回值添加 uniqueKey

const DataBase = require('sqlite-simple');

const db=new DataBase("./a.db",function(db){
    db.pushTable({
        tableName: 'classify',
        fields: {
            "id": ["integer", "autoincrement"],
            "name": ["text", "NOT NULL"]
        },
        primaryKey: ["id"]
    },{
        tableName:'list',
        fields: {
            "id": ["integer", "autoincrement"],
            "title": ["text", "NOT NULL"],
            "content":["text", "NOT NULL"]//, "UNIQUE" ,"DEFAULT(...)"
        },
        primaryKey: ["id"],
        //如果需要在 insert、put 操作后获取返回值添加这个配置
        uniqueKey: ["title"]
        /* CREATE TABLE IF NOT EXISTS ${tableName}(
         * for(field in fields)
         * [${field}] ${fields[field].join(" ")}
         * 
         * PRIMARY KEY(${primaryKey})
         * )
         */
    })
});

bulkInsert

注意: 数据库是异步创建,如果要立即操作数据, 加 ready

db.ready().then(function(db){
    db.list.bulkInsert([
        {
            title:"标题1",
            content:"内容1"
        },
        {
            title:"标题2",
            content:"内容2"
        }
    ]).then(function(e){
        console.log(e)
    }).catch(function(e){
        console.error(e)
    })
})

select

注意: 对应 sqlite3 中的 db.all

//所有字段
db.list.select().then(function(result){
    console.log(result)
}).catch(function(e){
    console.error(e)
})
//部分字段
db.list.select(["code","name"]).then(function(result){
    console.log(result)
}).catch(function(e){
    console.error(e)
})
//部分字段
db.list.select("code,name").then(function(result){
    console.log(result)
}).catch(function(e){
    console.error(e)
})