更新记录

1.0.3(2026-04-23)

优化

1.0.2(2026-04-23)

新增关闭数据库

1.0.1(2026-04-23)

增加注释

查看更多

平台兼容性

uni-app x(3.7.6)

Chrome Safari Android Android插件版本 iOS 鸿蒙 鸿蒙插件版本 微信小程序
× × 5.0 1.0.0 × 9 1.0.0 -

tc-sqlite

导入插件后创建一个.uvue 复制下方代码即可跑通,关闭页面或不再继续数据库操作,记得关闭数据库。

DEMO

<template>
      <scroll-view class="page">
        <button @click="OpenDB">打开数据库</button>
        <button @click="CloseDB">关闭数据库</button>
        <button @click="CreateTable">创建表</button>
        <button @click="InsertData">插入数据</button>
        <button @click="AllData">查询全部数据</button>
        <button @click="DeleteData">按条件删除(默认填写的是ID 1)</button>
        <button @click="queryIdData">按条件查询</button>
        <button @click="queryPageData">分页查询</button>
        <text class="result">查询结果:{{ resultText }}</text>
    </scroll-view>
</template>
<script lang="uts">
import { openDatabase, executeSQL, querySQL,closeDatabase } from '@/uni_modules/tc-sqlite';
export default {
    data() {
        return {
            dbOpened: false,// 数据库是否已打开
            resultText: ''
        }
    },
    onLoad() {
    },
    onReady() {
    },
    methods: {
        OpenDB() {//打开数据库
            const success = openDatabase('test.db', 1);//// 传入 数据库名.db 版本号 number 类型(默认1就行)
            if (success) {
                this.dbOpened = true;
                this.resultText = '数据库已打开';
            } else {
                uni.showToast({ title: '数据库打开失败', icon: 'none' });
            }
        },
        CloseDB() {//关闭数据库
            if (!this.dbOpened) {
                uni.showToast({ title: '数据库未打开', icon: 'none' });
                return;
            }
            const success = closeDatabase();
            if (success) {
                this.dbOpened = false;
                this.resultText = '数据库已关闭';
            } else {
                uni.showToast({ title: '关闭失败', icon: 'none' });
            }
        },
        CreateTable() {//创建数据表
            if (!this.dbOpened) {
                uni.showToast({ title: '请先打开数据库', icon: 'none' });
                return;
            }
            const sql = "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT NOT NULL,age INTEGER)";
            const success = executeSQL(sql);
            if (success) {
                uni.showToast({ title: '表创建成功', icon: 'success' });
            } else {
                uni.showToast({ title: '表创建失败', icon: 'none' });
            }
        },
        InsertData() {//插入测试数据
            if (!this.dbOpened) {
                uni.showToast({ title: '请先打开数据库', icon: 'none' });
                return;
            }
            const sql = "INSERT INTO users (name, age) VALUES ('张三', 25)";
            const success = executeSQL(sql);
            if (success) {
                uni.showToast({ title: '数据插入成功', icon: 'success' });
            } else {
                uni.showToast({ title: '数据插入失败', icon: 'none' });
            }
        },
        AllData() {//查询全部数据
            if (!this.dbOpened) {
                uni.showToast({ title: '请先打开数据库', icon: 'none' });
                return;
            }
            const sql = "SELECT * FROM users";
            const rows = querySQL(sql);
            this.resultText = JSON.stringify(rows);
        },
        queryIdData() {//按条件查询
            if (!this.dbOpened) {
                uni.showToast({ title: '请先打开数据库', icon: 'none' });
                return;
            }
            let id=2;
            const sql = "SELECT * FROM users WHERE id="+id;
            const rows = querySQL(sql);
            if (rows.length > 0) {
                this.resultText = JSON.stringify(rows[0]);
            } else {
                this.resultText = '未找到的记录';
            }
        },
        DeleteData() {//删除数据库
            if (!this.dbOpened) {
                uni.showToast({ title: '请先打开数据库', icon: 'none' });
                return;
            }
            let id=1;
            const sql = "DELETE FROM users WHERE id ="+id;
            const success = executeSQL(sql);
            if (success) {
                uni.showToast({ title: '删除成功', icon: 'success' });
                this.AllData(); // 刷新显示
            } else {
                uni.showToast({ title: '删除失败,可能ID不存在', icon: 'none' });
            }
        },
        queryPageData() {//分页查询
            if (!this.dbOpened) {
                uni.showToast({ title: '请先打开数据库', icon: 'none' });
                return;
            }
            const countSql = "SELECT COUNT(*) as total FROM users";
            const countResult = querySQL(countSql);
            const total = countResult.length as number;
            if (total== 0) {
                this.resultText = '暂无数据,请先插入记录';
                return;
            }else{
                const pageNum = 1;//第一页
                const pageSize = 2;//显示数据数量
                const offset = (pageNum - 1) * pageSize;
                const sql = "SELECT * FROM users LIMIT "+pageSize" OFFSET "+offset"";
                const rows = querySQL(sql);
                this.resultText = JSON.stringify(rows);
            }
        }
    }
}
</script>

隐私、权限声明

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

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

插件不采集任何数据

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