更新记录

1.0.4(2026-04-25)

更新安卓以及修复鸿蒙端。

1.0.3(2026-04-23)

优化

1.0.2(2026-04-23)

新增关闭数据库

查看更多

平台兼容性

uni-app x(5.0)

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>
  <text v-if="list">------------获取到的数据列表----------</text>
  <list-view>
    <list-item v-for="(item, index) in list">
      <text>{{ index }} : id: {{ item['id'] }} - name: {{ item['name'] }} - age: {{ item['age'] }}</text>
    </list-item>
  </list-view>
</template>
<script lang="uts">
import { openDatabase, executeSQL, querySQL, closeDatabase } from '@/uni_modules/tc-sqlite';

export default {
  data() {
    return {
      dbOpened: false,   // 数据库是否已打开
      resultText: '',
      list: null as UTSJSONObject[] | null,
    };
  },
  methods: {
    async OpenDB() {
      const success = await openDatabase('test.db', 1);
      if (success) {
        this.dbOpened = true;
        this.resultText = '数据库已打开';
      } else {
        uni.showToast({ title: '数据库打开失败', icon: 'none' });
      }
    },
    async CloseDB() {
      if (!this.dbOpened) {
        uni.showToast({ title: '数据库未打开', icon: 'none' });
        return;
      }
      const success = await closeDatabase();
      if (success) {
        this.dbOpened = false;
        this.resultText = '数据库已关闭';
      } else {
        uni.showToast({ title: '关闭失败', icon: 'none' });
      }
    },
    async 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 = await executeSQL(sql);
      if (success) {
        uni.showToast({ title: '表创建成功', icon: 'success' });
      } else {
        uni.showToast({ title: '表创建失败', icon: 'none' });
      }
    },
    async InsertData() {
      if (!this.dbOpened) {
        uni.showToast({ title: '请先打开数据库', icon: 'none' });
        return;
      }
      const sql = "INSERT INTO users (name, age) VALUES ('张三', 25)";
      const success = await executeSQL(sql);
      if (success) {
        uni.showToast({ title: '数据插入成功', icon: 'success' });
      } else {
        uni.showToast({ title: '数据插入失败', icon: 'none' });
      }
    },
    async AllData() {
      if (!this.dbOpened) {
        uni.showToast({ title: '请先打开数据库', icon: 'none' });
        return;
      }
      const rows = await querySQL("SELECT * FROM users");
      if (rows.length > 0) {
        this.list = rows as UTSJSONObject[];
      }
      this.resultText = "全部数据";
    },
    async queryIdData() {
      if (!this.dbOpened) {
        uni.showToast({ title: '请先打开数据库', icon: 'none' });
        return;
      }
      const id = 2;
      const sql = "SELECT * FROM users WHERE id="+id;
      const rows = await querySQL(sql);
      if (rows.length > 0) {
        this.list = rows as UTSJSONObject[];
        // 另一种方式:单独取第一个
        // let row = rows[0] as UTSJSONObject;
        // this.resultText = row['name'] as string;
        this.resultText = "按条件查询";
      } else {
        this.resultText = '未找到的记录';
      }
    },
    async DeleteData() {
      if (!this.dbOpened) {
        uni.showToast({ title: '请先打开数据库', icon: 'none' });
        return;
      }
      const id = 1;
      const sql = "DELETE FROM users WHERE id="+id;
      const success = await executeSQL(sql);
      if (success) {
        uni.showToast({ title: '删除成功', icon: 'success' });
        this.AllData(); // 刷新显示
      } else {
        uni.showToast({ title: '删除失败,可能ID不存在', icon: 'none' });
      }
    },
    async queryPageData() {
      if (!this.dbOpened) {
        uni.showToast({ title: '请先打开数据库', icon: 'none' });
        return;
      }
      const countSql = "SELECT COUNT(*) as total FROM users";
      const countResult = await querySQL(countSql);
      const total = countResult.length;
      if (total == 0) {
        this.resultText = '暂无数据,请先插入记录';
        return;
      }
      const pageNum = 1;
      const pageSize = 2;
      const offset = (pageNum - 1) * pageSize;
      const sql = "SELECT * FROM users LIMIT "+pageSize+" OFFSET "+offset+"";
      const rows = await querySQL(sql);
      this.list = rows as UTSJSONObject[];
      this.resultText = "分页查询";
    }
  }
};
</script>

隐私、权限声明

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

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

插件不采集任何数据

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