简介(下载地址)

Ba-Sqlite 是一款原生sqlite数据库管理插件。支持创建表、删除表;增、删、改、查;事务控制;分页查询;批量删、改、查等等。

  • 支持多个数据库切换
  • 支持创建表、删除表
  • 支持增、删、改、查
  • 支持事务
  • 支持批量修改
  • 支持分页查询
  • 支持条件排序
  • 支持自定义条件,批量删、改、查
  • 支持查询当前表结构
  • 支持执行自定义sql
  • 支持返回数据总数(列表)
  • 支持自定义数据库路径

效果展示

uniapp SharePreferences 数据存储安全 uniapp本地数据库_数据

使用方法

引入组件

script 中引入组件

const sqlite = uni.requireNativePlugin('Ba-Sqlite')

调用示例

script 中调用

data() {
			return {
				dbName: 'ba-db.db',
				tableName: 'user',
				columnNames: "[{name: '_id',type: 'int',isId: true,desc:'主键id'}, {name: 'name',type: 'text',desc:'姓名',notNull:true}, {name: 'sex',type: 'int',desc:'性别1:男 2:女 0:未知'}, {name: 'hobby',type: 'text',desc:'爱好'}]",
				values: "[{_id: 1,name: '张三',sex: '1'}, {_id: 2,name: '李四',sex: '2'}]",
				deleteIdKey: '_id',
				deleteId: '1',
				updateQueryKey: 'sex = ?',
				updateQueryValue: "['2']",
				updateContent: "{hobby: '逛街'}",
				pageNum: 1,
				pageSize: 10,
				orderKey: "sex",
				orderType: "desc",
				dataResult: ""
			}
		},
		methods: {
			openOrCreate() { //打开或创建数据库(也可以用来切换多个数据库)
				sqlite.openOrCreate({
						'dbName': this.dbName,
						//'dbPath': '/storage/emulated/0',//自定义数据库路径
					},
					(res) => {
						console.log(res);
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 3000
						})
					});
			},
			isHasDb() { //查询某个数据库是否存在
				sqlite.isHasDb({
						'dbName': this.dbName,
					},
					(res) => {
						console.log(res);
						let msg = res.msg;
						if (res.ok) {
							msg = res.isHasDb ? "存在" : "不存在";
						}
						uni.showToast({
							title: msg,
							icon: "none",
							duration: 3000
						})
					});
			},
			createTable() { //创建表
				sqlite.createTable({
						'tableName': this.tableName,
						'columnNames': this.columnNames,
					},
					(res) => {
						console.log(res);
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 3000
						})
					});
			},
			deleteTable() { //删除表
				sqlite.deleteTable({
						'tableName': this.tableName,
					},
					(res) => {
						console.log(res);
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 3000
						})
					});
			},
			isHasTable() { //查询某个表是否存在
				sqlite.isHasTable({
						'tableName': this.tableName,
					},
					(res) => {
						console.log(res);
						let msg = res.msg;
						if (res.ok) {
							msg = res.isHasTable ? "存在" : "不存在";
						}
						uni.showToast({
							title: msg,
							icon: "none",
							duration: 3000
						})
					});
			},
			getTables() { //获取数据库所有表
				sqlite.getTables({},
					(res) => {
						console.log(res);

						this.dataResult = res.ok ? JSON.stringify(res) : '';

						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 3000
						})
					});
			},
			insert() { //插入数据
				sqlite.insert({
						'tableName': this.tableName,
						'values': this.values,
					},
					(res) => {
						console.log(res);
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 3000
						})
					});
			},
			replace() { //更新数据
				sqlite.replace({
						'tableName': this.tableName,
						'values': this.values,
					},
					(res) => {
						console.log(res);
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 3000
						})
					});
			},
			update() { //批量修改数据
				sqlite.update({
						'tableName': this.tableName,
						'tableName': this.tableName,
						'selection': this.updateQueryKey,
						'selectionArgs': this.updateQueryValue,
						'values': this.updateContent,
					},
					(res) => {
						console.log(res);
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 3000
						})
					});
			},
			deleteData() { //删除数据
				sqlite.delete({
						'tableName': this.tableName,
						'idKey': this.deleteIdKey,
						'idValue': this.deleteId,
					},
					(res) => {
						console.log(res);
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 3000
						})
					});
			},
			clearData() { //清空数据
				sqlite.clear({
						'tableName': this.tableName,
					},
					(res) => {
						console.log(res);
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 3000
						})
					});
			},
			query() { //查询数据
				let orderBy;
				if (this.orderKey && this.orderType) {
					orderBy = this.orderKey + " " + this.orderType;
				}
				sqlite.query({
						'tableName': this.tableName,
						'orderBy': orderBy,
					},
					(res) => {
						console.log(res);
						this.dataResult = res.ok ? JSON.stringify(res) : '';

						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 3000
						})
					});
			},
			queryPage() { //分页查询
				let orderBy;
				if (this.orderKey && this.orderType) {
					orderBy = this.orderKey + " " + this.orderType;
				}
				sqlite.queryPage({
						'tableName': this.tableName,
						'pageNum': this.pageNum,
						'pageSize': this.pageSize,
						'orderBy': orderBy,
					},
					(res) => {
						console.log(res);

						this.dataResult = res.ok ? JSON.stringify(res) : '';

						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 3000
						})
					});
			},
			closeDb() { //关闭数据库
				sqlite.closeDb({},
					(res) => {
						console.log(res);
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 3000
						})
					});
			},
		}

方法清单

名称

说明

openOrCreate

打开或创建数据库(也可以用来切换多个数据库)

isHasDb

查询某个数据库是否存在

createTable

创建表

deleteTable

删除表

isHasTable

查询某个表是否存在

getTables

获取数据库所有表

insert

插入数据

replace

更新数据

update

批量修改数据

delete

删除数据

clear

清空数据

query

查询数据

queryPage

分页查询

execSQL

执行sql语句(无返回)

rawQuery

自定义sql语句查询

openOrCreate 方法参数

打开或创建数据库(也可以用来切换多个数据库)

属性名

类型

默认值

说明

dbName

String

‘ba-db.db’

数据库名称

isHasDb 方法参数

查询某个数据库是否存在

属性名

类型

默认值

说明

dbName

String

‘ba-db.db’

数据库名称

dbPath

String

默认

自定义数据库路径,不传为默认路径(传值为应用可访问的手机本地路径)

返回:
{"isHasDb":false,"msg":"success","ok":true}

createTable 方法参数

创建表

属性名

类型

默认值

说明

tableName

String

‘’

表名

columnNames

Array

[]

表结构(参考如下)

表结构 columnNames

属性名

类型

默认值

说明

name

String

‘’

字段名称

type

String

‘’

字段类型(text、int)

isId

Boolean

false

是否是主键(默认主键’_id’)

isAuto

Boolean

false

是否自增(仅主键)

notNull

Boolean

false

是否不能为空

deleteTable 方法参数

删除表

属性名

类型

默认值

说明

tableName

String

‘’

表名

isHasTable 方法参数

查询某个表是否存在

属性名

类型

默认值

说明

tableName

String

‘’

表名

返回:
{"isHasTable":false,"msg":"success","ok":true}

insert 方法参数

插入数据

属性名

类型

默认值

说明

tableName

String

‘’

表名

values

Array

‘’

需要插入的数据(json),如 [{_id: 1,name: ‘张三’,sex: ‘1’}]

replace 方法参数

更新数据

属性名

类型

默认值

说明

tableName

String

‘’

表名

values

Array

‘’

需要更新的数据(json),如 [{_id: 1,name: ‘张三’,sex: ‘1’}]

update 方法参数

根据条件批量修改数据

属性名

类型

默认值

说明

tableName

String

‘’

表名

selection

String

‘’

查询的字段,如 ‘sex = ?’

selectionArgs

String[]

[]

查询的字段的值,如 [‘1’]

values

Object

{}

批量更改的数据,如 {hobby: ‘逛街’}

delete 方法参数

可根据id删除,也可自定义selection条件删除

属性名

类型

默认值

说明

tableName

String

‘’

表名

idKey

String

‘_id’

id字段,默认 ‘_id’

idValue

String

id的值,如 1

selection

String

‘’

查询的字段,如 ‘sex = ?’(注意selection有值时,idKey和idValue无效,)

selectionArgs

String[]

[]

查询的字段的值,如 [‘1’]

clear 方法参数

清空表

属性名

类型

默认值

说明

dbName

String

‘ba-db.db’

数据库名称

tableName

String

‘’

表名

query 方法参数

查询数据

属性名

类型

默认值

说明

dbName

String

‘ba-db.db’

数据库名称

tableName

String

‘’

表名

selection

String

‘’

查询的字段,如 ‘sex = ?’

selectionArgs

String[]

[]

查询的字段的值,如 [‘1’]

groupBy

String

‘’

对相同的数据进行分组

having

String

‘’

orderBy

String

‘’

一个或多个列按升序或降序顺序排列数据

limit

String

‘’

限制返回的数据数量

rawQuery 方法参数

自定义sql语句查询

属性名

类型

默认值

说明

sql

String

‘’

sql语句,如:select * from user

execSQL 方法参数

执行sql语句(无返回)

属性名

类型

默认值

说明

sql

String

‘’

sql语句,如:UPDATE user SET hobby=‘美食’