Data Ability关系型数据库练习 原创

鸿蒙时代
发布于 2021-3-5 17:16
浏览
1收藏

一、具体场景

主要参照官方示范案例进行的操作练习。最后有完整的跑通的代码供大家参考。练习场景为数据库的交互-后台打印信息。

二、呈现的效果:

  Data Ability关系型数据库练习-鸿蒙开发者社区

三、部分代码说明 

“entry > src > main > java > com.xxx.xxx” 选择“File > New > Ability > Empty Data Ability” ,设置“Data Name”为PersonDataAbility并完成创建此时,HUAWEI DevEco Studio将自动生成类PersonDataAbility及相关方法,其继承类Ability(默认实现了数据库的增/删/改/查API方法),部分示例代码如下:

PersonDataAbility中

package com.example.jltfshujuku;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.content.Intent;
import ohos.data.DatabaseHelper;
import ohos.data.dataability.DataAbilityUtils;
import ohos.data.rdb.*;
import ohos.data.resultset.ResultSet;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;
import ohos.utils.PacMap;

import java.io.FileDescriptor;

public class PersonDataAbility extends Ability {

    /*定义Data Ability数据库相关常量*/
    private static final String DB_NAME = "persondataability.db";

    private static final String DB_TAB_NAME = "person";

    private static final String DB_COLUMN_PERSON_ID = "id";

    private static final String DB_COLUMN_NAME = "name";

    private static final String DB_COLUMN_GENDER = "gender";

    private static final String DB_COLUMN_AGE = "age";

    private static final int DB_VERSION = 1;

  /*  创建关系型数据库*/
    private StoreConfig config = StoreConfig.newDefaultConfig(DB_NAME);

    private RdbStore rdbStore;

    private RdbOpenCallback rdbOpenCallback = new RdbOpenCallback() {
        @Override
        public void onCreate(RdbStore store) {
            store.executeSql("create table if not exists "
                    + DB_TAB_NAME + " ("
                    + DB_COLUMN_PERSON_ID + " integer primary key, "
                    + DB_COLUMN_NAME + " text not null, "
                    + DB_COLUMN_GENDER + " text not null, "
                    + DB_COLUMN_AGE + " integer)");
        }

        @Override
        public void onUpgrade(RdbStore store, int oldVersion, int newVersion) {
        }
    };


    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        HiLog.info(LABEL_LOG, "PersonDataAbility onStart");
        /*初始化数据库连接*/
        DatabaseHelper databaseHelper = new DatabaseHelper(this);
        rdbStore = databaseHelper.getRdbStore(config, DB_VERSION, rdbOpenCallback, null);
    }
    /*重写数据库操作方法*/
    @Override
    public ResultSet query(Uri uri, String[] columns, DataAbilityPredicates predicates) {
        /*return null;*/
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates, DB_TAB_NAME);
        ResultSet resultSet = rdbStore.query(rdbPredicates, columns);
        if (resultSet == null) {
            HiLog.info(LABEL_LOG, "resultSet is null");
        }
        return resultSet;
    }

    @Override
    public int insert(Uri uri, ValuesBucket value) {
        HiLog.info(LABEL_LOG, "PersonDataAbility insert");
        /*return 999;*/
        String path = uri.getLastPath();
        if (!"person".equals(path)) {
            HiLog.info(LABEL_LOG, "DataAbility insert path is not matched");
            return -1;
        }
        ValuesBucket values = new ValuesBucket();
        values.putInteger(DB_COLUMN_PERSON_ID, value.getInteger(DB_COLUMN_PERSON_ID));
        values.putString(DB_COLUMN_NAME, value.getString(DB_COLUMN_NAME));
        values.putString(DB_COLUMN_GENDER, value.getString(DB_COLUMN_GENDER));
        values.putInteger(DB_COLUMN_AGE, value.getInteger(DB_COLUMN_AGE));
        int index = (int) rdbStore.insert(DB_TAB_NAME, values);
        DataAbilityHelper.creator(this, uri).notifyChange(uri);
        return index;
    }

    @Override
    public int delete(Uri uri, DataAbilityPredicates predicates) {
        /*return 0;*/
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates, DB_TAB_NAME);
        int index = rdbStore.delete(rdbPredicates);
        HiLog.info(LABEL_LOG, "delete: " + index);
        DataAbilityHelper.creator(this, uri).notifyChange(uri);
        return index;
    }

    @Override
    public int update(Uri uri, ValuesBucket value, DataAbilityPredicates predicates) {
      /*  return 0;*/
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates, DB_TAB_NAME);
        int index = rdbStore.update(value, rdbPredicates);
        HiLog.info(LABEL_LOG, "update: " + index);
        DataAbilityHelper.creator(this, uri).notifyChange(uri);
        return index;
    }

    @Override
    public FileDescriptor openFile(Uri uri, String mode) {
        return null;
    }

    @Override
    public String[] getFileTypes(Uri uri, String mimeTypeFilter) {
        return new String[0];
    }

    @Override
    public PacMap call(String method, String arg, PacMap extras) {
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }
}

 

四、后续计划,数据库的数据用日志的形式在后台打印出来的效果完成后,需要进一步尝试前后端数据的交互。

 

五、代码地址

https://gitee.com/jltfcloudcn/jump_to/commit/b460666e2997ce284bb5e1ade0801344e045d246 

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
标签
2
收藏 1
回复
举报
1条回复
按时间正序
/
按时间倒序
张荣超_九丘教育
张荣超_九丘教育

👍👍👍

回复
2021-3-5 21:07:36
回复
    相关推荐