diff --git a/zh-cn/application-dev/database/database-relational-guidelines.md b/zh-cn/application-dev/database/database-relational-guidelines.md index 7ad8f8cc654456ed69a8ee90aa6551db1f7ced72..2af7038c8dcd8b018d22ace518cf5c4ac8038291 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