更新记录

1.0.4(2024-04-14)

新增获取db文件地址函数getDatabasePath

1.0.3(2024-04-05)

修复创建和更新表时不能执行多行语句 新增获取sql语句的表名函数:getSqlTableNames

1.0.2(2024-04-02)

新增删除数据库

查看更多

平台兼容性

Vue2 Vue3
×
App 快应用 微信小程序 支付宝小程序 百度小程序 字节小程序 QQ小程序
HBuilderX 4.01,Android:5.0,iOS:不支持 × × × × × ×
钉钉小程序 快手小程序 飞书小程序 京东小程序
× × × ×
H5-Safari Android Browser 微信浏览器(Android) QQ浏览器(Android) Chrome IE Edge Firefox PC-Safari
× × × × × × × × ×

Young-Sqlite

  • Sqlite数据库执行插件,提供数据库语句执行操作。4.0.7,亲测无需打包自定义基座。

基础使用

<template>
    <view class="content">
        <textarea placeholder="请输入sql语句,每条使用;分隔" :value="testsqlcode" @input="inputtest"
            style="max-height: 300rpx; width: 100%;padding: 5px;background-color: bisque;"></textarea>
        <button type="primary" @click="test" style="width: 100%;margin-top: 10rpx;"> 执行SQL </button>
        <text style="margin-top: 10rpx;">{{testText}}</text>
        <button type="primary" @click="del" style="width: 100%;margin-top: 10rpx;"> 删除SQL </button>
    </view>
</template>
<script>
    import { useSqliteManager, InitSql, EmptySqlite, SqliteApiOptions } from "@/uni_modules/Young-Sqlite";
    export default {
        data() {
            return {
                testText: '',
                testsqlcode: 'INSERT INTO my_table (id, name) VALUES ("111", "name");INSERT INTO test_table (id, name) VALUES ("222", "name");SELECT * FROM my_table;SELECT * FROM test_table;',
                test_db: null as EmptySqlite,
            }
        },
        onLoad() {
            this.test_db = useSqliteManager({
                DbVersion: 5,
                dbObject: null,
                DbName: 'test', // 不用携带文件后缀.db
                createAfterUpgrade: true, // 是否升级版本后执行创建sql
                CreateSql: "CREATE TABLE IF NOT EXISTS my_table (id INTEGER PRIMARY KEY, name TEXT);" +
                    "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, name TEXT);",
                UpgradeSql: 'DROP TABLE IF EXISTS my_table;DROP TABLE IF EXISTS test_table' // 删除表
            } as InitSql);
            console.log('初始化成功', this.test_db!.uuid());
        },
        methods: {
            inputtest(e : UniInputEvent) {
                // console.log(e.detail.value);
                this.testsqlcode = e.detail.value;
            },
            del() {
                const res = this.test_db!.deleteDatabase("test");
                uni.showToast({
                    title: res.Msg,
                    position: "bottom"
                })
            },
            test() {
                // 批量执行sql语句
                this.testText = '';
                this.test_db!.ExecSQL({
                    ignoreErrors: true, // 遇到错误是否继续执行下去
                    sql: this.testsqlcode as string,
                    success: res => {
                        console.log('执行成功', res);
                        for (var i = 0; i < res.Data.length; i++) {
                            const item = res.Data[i];
                            const cursor = item.Cursor;
                            // console.log(typeof cursor);
                            if (item.Result == true && item.Type == "SELECT" && cursor != null) {
                                if (cursor.moveToFirst()) {
                                    do {
                                        // 获取列值:id 和 name
                                        const id = cursor.getInt(cursor.getColumnIndex('id'));
                                        const name = cursor.getString(cursor.getColumnIndex('name'));
                                        console.log(`表名: ${test_db.getSqlTableNames(item.Sql)}, ID: ${id}, Name: ${name}`);
                                        this.testText += `[${new Date().getTime()}] 表名: ${test_db.getSqlTableNames(item.Sql)}, ID: ${id}, Name: ${name}\n\n`;
                                    } while (cursor.moveToNext());
                                } else {
                                    console.log('未查询到数据');
                                    this.testText += `[${new Date().getTime()}] 未查询到数据\n\n`;
                                }
                            } else if (item.Result == true && item.Type == "INSERT") {
                                console.log(`插入:${item.Sql}, 结果: ${item.Result}`);
                                this.testText += `[${new Date().getTime()}] 插入:${item.Sql}, 结果: ${item.Result}\n\n`;
                            }
                            else {
                                console.log(`执行:${item.Sql}, 错误: ${item.ErrMsg}`);
                                this.testText += `[${new Date().getTime()}] 执行:${item.Sql}, 错误: ${item.ErrMsg}\n\n`;
                            }
                        }
                    }, fail: err => {
                        console.log('执行错误', err);
                        this.testText += `[${new Date().getTime()}] 执行错误: ${err.errMsg}\n\n`;
                    }, complete: res => {
                        console.log('执行完成', res);
                    }
                } as SqliteApiOptions);
            }
        }
    }
</script>

开发文档

UTS 语法 UTS API插件 UTS 组件插件 Hello UTS

隐私、权限声明

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

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

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

许可协议

MIT协议

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