Easy Tutorial
❮ Android Tutorial Handler Message Zookeeper Bs Command ❯

6.3.2 Data Storage and Access—SQLite Database Revisited

Category Android Basic Tutorial

Introduction to This Section:

>

After completing the previous section on the basic operations of SQLite in Android, you have already mastered them. In this section, we will learn some slightly more advanced topics, such as database transactions, how to store large binary data in the database, and how to handle database version upgrades! Alright, let's begin this section!


1. SQLite Transactions

In simple terms, all database operations written within a transaction must be successful for the transaction to be committed; otherwise, the transaction is rolled back, which means returning to the previous state—before the database operations were executed! Additionally, as we mentioned earlier, in the data/data/<package name>/database/ directory, besides the db file we created, there is also a xxx.db-journal file, which is a temporary log file generated to support transactions in the database!


2. SQLite Storage of Large Binary Files

Of course, we generally rarely store large binary files in the database, such as images, audio, video, etc. For these, we usually store the file paths. However, there will always be some peculiar requirements. One day, you may suddenly want to store these files in the database. Below, we take images as an example, save images to SQLite, and read images from SQLite!


3. Binding Database Data with SimpleCursorAdapter

Of course, this can be fun to play with, but it is not recommended for use, even though it is very simple to use! In fact, when we talked about ContentProvider, we used this tool to bind the contact list! Here, I will not write an example, but I will provide the core code directly! You can figure it out by yourself, and now we generally rarely write our own database code, usually through third-party frameworks: ormlite, greenDao, etc. In the advanced section, we will study again~


4. A Collection of Database Upgrade Tips

PS: Well, I haven't done this part, and it's always a lack of project experience. The company's products are all positioning, and I just looked at the company's project and found that the code left by the predecessors is: onCreate() creates the DB, and then onUpgrade() deletes the previous DB, and then calls the onCreate() method again! After looking at the code of several versions, I found that there is no database upgrade operation... There is nothing to learn from, and I can only refer to the practices of others. The following is a summary of some information I found after consulting materials. If there is anything wrong, please point it out. Some third-party frameworks may have already done this, and due to time constraints, I won't go into it slowly! If you know, you can leave a message, thank you!

1) What is database version upgrade? How to upgrade?

>

Answer: Suppose we have developed an APP that uses a database, let's assume this database version is v1.0. In this version, we created a database file x.db, and we created the first table, t_user, through the onCreate() method, which has two fields: _id, user_id. Later, we want to add a field user_name, and at this time, we need to modify the structure of the database table. We can put the operation of updating the database into the onUpgrade() method. We just need to change the version number when instantiating the custom SQLiteOpenHelper, for example, change 1 to 2, and it will automatically call the onUpgrade() method! In addition, for each database version, we should keep a corresponding record (document), similar to the following:

Database Version Android Corresponding Version Content
v1.0 1 The first version, containing two fields...
v1.1 2 Data retention, add user_name field

2) Some Doubts and Related Solutions

① Will the database file be deleted when the app is upgraded?

>

Answer: No! The data is still there!

② If I want to delete a field in the table or add a new field, is the original data still there?

>

Answer: Yes, it is!

③ You just mentioned that rough way of updating the database version, which does not retain data, can you post it?

>

Answer: Yes, here we use the third-party ormlite, and you can also write your own database creation and deletion code:

**④ For example, suppose we have already upgraded to the third version, and we added a table in the second version, and then the third version also added a table.

❮ Android Tutorial Handler Message Zookeeper Bs Command ❯