分析:
本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
导入模块
import relationalStore from '@ohos.data.relationalStore';复制到剪贴板错误复制
复制代码
relationalStore.getRdbStore
getRdbStore(context: Context, config: StoreConfig, callback: AsyncCallback<RdbStore>): void
获得一个相关的RdbStore,操作关系型数据库,用户可以根据本身的需求配置RdbStore的参数,然后通过RdbStore调用相关接口可以实行相关的数据操作,利用callback异步回调。 体系能力: SystemCapability.DistributedDataManager.RelationalStore.Core 参数:
参数名类型必填分析contextContext是应用的上下文。
FA模子的应用Context界说见Context。
Stage模子的应用Context界说见Context。configStoreConfig是与此RDB存储相关的数据库配置。回调AsyncCallback<RdbStore>是指定callback回调函数,返回RdbStore对象。 错误码:
以下错误码的具体介绍请拜见关系型数据库错误码。 错误码ID错误信息14800010Failed to open or delete database by invalid database path.14800011Failed to open database by database corrupted.14800000Inner error.14801001Only supported in stage mode.14801002The data group id is not valid. 示例:
FA模子示例:
import featureAbility from '@ohos.ability.featureAbility';
import { BusinessError } from "@ohos.base";
let store: relationalStore.RdbStore | undefined = undefined;
console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`);
return;
}
console.info('Get RdbStore successfully.');
})
}
}复制到剪贴板错误复制
复制代码
relationalStore.getRdbStore
getRdbStore(context: Context, config: StoreConfig): Promise<RdbStore>
获得一个相关的RdbStore,操作关系型数据库,用户可以根据本身的需求配置RdbStore的参数,然后通过RdbStore调用相关接口可以实行相关的数据操作,利用Promise异步回调。 体系能力: SystemCapability.DistributedDataManager.RelationalStore.Core 参数:
参数名类型必填分析contextContext是应用的上下文。
FA模子的应用Context界说见Context。
Stage模子的应用Context界说见Context。configStoreConfig是与此RDB存储相关的数据库配置。 返回值:
类型分析Promise<RdbStore>Promise对象。返回RdbStore对象。 错误码:
以下错误码的具体介绍请拜见关系型数据库错误码。 错误码ID错误信息14800010Failed to open or delete database by invalid database path.14800011Failed to open database by database corrupted.14800000Inner error.14801001Only supported in stage mode.14801002The data group id is not valid. 示例:
FA模子示例:
import featureAbility from '@ohos.ability.featureAbility';
import { BusinessError } from "@ohos.base";
let store: relationalStore.RdbStore | undefined = undefined;
console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`);
})
}
}
复制代码
relationalStore.deleteRdbStore10+
deleteRdbStore(context: Context, config: StoreConfig, callback: AsyncCallback<void>): void
利用指定的数据库文件配置删除数据库,利用callback异步回调。
删除成功后,发起将数据库对象置为null。若数据库文件处于公共沙箱目录下,则删除数据库时必须利用该接口,当存在多个历程操作同一个数据库的情况,发起向其他历程发送数据库删除关照使其感知并处理。 体系能力: SystemCapability.DistributedDataManager.RelationalStore.Core 参数:
参数名类型必填分析contextContext是应用的上下文。
FA模子的应用Context界说见Context。
Stage模子的应用Context界说见Context。configStoreConfig是与此RDB存储相关的数据库配置。callbackAsyncCallback<void>是指定callback回调函数。 错误码:
以下错误码的具体介绍请拜见关系型数据库错误码。 错误码ID错误信息14800010Failed to open or delete database by invalid database path.14800000Inner error.14801001Only supported in stage mode.14801002The data group id is not valid. 示例:
FA模子示例:
import featureAbility from '@ohos.ability.featureAbility';
import { BusinessError } from "@ohos.base";
let store: relationalStore.RdbStore | undefined = undefined;
console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`);
return;
}
store = undefined;
console.info('Delete RdbStore successfully.');
})
}
}
Copy to clipboardErrorCopied
复制代码
relationalStore.deleteRdbStore10+
deleteRdbStore(context: Context, config: StoreConfig): Promise<void>
利用指定的数据库文件配置删除数据库,利用Promise异步回调。
删除成功后,发起将数据库对象置为null。若数据库文件处于公共沙箱目录下,则删除数据库时必须利用该接口,当存在多个历程操作同一个数据库的情况,发起向其他历程发送数据库删除关照使其感知并处理。 体系能力: SystemCapability.DistributedDataManager.RelationalStore.Core 参数
参数名类型必填分析contextContext是应用的上下文。
FA模子的应用Context界说见Context。
Stage模子的应用Context界说见Context。configStoreConfig是与此RDB存储相关的数据库配置。 返回值:
类型分析Promise<void>无返回结果的Promise对象。 错误码:
以下错误码的具体介绍请拜见关系型数据库错误码。 错误码ID错误信息14800010Failed to open or delete database by invalid database path.14800000Inner error.14801001Only supported in stage mode.14801002The data group id is not valid. 示例:
FA模子示例:
import featureAbility from '@ohos.ability.featureAbility';
import { BusinessError } from "@ohos.base";
let store: relationalStore.RdbStore | undefined = undefined;
(store as relationalStore.RdbStore).querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = 'sanguo'").then((resultSet: relationalStore.ResultSet) => {
getColumnIndex(columnName: string): number
根据指定的列名获取列索引。 体系能力: SystemCapability.DistributedDataManager.RelationalStore.Core 参数:
参数名类型必填分析columnNamestring是表现结果会集指定列的名称。 返回值:
类型分析number返回指定列的索引。 错误码:
以下错误码的具体介绍请拜见关系型数据库错误码。 错误码ID错误信息14800013The column value is null or the column type is incompatible. 示例:
if(resultSet != undefined) {
const id = (resultSet as relationalStore.ResultSet).getLong((resultSet as relationalStore.ResultSet).getColumnIndex("ID"));
const name = (resultSet as relationalStore.ResultSet).getString((resultSet as relationalStore.ResultSet).getColumnIndex("NAME"));
const age = (resultSet as relationalStore.ResultSet).getLong((resultSet as relationalStore.ResultSet).getColumnIndex("AGE"));
const salary = (resultSet as relationalStore.ResultSet).getDouble((resultSet as relationalStore.ResultSet).getColumnIndex("SALARY"));
}Copy to clipboardErrorCopied
复制代码
getColumnName
getColumnName(columnIndex: number): string
根据指定的列索引获取列名。 体系能力: SystemCapability.DistributedDataManager.RelationalStore.Core 参数:
参数名类型必填分析columnIndexnumber是表现结果会集指定列的索引。 返回值:
类型分析字符串返回指定列的名称。 错误码:
以下错误码的具体介绍请拜见关系型数据库错误码。 错误码ID错误信息14800013The column value is null or the column type is incompatible. 示例:
if(resultSet != undefined) {
const id = (resultSet as relationalStore.ResultSet).getColumnName(0);
const name = (resultSet as relationalStore.ResultSet).getColumnName(1);
const age = (resultSet as relationalStore.ResultSet).getColumnName(2);
}复制到剪贴板错误复制
复制代码
goTo
goTo(offset:number): boolean
向前或向后转至结果集的指定行,相对于其当前位置偏移。 体系能力: SystemCapability.DistributedDataManager.RelationalStore.Core 参数:
参数名类型必填分析抵消数是表现相对于当前位置的偏移量。 返回值:
类型分析布尔如果成功移动结果集,则为true;否则返回false。 错误码:
以下错误码的具体介绍请拜见关系型数据库错误码。 错误码ID错误信息14800012The result set is empty or the specified location is invalid. 示例:
if(resultSet != undefined) {
(resultSet as relationalStore.ResultSet).goTo(1);
}复制到剪贴板错误复制
复制代码
goToRow
goToRow(position: number): boolean
转到结果集的指定行。 体系能力: SystemCapability.DistributedDataManager.RelationalStore.Core 参数:
参数名类型必填分析position数是表现要移动到的指定位置。 返回值:
类型分析布尔如果成功移动结果集,则为true;否则返回false。 错误码:
以下错误码的具体介绍请拜见关系型数据库错误码。 错误码ID错误信息14800012The result set is empty or the specified location is invalid. 示例:
if(resultSet != undefined) {
(resultSet as relationalStore.ResultSet).goToRow(5);
}复制到剪贴板错误复制
复制代码
goToFirstRow
goToFirstRow(): boolean
转到结果集的第一行。 体系能力: SystemCapability.DistributedDataManager.RelationalStore.Core 返回值:
类型分析boolean如果成功移动结果集,则为true;否则返回false。 错误码:
以下错误码的具体介绍请拜见关系型数据库错误码。 错误码ID错误信息14800012The result set is empty or the specified location is invalid. 示例:
if(resultSet != undefined) {
(resultSet as relationalStore.ResultSet).goToFirstRow();
}Copy to clipboardErrorCopied
复制代码
goToLastRow
goToLastRow(): boolean
转到结果集的末了一行。 体系能力: SystemCapability.DistributedDataManager.RelationalStore.Core 返回值:
类型分析boolean如果成功移动结果集,则为true;否则返回false。 错误码:
以下错误码的具体介绍请拜见关系型数据库错误码。 错误码ID错误信息14800012The result set is empty or the specified location is invalid. 示例:
if(resultSet != undefined) {
(resultSet as relationalStore.ResultSet).goToLastRow();
}Copy to clipboardErrorCopied
复制代码
goToNextRow
goToNextRow(): boolean
转到结果集的下一行。 体系能力: SystemCapability.DistributedDataManager.RelationalStore.Core 返回值:
类型分析boolean如果成功移动结果集,则为true;否则返回false。 错误码:
以下错误码的具体介绍请拜见关系型数据库错误码。 错误码ID错误信息14800012The result set is empty or the specified location is invalid. 示例:
if(resultSet != undefined) {
(resultSet as relationalStore.ResultSet).goToNextRow();
}Copy to clipboardErrorCopied
复制代码
goToPreviousRow
goToPreviousRow(): boolean
转到结果集的上一行。 体系能力: SystemCapability.DistributedDataManager.RelationalStore.Core 返回值:
类型分析boolean如果成功移动结果集,则为true;否则返回false。 错误码:
以下错误码的具体介绍请拜见关系型数据库错误码。 错误码ID错误信息14800012The result set is empty or the specified location is invalid. 示例:
if(resultSet != undefined) {
(resultSet as relationalStore.ResultSet).goToPreviousRow();
}Copy to clipboardErrorCopied
复制代码
getBlob
getBlob(columnIndex: number): Uint8Array
以字节数组的情势获取当前行中指定列的值。 体系能力: SystemCapability.DistributedDataManager.RelationalStore.Core 参数:
参数名类型必填分析columnIndexnumber是指定的列索引,从0开始。 返回值:
类型分析Uint8Array以字节数组的情势返回指定列的值。 错误码:
以下错误码的具体介绍请拜见关系型数据库错误码。 错误码ID错误信息14800013The column value is null or the column type is incompatible. 示例:
if(resultSet != undefined) {
const codes = (resultSet as relationalStore.ResultSet).getBlob((resultSet as relationalStore.ResultSet).getColumnIndex("CODES"));
}Copy to clipboardErrorCopied
复制代码
getString
getString(columnIndex: number): string
以字符串情势获取当前行中指定列的值。 体系能力: SystemCapability.DistributedDataManager.RelationalStore.Core 参数:
参数名类型必填分析columnIndexnumber是指定的列索引,从0开始。 返回值:
类型分析string以字符串情势返回指定列的值。 错误码:
以下错误码的具体介绍请拜见关系型数据库错误码。 错误码ID错误信息14800013The column value is null or the column type is incompatible. 示例:
if(resultSet != undefined) {
const name = (resultSet as relationalStore.ResultSet).getString((resultSet as relationalStore.ResultSet).getColumnIndex("NAME"));
}Copy to clipboardErrorCopied
复制代码
getLong
getLong(columnIndex: number): number
以Long情势获取当前行中指定列的值。 体系能力: SystemCapability.DistributedDataManager.RelationalStore.Core 参数:
参数名类型必填分析columnIndexnumber是指定的列索引,从0开始。 返回值:
类型分析number以Long情势返回指定列的值。
该接口支持的数据范围是:Number.MIN_SAFE_INTEGER ~ Number.MAX_SAFE_INTEGER,若超出该范围,发起利用getDouble。 错误码:
以下错误码的具体介绍请拜见关系型数据库错误码。 错误码ID错误信息14800013The column value is null or the column type is incompatible. 示例:
if(resultSet != undefined) {
const age = (resultSet as relationalStore.ResultSet).getLong((resultSet as relationalStore.ResultSet).getColumnIndex("AGE"));
}Copy to clipboardErrorCopied
复制代码
getDouble
getDouble(columnIndex: number): number
以double情势获取当前行中指定列的值。 体系能力: SystemCapability.DistributedDataManager.RelationalStore.Core 参数:
参数名类型必填分析columnIndexnumber是指定的列索引,从0开始。 返回值:
类型分析number以double情势返回指定列的值。 错误码:
以下错误码的具体介绍请拜见关系型数据库错误码。 错误码ID错误信息14800013The column value is null or the column type is incompatible. 示例:
if(resultSet != undefined) {
const salary = (resultSet as relationalStore.ResultSet).getDouble((resultSet as relationalStore.ResultSet).getColumnIndex("SALARY"));
}Copy to clipboardErrorCopied
复制代码
getAsset10+
getAsset(columnIndex: number): Asset
以Asset情势获取当前行中指定列的值。 体系能力: SystemCapability.DistributedDataManager.RelationalStore.Core 参数:
参数名类型必填分析columnIndexnumber是指定的列索引,从0开始。 返回值:
类型分析Asset以Asset情势返回指定列的值。 错误码:
以下错误码的具体介绍请拜见关系型数据库错误码。 错误码ID错误信息14800013The column value is null or the column type is incompatible. 示例:
if(resultSet != undefined) {
const doc = (resultSet as relationalStore.ResultSet).getAsset((resultSet as relationalStore.ResultSet).getColumnIndex("DOC"));
}Copy to clipboardErrorCopied
复制代码
getAssets10+
getAssets(columnIndex: number): Assets
以Assets情势获取当前行中指定列的值。 体系能力: SystemCapability.DistributedDataManager.RelationalStore.Core 参数:
参数名类型必填分析columnIndexnumber是指定的列索引,从0开始。 返回值:
类型分析Assets以Assets情势返回指定列的值。 错误码:
以下错误码的具体介绍请拜见关系型数据库错误码。 错误码ID错误信息14800013The column value is null or the column type is incompatible. 示例:
if(resultSet != undefined) {
const docs = (resultSet as relationalStore.ResultSet).getAssets((resultSet as relationalStore.ResultSet).getColumnIndex("DOCS"));
}Copy to clipboardErrorCopied
复制代码
isColumnNull
isColumnNull(columnIndex: number): boolean
检查当前行中指定列的值是否为null。 体系能力: SystemCapability.DistributedDataManager.RelationalStore.Core 参数:
参数名类型必填分析columnIndexnumber是指定的列索引,从0开始。 返回值:
类型分析boolean如果当前行中指定列的值为null,则返回true,否则返回false。 错误码:
以下错误码的具体介绍请拜见关系型数据库错误码。 错误码ID错误信息14800013The column value is null or the column type is incompatible. 示例:
if(resultSet != undefined) {
const isColumnNull = (resultSet as relationalStore.ResultSet).isColumnNull((resultSet as relationalStore.ResultSet).getColumnIndex("CODES"));