Data storage and access - see also SQLite database


Introduction to this section:

After studying the previous section, you have already mastered the basic operations of SQLite in Android, and in this section we will learn Some slightly more advanced things, database transactions, how to store large binary data into the database, and when upgrading the version How to handle the database! Okay, let’s start this section!


SQLite transaction

1.png

To put it simply: all database operations written in the transaction are successful and the transaction is committed , otherwise, the transaction rollback means going back to the previous point Status - when no database operation is performed! In addition, we also mentioned earlier that in the data/data/<package name>/database/ directory In addition to the db file we created, there is also a xxx.db-journal file, which is used to enable the database to support transactions. Temporary log files generated!


2.SQLite stores large binary files

Of course, generally we rarely store large binary files in the database, such as pictures, audio, videos, etc. For these we generally is the storage file path, but there are always some weird needs. One day you suddenly want to save these files into the database. Below we will use The picture is an example, save the picture to SQLite, and read the picture in SQLite!

2.png


3.SimpleCursorAdapter binds database data

Of course, this is fine for fun, but it is not recommended to use it, although it is very simple to use! In fact, when talking about ContentProvider, we used this thing to bind the contact list! I won’t write any examples here. Go directly to the core code! You can just tinker with it yourself if you need to. In addition, nowadays we rarely write database things by ourselves. , usually through third-party frameworks: ormlite, greenDao, etc. In the advanced part, we will learn again~

3.png


4. Some highlights of database upgrade

PS: Well, I have never done this before. I always have insufficient project experience. The company’s products are all positioning-based. I just visited the company. project, I found that the code left by predecessors is: onCreate() creates DB, then onUpgrade() deletes the previous DB, and then Call the onCreate() method again! After reading several versions of the code, I found that there is no database upgrade operation... I have nothing to learn from. I can only refer to other people's practices. Below are some conclusions from Xiaozhu's review of the information. If there is anything wrong, please point it out. Maybe some third-party frameworks have already done this, but due to time constraints, I won’t go into it slowly! You can leave a message if you know, thank you!

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

Answer: If we develop an APP and use a database, we assume that the database version is v1.0. In this version, we created an x.db database file, and we created the first table through the onCreate() method. t_user, there are two fields in it: _id, user_id; later we want to add a field user_name, this time We need to modify the structure of the database table, and we can update the database onUpgrade() In the method, we only need to modify the version number when instantiating the custom SQLiteOpenHelper, such as changing 1 to 2 In this way, the onUpgrade() method will be automatically called! In addition, for each database version we should do a good job The corresponding record (document) is similar to the following:

Database versionandoid corresponding versionContent
v1.01The first version contains two fields...
v1.12Data retention, new user_name field

2) Some questions and related solutions

① When the application is upgraded, will the database files be deleted?

Answer: No! The data and everything is there!

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

Answer: Yes!

③Can you post the crude way of updating the database version that you just mentioned without retaining the data?

Answer: Yes, the third-party ormlite is used here. You can also write the code for database creation and deletion yourself:

4.png

④For example, if we have upgraded to the third version, we add a table to the second version, and then the third version A table has also been added. Users can directly upgrade from the first version to the third version. In this way, without going through the second version, there will be no added table. How can this be broken?

Answer: It’s very simple. We can write a switch() in onUpgrade(). The structure is as follows:
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
            int arg2, int arg3) {
    switch(arg2){
        case 1:
            db.execSQL(第一个版本的建表语句);
        case 2:
            db.execSQL(第二个版本的建表语句);
        case 3:
            db.execSQL(第三个版本的建表语句); 
    }
}
If you are careful, you may find that break is not written here, so this is By the way, this is to ensure that when upgrading across versions, each time the database All modifications can be implemented! This ensures that the table structure is up to date! In addition, it is not necessarily a statement to create a table or modify the table structure. That’s okay too!


⑤The design of the old table is too bad, many fields need to be changed, and there are too many changes. I want to create a new table, but the table name must be the same

And some previous data needs to be saved to the new table!

Answer: Haha, I kneel down for you. Of course, there are solutions. Here are the ideas:

1. Rename the old table to a temporary table:

ALTER TABLE User RENAME TO _temp_User;

2. Create a new table:

CREATE TABLE User (u_id INTEGER PRIMARY KEY,u_name VARCHAR(20),u_age VARCHAR(4));

3. Import data;

INSERT INTO User SELECT u_id,u_name,"18" FROM _temp_User;

//If there is no original table, you need to set a default value yourself4. Delete the temporary table;

DROP TABLE_temp_User;

Summary of this section:

Okay, in this section we will discuss the SQLite Transactions, large binary storage, SimpleCursorAdapter and database upgrades Some issues have been explored, and about SQLite, we have learned so much for the time being, about the use of third parties, and We will study some advanced topics with you when we get to advanced levels~ That’s it for this section, thank you~