更新记录

1.0.2(2023-11-14)

新增多条上传

1.0.1(2022-08-15)

新增示例项目与插件使用说明

1.0.0(2022-08-11)

初始化

查看更多

平台兼容性

Vue2 Vue3
App 快应用 微信小程序 支付宝小程序 百度小程序 字节小程序 QQ小程序
app-vue app-nvue × × × × × ×
钉钉小程序 快手小程序 飞书小程序 京东小程序
× × × ×
H5-Safari Android Browser 微信浏览器(Android) QQ浏览器(Android) Chrome IE Edge Firefox PC-Safari

适用场景

  • 需要存储大量数据在前端的时候,使用Storage,性能很差,并且有很多限制
  • 在app端使用SQLite的时候,由于没有数据库管理工具,调试困难
  • 编写sql语句不符合前端开发者的开发习惯

平台兼容性

本插件只适用于web端和app端,在web端基于webSQL,app端基于SQLite
webSQL和SQLite都是关系型数据库,都使用sql语句,所以可以兼容
webSQL更多是方便调试使用(浏览器相当于数据库管理工具),所以每个数据库限制了2M
Chrome和Edge 119版本起webSQL弃用

使用步骤

  1. 下载本插件并存放到项目目录下,比如js_sdk/ljc-database,使用uni_modules导入目录为uni_modules/ljc-database/js_sdk
  2. 在页面中引入这个js模块
    import ljcDB from '@/js_sdk/ljc-database/database'
    import ljcDB from '@/uni_modules/ljc-database/js_sdk'
  3. 调用init方法
  4. 调用其他方法

api

该模块的方法如下:

方法1:Promise ljcDB.init(String dbName, Array[Array] tableStructure)

初始化数据库

参数说明
参数名 类型 必填 说明
dbName String 数据库名称
tableStructure Array[Array] 二维数组。外层数组每一项代表一个表;内层数组,第一个元素为表名,第二个元素为表结构
示例
let table1 = ["single", {
    msgId: "varchar(20)",
    msgText: "text",
    msgType: "int(11)",
    sendDate: "bigint(20)"
}]
let table2 = ["imGroup", { // 注意不要用sql关键字哦,例如group
    msgId: "varchar(20)",
    msgText: "text",
    msgType: "int(11)",
    sendDate: "bigint(20)"
}]
ljcDB.init('im', [table1, table2]).then(res => {
    console.log('初始化成功')
}).catch(err => {
    console.log('初始化失败,请检查参数')
})

方法2:Promise ljcDB.insert(String dbName, String tableName, Array[Object] arr)

参数说明
参数名 类型 必填 说明
dbName String 数据库名称
tableName String 表名
arr Object/Array[Object] 插入数据库表的数据,只插入一条可使用对象,插入多条,使用对象数组
示例
// 注意数据类型,对象中不要存在表结构中没有的字段
ljcDB.insert('im', 'single', { // 插入一条
    msgId: Date.now() + '',
    msgText: `我是第${this.dbData.length}条数据`,
    msgType: 1,
    sendDate: Date.now(),
}).then((res) => {
    console.log('新增成功')
})

ljcDB.insert('im', 'single', [{ // 插入多条
    msgId: Date.now() + '',
    msgText: `我是第${this.dbData.length}条数据`,
    msgType: 1,
    sendDate: Date.now(),
}]).then((res) => {
    console.log('新增成功')
})

方法3:Promise ljcDB.delete(String dbName, Object obj)

参数说明
参数名 类型 必填 说明
dbName String 数据库名称
obj.from String 表名
obj.where Array 见最下面的where说明
示例
ljcDB.delete('im', { // 删除整个表
    from: 'single'
}).then(res => {
    console.log('删除成功')
})

ljcDB.delete('im', { // 删除一条数据
    from: 'single',
    where: [{
        msgId: `= "${item.msgId}"`
    }]
}).then(res => {
    console.log('删除成功')
})

方法4:Promise ljcDB.update(String dbName, Object obj)

参数说明
参数名 类型 必填 说明
dbName String 数据库名称
obj.from String 表名
obj.set Object 改变数据库表的一条数据,注意数据类型,对象中不要存在表结构中没有的字段
obj.where Array 见最下面的where说明
示例
ljcDB.update('im', {
    from: 'single',
    set: {
        ...item,
        msgText: item.msgText + '1'
    },
    where: [{
        msgId: `= "${item.msgId}"`,
    }]
}).then(res => {
    console.log('修改成功')
})

方法5:Promise ljcDB.select(String dbName, Object obj, Object pageParams)

传入第四个参数,相当于启用分页查询

参数说明
参数名 类型 必填 说明
dbName String 数据库名称
obj.select Array 或 其他 查询的内容,如果不是数组,或者数组为空,则查询*
obj.from String 表名
obj.where Array 见最下面的where说明
obj.order Object 元素的键为需要排序的字段,可选值:"desc"或其他("asc")
obj.limit Number 查询条数,如果传入了第四个参数,将被忽略
obj.offset Number 偏移值,如果传入了第四个参数,将被忽略
pageParams.size Number 查询条数
pageParams.current Number 查询页数
pageParams.offset Number 查询偏移值
示例
ljcDB.select('im', { // 普通查询
    select: ['*'], // 不推荐使用*查询,这样性能不佳
    from: 'single',
    where: [],
    order: {
        sendDate: 'desc',
    },
    limit: 999,
    offset: 0
}).then(res => {
    console.log(res)
})

ljcDB.select('im', { // 分页查询
    select: ['msgId', 'msgText', 'msgType'], // 推荐这种写法
    from: 'single',
    where: [],
    order: {
        sendDate: 'desc',
    }
}, {
    size: 10,
    current: 1,
    offset: 0
}).then(res => {
    console.log(res.data)
    console.log(res.total)
})

where说明

数组中的每一项互为or关系(并集),每一项的元素,为and关系(交集)
where 符号和值之间请留一个空格, 并且如果值是字符串,请自行拼接双引号

示例
where: [{ // 这里存粹是为了演示写法,于业务无关
    msgId: `= "${item.msgId}"`,
    msgType: `<> ${1}`,
}, {
    msgId: `not "${item.msgId}"`,
    msgText: `like '_是%'`,
    msgType: `is not null`,
    sendDate: `id in(1,3,8)`
}]

隐私、权限声明

1. 本插件需要申请的系统权限列表:

SQLite(数据库)

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT协议

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问