HarmonyOS 鸿蒙Next SQLite DELETE 时使用RETURNING 语句应该调用rdb的哪个接口
HarmonyOS 鸿蒙Next SQLite DELETE 时使用RETURNING 语句应该调用rdb的哪个接口
数据模型如下:
class User { name: string age: number constructor(name: string, age: number) { this.name = name this.age = age; } }
SQL 语句如下:DELETE FROM db_user WHERE age = 18 RETURNING *;
调用rdb的 execute() 函数,只能返回1;如果调用 querySql 返回数据不正确且没有执行DELETE操作。
麻烦问下如果 DELETE 语句中用到了 RETURNING ,该调用rdb的哪个接口去执行该SQL语句?
更多关于HarmonyOS 鸿蒙Next SQLite DELETE 时使用RETURNING 语句应该调用rdb的哪个接口的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
测试插入多条数据后,可以删除 id = 2 的记录,查询id=2删除
const SQL_DELETE_TABLE = ' DELETE FROM EMPLOYEE where id = 2 RETURNING * ';
import fs from '[@ohos](/user/ohos).file.fs';
import buffer from '[@ohos](/user/ohos).buffer';
import { ValuesBucket } from '[@ohos](/user/ohos).data.ValuesBucket';
import common from '[@ohos](/user/ohos).app.ability.common';
import relationalStore from '[@ohos](/user/ohos).data.relationalStore'; // 导入模块
import { BusinessError } from '[@ohos](/user/ohos).base';
import promptAction from '[@ohos](/user/ohos).promptAction';
let store: relationalStore.RdbStore | undefined = undefined;
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
const table_name: string = 'EMPLOYEE'
// 建表Sql语句
const SQL_CREATE_TABLE =
`CREATE TABLE IF NOT EXISTS ${table_name} (` +
'ID INTEGER PRIMARY KEY AUTOINCREMENT, ' +
'NAME TEXT NOT NULL, AGE INTEGER, ' +
'SALARY REAL, ' +
'CODES BLOB' +
')';
[@Entry](/user/Entry)
[@Component](/user/Component)
struct Index {
[@State](/user/State) message: string = 'Hello World';
[@State](/user/State) database_version: number = 0;
[@State](/user/State) database_name: string = '未创建';
[@State](/user/State) database_flag: number = -1;
[@State](/user/State) create_flag: number = -1
[@State](/user/State) table_name1: string = 'EMPLOYEE'
context = getContext(this) as common.UIAbilityContext;
build() {
Column() {
//1、数据库创建
Row() {
Text(`数据库名:${this.database_name}`)
.textAlign(TextAlign.Start)
.fontSize(12)//.border({ width: 1 })
.padding(10)
.width('50%')
}
.width('90%')
.border({ width: 1 })
//2、建表
Row() {
if (this.create_flag === 1) {
Text(`表名:${this.table_name1}`)
.textAlign(TextAlign.Start)
.fontSize(12)
.padding(10)
.width('50%')
} else {
Text(`表名:未建表`)
.textAlign(TextAlign.Start)
.fontSize(12)
.padding(10)
.width('50%')
}
}
.width('90%')
.border({ width: 1 })
Button('1、创建数据库')
.onClick(() => {
if (this.database_flag === -1) {
this.getDataStore()
} else {
if (this.database_flag === 1) {
promptAction.showToast({
message: '数据库已创建'
});
}
}
})
Button('2、建表')
.onClick(() => {
if (this.database_flag === -1) {
promptAction.showToast({
message: '数据库未创建,请先创建数据库'
});
return;
}
this.createDatabase()
})
Button('3、插入数据')
.onClick(() => {
this.insertDate()
})
Button('4、delete')
.onClick(() => {
this.delete()
})
Button('6、查询数据表')
.onClick(() => {
this.queryData()
})
}
}
//获取store,创建数据库
getDataStore() {
//设置数据库基本配置信息
const STORE_CONFIG: relationalStore.StoreConfig = {
name: "RdbTest.db", // 数据库文件名
securityLevel: relationalStore.SecurityLevel.S1// 数据库安全级别
};
relationalStore.getRdbStore(this.context, STORE_CONFIG).then(async (rdbStore: relationalStore.RdbStore) => {
store = rdbStore; //操作关系型数据库
console.info('成功获取RdbStore:' + store)
this.database_name = STORE_CONFIG.name
this.database_flag = 1
}).catch((err: BusinessError) => {
console.error(`获取RdbStore失败, code is ${err.code},message is ${err.message}`);
})
}
//建表
createDatabase() {
if (store != undefined) {
(store as relationalStore.RdbStore).executeSql(SQL_CREATE_TABLE, (err) => {
if (err) {
console.error(`数据表创建失败, code is ${err.code},message is ${err.message}`);
return;
}
console.info('数据表创建成功');
this.create_flag = 1
// 设置数据库版本
if(store != undefined) {
// this.database_version=(store as relationalStore.RdbStore).version+1;
(store as relationalStore.RdbStore).version = 3;
// 获取数据库版本
console.info(`RdbStore version is ${store.version}`);
}
})
}
}
//插入数据
insertDate() {
//数据初始化
let value1 = 'Lisa';
let value2 = 18;
let value3 = 100.5;
let value4 = new Uint8Array([1, 2, 3, 4, 5]);
// 以下三种方式可用
const valueBucket1: ValuesBucket = {
'NAME': value1,
'AGE': value2,
'SALARY': value3,
'CODES': value4,
};
if (store != undefined) {
(store as relationalStore.RdbStore).insert("EMPLOYEE", valueBucket1, (err: BusinessError, rowId: number) => {
if (err) {
console.error(`插入失败, code is ${err.code},message is ${err.message}`);
return;
}
console.info(`插入成功, rowId是:${rowId}`);
promptAction.showToast({
message: `插入成功`
});
})
}
}
delete(){
// 删除表中所有数据
if(store != undefined) {
const SQL_DELETE_TABLE = ' DELETE FROM EMPLOYEE where id = 2 RETURNING * ';
(store as relationalStore.RdbStore).execute(SQL_DELETE_TABLE).then((data) => {
console.info(`delete result: ${data}`);
}).catch((err: BusinessError) => {
console.error(`delete failed, code is ${err.code}, message is ${err.message}`);
})
}
}
queryData() {
predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Lisa");
if (store != undefined) {
(store as relationalStore.RdbStore).query(predicates, (err, resultSet) => {
if (err) {
console.error(`查询失败, code is ${err.code},message is ${err.message}`);
return;
}
console.log(`结果集:ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`);
if (resultSet.rowCount === 0) {
console.log(`结果集行数:${resultSet.rowCount},没有查询到数据`)
return;
} else {
console.log(`查询到:${resultSet.rowCount}行数据`)
}
let str:string = ''
// resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。
while (resultSet.goToNextRow()) {
const id = resultSet.getLong(resultSet.getColumnIndex("ID"));
const name = resultSet.getString(resultSet.getColumnIndex("NAME"));
const age = resultSet.getLong(resultSet.getColumnIndex("AGE"));
const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY"));
const CODES = resultSet.getDouble(resultSet.getColumnIndex("CODES"));
console.log(`查询结果:id=${id}, name=${name}, age=${age}, salary=${salary}, CODES=${CODES}`);
str+=`查询结果:id=${id}, name=${name}, age=${age}, salary=${salary}\n`
}
promptAction.showToast({
message: str
});
// 释放数据集的内存
resultSet.close();
})
}
}
}
更多关于HarmonyOS 鸿蒙Next SQLite DELETE 时使用RETURNING 语句应该调用rdb的哪个接口的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,SQLite的DELETE
语句配合RETURNING
子句用于在删除记录的同时返回被删除的记录集。为了实现这一功能,你需要调用RDB(Relational Database)层提供的特定接口。
在鸿蒙的RDB框架中,DELETE
操作配合RETURNING
的功能通常通过RdbStore::DeleteWithReturning
接口来实现。这个接口允许你执行删除操作并获取被删除的数据。
具体调用方式(伪代码示例):
// 假设已经有一个RdbStore实例store
RdbStore* store = ...;
// 定义删除条件和返回的列
std::vector<std::string> returnColumns = {"column1", "column2"}; // 需要返回的列名
std::string condition = "WHERE id = ?";
std::vector<ValueType> conditionValues = {ValueType(1)}; // 条件参数值
// 执行删除并返回结果
std::unique_ptr<ResultSet> resultSet = store->DeleteWithReturning("your_table_name", condition, conditionValues, returnColumns);
// 处理resultSet
if (resultSet != nullptr) {
while (resultSet->Next()) {
// 读取结果
}
}
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html