In addition to using files or SharedPreferences to store data, you can also choose to use the SQLite database to store data. On the Android platform, an embedded relationship is integrated.
In addition to using files or SharedPreferences to store data, you can also choose to use the SQLite database to store data. On the Android platform, an embedded relationship is integrated.
In addition to using files or SharedPreferences to store data, you can also choose to use the SQLite database to store data.
On the Android platform, an embedded relational database-SQLite is integrated. SQLite3 supports NULL, INTEGER, REAL (floating point number), TEXT (string TEXT), and BLOB (binary object) data Type. Although only five types are supported, sqlite3 also accepts data types such as varchar (n), char (n), and decimal (p, s,
It is only converted to the corresponding five data types during operation or storage.
The biggest feature of SQLite is that you can save various types of data to any field without worrying about the data type declared by the field;
SQLite can also parse SQL statements that are not part of it, so it is easy to use;
In SQLite, why does the Android system implement the abstract class of SQLiteOpenHelper? So I will implement the method to allow us to manage the data and create it in version management;
In the help class of SQLiteOpenHelper,
Let's implementOnCreate (SQLiteDatabase db), onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion)These two methods
①:Public abstract void onCreate (SQLiteDatabase db)When the DataBase is created for the first time, the onCreate () method can generate the DataBase table structure and add the initialization data used by some applications, and return the DataBase
②:Public abstract void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion)Called when the database needs to be updated. You need to delete the original table or update the new version number before calling the database.
Of course we will also useGetReadableDatabase () and head WritableDatabase ()The two methods are used to obtain the data of an instance.
However, the getWritableDatabase () method opens the database in read/write mode. Once the disk space of the database is full, the database can only read but not write;
The getReadableDatabase () method first opens the database in read/write mode. If the disk space of the database is full, opening fails,
However, we were lucky to have failed to open the database. We still tried to open the database in read-only mode.
Demo: using student information management as an Example
Student_Model:
First, write the Database Help class:
At this point, the help class of the database has been basically completed, and the student information will be managed using methods in the SQLiteDatabase class;