用鴻蒙HarmonyOS官方關(guān)系型數(shù)據(jù)庫(kù)API去讀取已存在的數(shù)據(jù)庫(kù)
想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
https://harmonyos.51cto.com/#zz
文章開始前先分享一個(gè)報(bào)錯(cuò):
錯(cuò)誤: 類重復(fù): com.harmony.onlineedict.ResourceTable
這個(gè)報(bào)錯(cuò)發(fā)生的現(xiàn)場(chǎng)是:我把DEVECO升級(jí)到2.0Beta后,我打開了一個(gè)在升級(jí)之前的項(xiàng)目,在構(gòu)建的時(shí)候就發(fā)生了這個(gè)報(bào)錯(cuò),一時(shí)讓人很迷茫。
迷茫中找到的解決方式是在Build中Clean Project,然后繼續(xù)構(gòu)建就OK了。
正文開始:
申明一下:這個(gè)內(nèi)容是學(xué)習(xí)了李寧老師課程的基礎(chǔ)上寫出來(lái)的,大家可以多多去和李寧老師學(xué)習(xí)哦,這個(gè)老師很硬核!
先把代碼放上來(lái),https://gitee.com/forQinzhikai/harmony-osapplication.git
其中的RdbStoreExample文件夾為該demo完整實(shí)例代碼,大家有什么問(wèn)題,歡迎留言交流
先說(shuō)一下,要寫的demo的大概邏輯,我會(huì)直接將一個(gè)事先已經(jīng)存入一定數(shù)據(jù)的sqlite db文件放入demo文件夾中,然后使用Harmony Developer提供的關(guān)系型數(shù)據(jù)庫(kù)的相關(guān)API去讀取之前放入的sqlite文件的內(nèi)容,然后展示出來(lái)
最后的效果如下:

該demo中操作關(guān)系型數(shù)據(jù)庫(kù)的大概邏輯。
- 1.將拷貝過(guò)來(lái)的sqlite文件進(jìn)行讀入應(yīng)用中
- 2.然后用harmonyOS提供的API去處理讀入的數(shù)據(jù)并進(jìn)行展示
1.將拷貝過(guò)來(lái)的sqlite文件夾讀入應(yīng)用
1.1首先將一個(gè)已經(jīng)存在的sqlite文件放入指定位置
指定位置為/src/main/resources/rawfile,對(duì),必須得這兒,數(shù)據(jù)庫(kù)文件中的內(nèi)容如下:
1.2 然后將讀取上一步操作中放入的sqlite文件,將其讀入本應(yīng)用的所能識(shí)別的空間中(暫時(shí)先這么理解,反正只有這樣做,你才能讀取到)
具體的讀取過(guò)程,我創(chuàng)建了一個(gè)文件:readSqliteFile.java(見文章最后)
這份代碼中還涉及到了封裝打開數(shù)據(jù)庫(kù)和打開數(shù)據(jù)的操作,這一節(jié)只說(shuō)一下讀取上一步拷貝文件的過(guò)程。
首先通過(guò)下面兩行,指定讀取的數(shù)據(jù)要存入的位置:dbPath。
- dirPath = new File(context.getDataDir().toString() + "/MainAbility/databases/db");
- dbPath = new File(Paths.get(dirPath.toString(),"PremierLeague.sqlite").toString());
然后通過(guò)下面一行打開剛才我們放入的sqlite文件:resource
- Resource resource = context.getResourceManager().getRawFileEntry("resources/rawfile/PremierLeague.sqlite").openRawFile();
然后讀取resoruce寫入dbPath
- FileOutputStream fos = new FileOutputStream(dbPath);
- byte[] buffer = new byte[4096];
- int count = 0;
- while((count = resource.read(buffer)) >= 0){
- fos.write(buffer,0,count);
- }
2.然后用harmonyOS提供的API去處理讀入的數(shù)據(jù)并進(jìn)行展示
這一塊的代碼也在上一節(jié)展示的readSqliteFile.java文件中。在這里我們用到的是官方提供的數(shù)據(jù)管理模塊中關(guān)系型數(shù)據(jù)庫(kù)的API,鏈接:https://developer.harmonyos.com/cn/docs/documentation/doc-guides/database-relational-overview-0000000000030046,這個(gè)API是RdbSotre系列。
2.1 配置RdbStore
定義一個(gè)RdbStore的全局變量
- private RdbStore store;
定義需要的StoreConfig配置文件,可以看到配置文件中指定的數(shù)據(jù)庫(kù)的名字,這里是指的剛才寫入的dbPath中的。
- private StoreConfig config = StoreConfig.newDefaultConfig("PremierLeague.sqlite");
定義配置需要的回調(diào)函數(shù),這里我們還用不上,所以先用空的就行
- private static final RdbOpenCallback callback = new RdbOpenCallback() {
- @Override
- public void onCreate(RdbStore rdbStore) {
- }
- @Override
- public void onUpgrade(RdbStore rdbStore, int i, int i1) {
- }
- };
2.2 打開RdbStore
首先得new一個(gè) DatabaseHelper
- DatabaseHelper helper = new DatabaseHelper(context);
然后從new出的DatabaseHelper調(diào)用getRdbStore獲得RdbStore對(duì)象
- store = helper.getRdbStore(config,1,callback,null);
2.3 從上一步打開的RdbStore中進(jìn)行查詢
首先使用querySql傳入sql語(yǔ)句進(jìn)行查詢
- ResultSet resultSet = store.querySql("select * from team",null);
然后使用ResultSet類的goToNextRow()進(jìn)行讀取
- while(resultSet.goToNextRow()){
- sqliteData sqldata = new sqliteData();
- sqldata.no = resultSet.getInt(0);
- sqldata.clubName = resultSet.getString(1);
- result.add(sqldata);
- }
3.然后就得到了數(shù)據(jù)庫(kù)文件想要的數(shù)據(jù),這里將其存入了ArrayList,然后在需要的地方去遍歷它就可以嘍
下面附上readSqliteFile.java文件代碼,整個(gè)demo從文章開頭給出的gitee地址去下載就可以了!
- package com.harmony.rdbstoreexample;
- import ohos.app.AbilityContext;
- import ohos.data.DatabaseHelper;
- import ohos.data.rdb.RdbOpenCallback;
- import ohos.data.rdb.RdbStore;
- import ohos.data.rdb.StoreConfig;
- import ohos.data.resultset.ResultSet;
- import ohos.global.resource.Resource;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- public class readSqliteFile {
- private AbilityContext context;
- private File dirPath;
- private File dbPath;
- private RdbStore store;
- private StoreConfig config = StoreConfig.newDefaultConfig("PremierLeague.sqlite");
- private static final RdbOpenCallback callback = new RdbOpenCallback() {
- @Override
- public void onCreate(RdbStore rdbStore) {
- }
- @Override
- public void onUpgrade(RdbStore rdbStore, int i, int i1) {
- }
- };
- public readSqliteFile(AbilityContext context)
- {
- this.context = context;
- dirPath = new File(context.getDataDir().toString() + "/MainAbility/databases/db");
- if(!dirPath.exists()){
- dirPath.mkdirs();
- }
- dbPath = new File(Paths.get(dirPath.toString(),"PremierLeague.sqlite").toString());
- }
- private void extractDB() throws IOException{
- Resource resource = context.getResourceManager().getRawFileEntry("resources/rawfile/PremierLeague.sqlite").openRawFile();
- if(dbPath.exists()){
- dbPath.delete();
- }
- FileOutputStream fos = new FileOutputStream(dbPath);
- byte[] buffer = new byte[4096];
- int count = 0;
- while((count = resource.read(buffer)) >= 0){
- fos.write(buffer,0,count);
- }
- resource.close();
- fos.close();
- }
- public void init() throws IOException{
- extractDB();
- DatabaseHelper helper = new DatabaseHelper(context);
- store = helper.getRdbStore(config,1,callback,null);
- }
- public ArrayList search(){
- ResultSet resultSet = store.querySql("select * from team",null);
- ArrayList result = new ArrayList();
- while(resultSet.goToNextRow()){
- sqliteData sqldata = new sqliteData();
- sqldata.no = resultSet.getInt(0);
- sqldata.clubName = resultSet.getString(1);
- result.add(sqldata);
- }
- resultSet.close();
- return result;
- }
- }
©著作權(quán)歸作者和HarmonyOS技術(shù)社區(qū)共同所有,如需轉(zhuǎn)載,請(qǐng)注明出處,否則將追究法律責(zé)任
想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
https://harmonyos.51cto.com/#zz