uniapp 原生sqlite本地数据库管理 Ba-Sqlite

简介(下载地址

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

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

效果展示

在这里插入图片描述

使用方法

引入组件

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 方法参数

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

属性名类型默认值说明
dbNameString‘ba-db.db’数据库名称

isHasDb 方法参数

查询某个数据库是否存在

属性名类型默认值说明
dbNameString‘ba-db.db’数据库名称
dbPathString默认自定义数据库路径,不传为默认路径(传值为应用可访问的手机本地路径)
返回:
{"isHasDb":false,"msg":"success","ok":true}

createTable 方法参数

创建表

属性名类型默认值说明
tableNameString‘’表名
columnNamesArray[]表结构(参考如下)
表结构 columnNames
属性名类型默认值说明
nameString‘’字段名称
typeString‘’字段类型(text、int)
isIdBooleanfalse是否是主键(默认主键’_id’)
isAutoBooleanfalse是否自增(仅主键)
notNullBooleanfalse是否不能为空

deleteTable 方法参数

删除表

属性名类型默认值说明
tableNameString‘’表名

isHasTable 方法参数

查询某个表是否存在

属性名类型默认值说明
tableNameString‘’表名
返回:
{"isHasTable":false,"msg":"success","ok":true}

insert 方法参数

插入数据

属性名类型默认值说明
tableNameString‘’表名
valuesArray‘’需要插入的数据(json),如 [{_id: 1,name: ‘张三’,sex: ‘1’}]

replace 方法参数

更新数据

属性名类型默认值说明
tableNameString‘’表名
valuesArray‘’需要更新的数据(json),如 [{_id: 1,name: ‘张三’,sex: ‘1’}]

update 方法参数

根据条件批量修改数据

属性名类型默认值说明
tableNameString‘’表名
selectionString‘’查询的字段,如 ‘sex = ?’
selectionArgsString[][]查询的字段的值,如 [‘1’]
valuesObject{}批量更改的数据,如 {hobby: ‘逛街’}

delete 方法参数

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

属性名类型默认值说明
tableNameString‘’表名
idKeyString‘_id’id字段,默认 ‘_id’
idValueStringid的值,如 1
selectionString‘’查询的字段,如 ‘sex = ?’(注意selection有值时,idKey和idValue无效,)
selectionArgsString[][]查询的字段的值,如 [‘1’]

clear 方法参数

清空表

属性名类型默认值说明
dbNameString‘ba-db.db’数据库名称
tableNameString‘’表名

query 方法参数

查询数据

属性名类型默认值说明
dbNameString‘ba-db.db’数据库名称
tableNameString‘’表名
selectionString‘’查询的字段,如 ‘sex = ?’
selectionArgsString[][]查询的字段的值,如 [‘1’]
groupByString‘’对相同的数据进行分组
havingString‘’
orderByString‘’一个或多个列按升序或降序顺序排列数据
limitString‘’限制返回的数据数量

rawQuery 方法参数

自定义sql语句查询

属性名类型默认值说明
sqlString‘’sql语句,如:select * from user

execSQL 方法参数

执行sql语句(无返回)

属性名类型默认值说明
sqlString‘’sql语句,如:UPDATE user SET hobby=‘美食’

系列插件

图片选择插件 Ba-MediaPicker文档

图片编辑插件 Ba-ImageEditor文档

文件选择插件 Ba-FilePicker文档

应用消息通知插件 Ba-Notify文档

应用未读角标插件 Ba-Shortcut-Badge文档

应用开机自启插件 Ba-Autoboot文档

扫码原生插件(毫秒级、支持多码)文档

动态修改状态栏、导航栏背景色、字体颜色插件 Ba-AppBar文档

原生sqlite本地数据库管理 Ba-Sqlite文档

安卓保活插件 Ba-KeepAlive文档

安卓快捷方式(桌面长按app图标) Ba-Shortcut文档

自定义图片水印 Ba-Watermark文档

视频压缩插件 Ba-VideoCompressor文档

动态切换应用图标、名称(如新年、国庆等) Ba-ChangeIcon文档

  • 0
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
UniApp中使用SQLite数据库,你可以借助第三方插件uni-sqlite来实现。以下是使用uni-sqlite插件UniApp中使用SQLite数据库的基本步骤: 1. 安装插件:在UniApp项目中,使用命令行工具或插件市场安装uni-sqlite插件。 2. 引入插件:在需要使用SQLite的页面或组件中,通过`require`方法引入uni-sqlite插件。例如: ```javascript const db = require('@dcloudio/uni-sqlite') ``` 3. 打开数据库:在需要操作数据库的地方,通过`db.openDatabase`方法打开数据库连接。例如: ```javascript const databasePath = uni.env.USER_DATA_PATH + '/your-database-name.db' const database = db.openDatabaseSync(databasePath) ``` 这段代码会创建一个名为"your-database-name.db"的数据库文件,并将其存储在用户数据目录中。 4. 创建表和操作数据:可以使用SQLite语句创建表和执行数据操作。例如: ```javascript // 创建表 database.executeSql('CREATE TABLE IF NOT EXISTS chat_messages (id INTEGER PRIMARY KEY AUTOINCREMENT, content TEXT, timestamp INTEGER)') // 插入数据 database.executeSql('INSERT INTO chat_messages (content, timestamp) VALUES (?, ?)', ['Hello', Date.now()]) // 查询数据 const result = database.executeSql('SELECT * FROM chat_messages') console.log(result.rows) ``` 这段代码展示了创建名为"chat_messages"的表,以及插入和查询数据的示例。 需要注意的是,uni-sqlite插件是基于原生平台的SQLite实现,因此在运行时需要在对应的平台环境下进行测试和调试。另外,SQLite支持的语法和特性可能与其他数据库有所不同,可以参考SQLite的官方文档进行更详细的了解和学习。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值