Initial commit
This commit is contained in:
81
ak/sqlite.uts
Normal file
81
ak/sqlite.uts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { createSQLiteContext } from '../uni_modules/ak-sqlite/utssdk/app-android/index.uts'
|
||||
import { createSQLiteContextOptions } from '../uni_modules/ak-sqlite/utssdk/interface.uts'
|
||||
//创建查询的上下文
|
||||
export const sqliteContext = createSQLiteContext({
|
||||
name: 'ble.db',
|
||||
} as createSQLiteContextOptions);
|
||||
|
||||
// 初始化所有表
|
||||
export function initTables() {
|
||||
const sql = `
|
||||
|
||||
CREATE TABLE IF NOT EXISTS ble_event_log (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
device_id TEXT,
|
||||
event_type TEXT,
|
||||
detail TEXT,
|
||||
timestamp INTEGER
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS ble_data_log (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
device_id TEXT,
|
||||
service_id TEXT,
|
||||
char_id TEXT,
|
||||
direction TEXT,
|
||||
data TEXT,
|
||||
timestamp INTEGER
|
||||
);
|
||||
`;
|
||||
console.log(sql); // 打印建表语句,便于调试
|
||||
sqliteContext.executeSql({
|
||||
sql: sql,
|
||||
success: function(res) { console.log('SQLite表已创建', res); },
|
||||
fail: function(err) { console.error('SQLite表创建失败', err); }
|
||||
});
|
||||
}
|
||||
|
||||
// 测试插入一条 ble_data_log 日志
|
||||
export function testInsertDataLog() {
|
||||
sqliteContext.executeSql({
|
||||
sql: `
|
||||
INSERT INTO ble_data_log (device_id, service_id, char_id, direction, data, timestamp)
|
||||
VALUES ('test_device', 'test_service', 'test_char', 'send', 'test_data', ${Date.now()})
|
||||
`,
|
||||
success: (res) => { console.log('插入 ble_data_log 成功', res); },
|
||||
fail: (err) => { console.error('插入 ble_data_log 失败', err); }
|
||||
});
|
||||
}
|
||||
|
||||
// 测试查询所有 ble_data_log 日志
|
||||
export function testQueryDataLog() {
|
||||
sqliteContext.selectSql({
|
||||
sql: `SELECT * FROM ble_data_log ORDER BY timestamp DESC`,
|
||||
success: (res) => { console.log('查询 ble_data_log 成功', res); },
|
||||
fail: (err) => { console.error('查询 ble_data_log 失败', err); }
|
||||
});
|
||||
}
|
||||
|
||||
// 测试更新 ble_data_log(将 data 字段改为 'updated_data',只更新最新一条)
|
||||
export function testUpdateDataLog() {
|
||||
sqliteContext.executeSql({
|
||||
sql: `
|
||||
UPDATE ble_data_log
|
||||
SET data = 'updated_data'
|
||||
WHERE id = (SELECT id FROM ble_data_log ORDER BY timestamp DESC LIMIT 1)
|
||||
`,
|
||||
success: (res) => { console.log('更新 ble_data_log 成功', res); },
|
||||
fail: (err) => { console.error('更新 ble_data_log 失败', err); }
|
||||
});
|
||||
}
|
||||
|
||||
// 测试删除 ble_data_log(删除最新一条)
|
||||
export function testDeleteDataLog() {
|
||||
sqliteContext.executeSql({
|
||||
sql: `
|
||||
DELETE FROM ble_data_log
|
||||
WHERE id = (SELECT id FROM ble_data_log ORDER BY timestamp DESC LIMIT 1)
|
||||
`,
|
||||
success: (res) => { console.log('删除 ble_data_log 成功', res); },
|
||||
fail: (err) => { console.error('删除 ble_data_log 失败', err); }
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user