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

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

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

效果展示

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

; 使用方法

引入组件

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,

                    },
                    (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的值,如 1selectionString”查询的字段,如 ‘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文档

Original: https://blog.csdn.net/u013164293/article/details/126773725
Author: 三杯五岳
Title: uniapp 原生sqlite本地数据库管理 Ba-Sqlite

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/815282/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球