更新记录
1.0.0(2026-11-05) 下载此版本
1.0.0
- 首个版本,功能可用
平台兼容性
pk-sqlite 本地数据库套件
plus.sqlite 轻量 ORM 封装:链式无需、SQL 全参数占位防注入、事务自动回滚、JSON 列透明编解码。App 端可用(plus.sqlite 为 HBuilderX 内置模块,无需额外插件)。
为什么做它
裸 plus.sqlite 只有 executeSql/selectSql 两个口子:SQL 拼接全靠手写、注入风险全靠自己防、事务要自己 BEGIN/COMMIT/ROLLBACK 三段式。pk-sqlite 用「where 对象 + 值全占位」把注入面收干净,事务一行包完。
快速上手
import db from '@/uni_modules/pk-sqlite/js_sdk/index.js';
await db.open('myapp');
await db.createTable('myapp', 'users', {
id: { type: 'integer', primaryKey: true, autoIncrement: true },
name: { type: 'text', notNull: true },
score: { type: 'real', default: 0 },
meta: 'TEXT' // JSON 列
});
await db.insert('myapp', 'users', { name: '张三', score: 90, meta: { vip: true } });
const r = await db.select('myapp', 'users', {
where: { score: { $op: '>=', $v: 60 } },
orderBy: 'score', desc: true, limit: 10, jsonCols: ['meta']
});
r.rows[0].meta.vip; // true — JSON 自动还原
await db.transaction('myapp', async () => {
await db.insert('myapp', 'users', { name: '李四' });
await db.update('myapp', 'users', { score: 100 }, { name: '李四' });
// 抛错自动 ROLLBACK
});
全部方法
| 方法 | 说明 |
|---|---|
open(name, { path, _mock }) |
打开/创建库(默认 _doc/{name}.db) |
close(name) / isOpen(name) |
关闭 / 状态 |
execute(sql, params) |
任意 SQL |
createTable(name, table, schema) |
建表(IF NOT EXISTS) |
insert(name, table, row, opts) |
插入;{ orIgnore, orReplace } 冲突策略 |
insertBatch(name, table, rows) |
批量插入(事务包裹) |
update(name, table, set, where) |
更新,返回 affected |
del(name, table, where) |
删除,返回 affected |
select(name, table, q) |
查询:{ columns, where, orderBy, desc, limit, offset, jsonCols } |
count(name, table, where) |
计数 |
tableExists(name, table) / tables(name) |
表存在性 / 表清单 |
transaction(name, fn) |
事务,fn 抛错自动 ROLLBACK(E-SQ-30) |
where 语法
{ uid: 7 } // uid = ?
{ uid: 7, remark: null } // AND remark IS NULL
{ age: { $op: '>=', $v: 18 } } // AND age >= ?
{ sql: 'age > ? AND name LIKE ?', params: [18, '%x%'] } // 复杂条件透传
安全设计
- 标识符白名单:表名/列名必须过
validateIdent(/^[A-Za-z_][A-Za-z0-9_]{0,63}$/),注入串直接E-SQ-01快速失败 - 值全占位:所有值走
?参数绑定,操作符只允许白名单(= != > >= < <= LIKE)
示例工程
example/ 为完整可跑工程(node 测试用内存 mock driver 全链路验证)。在线文档:https://plugins.cnajiu.cn/public/pk-sqlite.html

收藏人数:
下载插件并导入HBuilderX
下载插件ZIP
赞赏(0)
下载 3
赞赏 0
下载 12744412
赞赏 1959
赞赏
京公网安备:11010802035340号