In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to use relational database API to read existing databases". In daily operation, I believe many people have doubts about how to use relational database API to read existing databases. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "how to use relational database API to read existing databases". Next, please follow the editor to study!
The effect is as follows:
The general logic for operating a relational database in this demo.
-1. Read the copied sqlite file into the application
-2. Then use the API provided by harmonyOS to process the read data and display it.
1. Read the copied sqlite folder into the application
1.1First, put an existing sqlite file in the specified location
Specify the location as / src/main/resources/rawfile, yes, it has to be here, the contents of the database file are as follows:
1.2 then read the sqlite file you put in the previous operation into the recognizable space of this application (understand this for the time being, but only by doing so can you read it)
For the specific reading process, I created a file: readSqliteFile.java (see the end of the article)
This code also involves encapsulating the operation of opening the database and opening the data. This section only talks about the process of reading the file copied in the previous step.
First, specify the location where the read data will be stored through the following two lines: dbPath.
DirPath = new File (context.getDataDir (). ToString () + "/ MainAbility/databases/db"); dbPath = new File (Paths.get (dirPath.toString (), "PremierLeague.sqlite") .toString ())
Then open the sqlite file we just put in with the following line: resource
Resource resource = context.getResourceManager () .getRawFileEntry ("resources/rawfile/PremierLeague.sqlite") .openRawFile ()
Then read resoruce and write to 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);}
two。 Then use the API provided by harmonyOS to process the read data and display it.
The code for this piece is also shown in the readSqliteFile.java file shown in the previous section. Here we use the API of the relational database in the official data management module, link: https://developer.harmonyos.com/cn/docs/documentation/doc-guides/database-relational-overview-0000000000030046, this API is the RdbSotre series.
2.1 configure RdbStore
Define a global variable for RdbStore
Private RdbStore store
Define the required StoreConfig configuration file, and you can see the name of the database specified in the configuration file, which refers to the dbPath you just wrote.
Private StoreConfig config = StoreConfig.newDefaultConfig ("PremierLeague.sqlite")
Define the callback function required by the configuration, which we don't need here, so just use the empty one first.
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 Open RdbStore
First, you have to new a DatabaseHelper.
DatabaseHelper helper = new DatabaseHelper (context)
Then call getRdbStore from the DatabaseHelper out of new to get the RdbStore object
Store = helper.getRdbStore (config,1,callback,null)
2.3 query from the RdbStore opened in the previous step
First use querySql to pass in the sql statement to query
ResultSet resultSet = store.querySql ("select * from team", null)
Then use the goToNextRow () of the ResultSet class to read
While (resultSet.goToNextRow ()) {sqliteData sqldata = new sqliteData (); sqldata.no = resultSet.getInt (0); sqldata.clubName = resultSet.getString (1); result.add (sqldata);}
3. Then you get the data that the database file wants, here you store it in ArrayList, and then you can traverse it where you need it.
The readSqliteFile.java file code is attached below, and the entire demo can be downloaded from the gitee address given at the beginning of the article.
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;}} at this point, the study on "how to use relational database API to read existing databases" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.