From f3b6db4700f9bcd4fc67c80d440df99b800298bd Mon Sep 17 00:00:00 2001 From: ltdong Date: Fri, 16 Sep 2022 12:55:41 +0800 Subject: [PATCH] =?UTF-8?q?rdb=E5=BC=80=E5=8F=91=E6=8C=87=E5=8D=97?= =?UTF-8?q?=E5=90=8C=E6=AD=A5FA=E6=A8=A1=E5=9E=8B=E8=8E=B7=E5=8F=96context?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ltdong --- .../database-relational-guidelines.md | 41 ++++++++++++++++--- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/zh-cn/application-dev/database/database-relational-guidelines.md b/zh-cn/application-dev/database/database-relational-guidelines.md index 7ad8f8cc65..2af7038c8d 100644 --- a/zh-cn/application-dev/database/database-relational-guidelines.md +++ b/zh-cn/application-dev/database/database-relational-guidelines.md @@ -197,16 +197,45 @@ (3) 创建数据库。 示例代码如下: + FA模型示例: ```js + import data_rdb from '@ohos.data.rdb' - - const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)"; - const STORE_CONFIG = { name: "rdbstore.db" } - data_rdb.getRdbStore(this.context, STORE_CONFIG, 1, function (err, rdbStore) { + // 获取context + import featureAbility from '@ohos.ability.featureAbility' + var context = featureAbility.getContext() + + const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)"; + + const STORE_CONFIG = { name: "RdbTest.db" } + data_rdb.getRdbStore(context, STORE_CONFIG, 1, function (err, rdbStore) { rdbStore.executeSql(CREATE_TABLE_TEST) console.info('create table done.') - }) + }) + + ``` + Stage模型示例: + ```js + + import data_rdb from '@ohos.data.rdb' + // 获取context + import Ability from '@ohos.application.Ability' + var context + class MainAbility extends Ability{ + onWindowStageCreate(windowStage){ + context = this.context + } + } + + const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)"; + + const STORE_CONFIG = { name: "rdbstore.db" } + data_rdb.getRdbStore(context, STORE_CONFIG, 1, function (err, rdbStore) { + rdbStore.executeSql(CREATE_TABLE_TEST) + console.info('create table done.') + }) + ``` 2. 插入数据。 @@ -395,4 +424,4 @@ ## 相关实例 针对关系型数据库开发,有以下相关实例可供参考: - [`DistributedRdb`:分布式关系型数据库(eTS)(API8)(Full SDK)](https://gitee.com/openharmony/applications_app_samples/tree/master/data/DistributedRdb) -- [关系型数据库(JS)(API8)](https://gitee.com/openharmony/codelabs/tree/master/Data/JSRelationshipData) \ No newline at end of file +- [关系型数据库(JS)(API8)](https://gitee.com/openharmony/codelabs/tree/master/Data/JSRelationshipData)d \ No newline at end of file -- GitLab