下载人数: 0
下载次数: 0
收藏人数:
0
更新日期:2026-03-02
版本:v1.0.0
更新记录
v1.0.0(2026-03-02)
初始化
平台兼容性
uni-app x(4.35)
| Chrome |
Safari |
Android |
iOS |
鸿蒙 |
微信小程序 |
| × |
× |
√ |
- |
√ |
× |
<button class="btn" @click="handleInit">1. 初始化表</button>
<button class="btn btn-blue half" @click="handleBatchInsert">2. 事务批量插入 (100条)</button>
<button class="btn btn-orange half" @click="handleCount">3. 统计总人数 (Count)</button>
<button class="btn btn-red" @click="handleTransactionFail">4. 测试事务回滚 (异常撤销)</button>
<button class="btn btn-green" @click="handleQuery">5. 查看最后 5 条数据</button>
<button class="btn" @click="handleClear">清空日志</button>
控制台日志 (已开启 SQL 打印):
{{ logContent }}
import {
ref
} from 'vue';
import {
zorm
} from '@/uni_modules/vegetableKun-zorm/utssdk/index.uts';
const logContent = ref('等待操作...');
// 辅助函数:添加日志到面板
const addLog = (msg: string) => {
const now = new Date().toLocaleTimeString();
logContent.value = `[${now}] ${msg}\n${logContent.value}`;
};
/**
* 1. 初始化数据库与表
*/
const handleInit = async () => {
const connected = await zorm.connect("zorm_advance.db");
if (connected) {
await zorm.execute(
"CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)",
[]);
addLog("数据库与表已就绪");
}
};
/**
* 2. 事务批量插入:感受极致提速
* 在事务中执行循环插入,性能远高于普通循环插入
*/
const handleBatchInsert = async () => {
addLog("事务批量插入中...");
const startTime = Date.now();
const success = await zorm.transaction(async () => {
for (let i = 0; i < 100; i++) {
// 在 transaction 回调中直接执行 SQL
await zorm.execute("INSERT INTO users (name, age) VALUES (?, ?)", ["用户" + i, 20 + i]);
}
});
const duration = Date.now() - startTime;
if (success) {
addLog(`✅ 100条数据插入完成!耗时:${duration}ms`);
} else {
addLog("❌ 批量插入失败");
}
};
/**
* 3. 统计功能演示
*/
const handleCount = async () => {
// 全表统计
const total = await zorm.count("users", null, null);
// 带条件的统计
const seniors = await zorm.count("users", "age > ?", [50]);
addLog(`总人数:${total},其中大于50岁:${seniors}`);
};
/**
* 4. 事务回滚测试
* 模拟:在事务中先插入数据,随后抛出异常,验证数据是否自动撤销
*/
const handleTransactionFail = async () => {
addLog("开始测试回滚...");
const beforeCount = await zorm.count("users", null, null);
const success = await zorm.transaction(async () => {
await zorm.execute("INSERT INTO users (name, age) VALUES (?, ?)", ["临时工", 99]);
addLog("已执行插入操作,现在模拟崩溃...");
// 故意抛出错误触发回滚
throw new Error("意料之中的崩溃");
});
const afterCount = await zorm.count("users", null, null);
if (!success) {
addLog(`事务回滚成功!插入前:${beforeCount}条,回滚后:${afterCount}条 (数据未发生改变)`);
}
};
/**
* 5. 常规查询展示
*/
const handleQuery = async () => {
const res = await zorm.query("SELECT * FROM users ORDER BY id DESC LIMIT 5", []);
if (res.success) {
addLog("最近5条数据:\n" + JSON.stringify(res.data));
}
};
const handleClear = () => {
logContent.value = "";
};
<style scoped>
.container {
padding: 20px;
background-color: #f0f0f2;
height: 100%;
}
.header {
font-size: 16px;
font-weight: bold;
margin-bottom: 20px;
text-align: center;
}
.action-card {
background-color: #fff;
padding: 15px;
border-radius: 12px;
margin-bottom: 20px;
}
.btn {
margin-bottom: 10px;
background-color: #007aff;
color: #fff;
font-size: 14px;
}
.btn-blue {
background-color: #00aaff;
}
.btn-green {
background-color: #34c759;
}
.btn-orange {
background-color: #ff9500;
}
.btn-red {
background-color: #ff3b30;
}
.row {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.half {
width: 48%;
}
.result-area {
flex: 1;
background-color: #1c1c1e;
padding: 10px;
border-radius: 8px;
min-height: 200px;
}
.label {
color: #8e8e93;
font-size: 11px;
margin-bottom: 5px;
display: block;
}
.log-text {
color: #32d74b;
font-family: monospace;
font-size: 12px;
white-space: pre-wrap;
}
</style>
隐私、权限声明
1. 本插件需要申请的系统权限列表:
无
2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:
插件不采集任何数据
3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:
无