Android-related SQLite usage and database version upgrades
SQLite is a frequently used data storage method in Android and is generally used to store temporary data locally. When the application version is upgraded, the database version must also be upgraded.
Public class DBHelper extends SQLiteOpenHelper {
Private static final int DATABASE_VERSION = 1;
String SQL = create table if not exists my_product + (id integer primary key, category varchar, biaoqian varchar, +
BgState integer, bqState Integer );
Public DBHelper (Context context, String name, CursorFactory factory,
Int version ){
Super (context, name, factory, version );
// TODO Auto-generated constructor stub
}
Public DBHelper (Context context, String name, CursorFactory factory,
Int version, DatabaseErrorHandler errorHandler ){
Super (context, name, factory, version, errorHandler );
// TODO Auto-generated constructor stub
}
@ Override
Public void onCreate (SQLiteDatabase db ){
// TODO Auto-generated method stub
Db.exe cSQL (SQL );
}
@ Override
Public void onUpgrade (SQLiteDatabase db, int oldVision, int newVision ){
Log. I (Allen, perform Database Upgrade );
// TODO Auto-generated method stub
Db. delete (my_product, null, null );
Db.exe cSQL (SQL );
}
}
The above code is a simple sqlite application. databaseHelper must inherit the SQLiteOpenHelper class. Generally, at least two methods must be implemented. One is the onCreate method, which is called when the databaseHelper object is created. The preceding SQL statement contains a database table. Another method to be implemented is the onUpgrade method, which is called when the version number changes. The above means to delete the original database table and recreate a table.
When used in Activity,
Private static String DATABASE_NAME = shipin. db;
Private static String TABLE_NAME = my_product;
DBHelper dbHelper = new DBHelper (this, DATABASE_NAME, null, 2 );
SQLiteDatabase db = dbHelper. getWritableDatabase ();
Cursor cursor = db. query (TABLE_NAME, new String [] {
Biaoqian, bgState, bqState}, category = ?, New String [] {cap}, null );
// Move the cursor to the next row to determine whether there is any data in the result set. If yes, true is returned. If no, false is returned.
While (cursor. moveToNext ()){
Biaoqian = cursor. getString (cursor. getColumnIndex (biaoqian); // label text
BgState = cursor. getInt (cursor. getColumnIndex (bgState); // specifies whether the background status has been used.
BqState = cursor. getInt (cursor. getColumnIndex (bqState); // tag state, used to distinguish the background color of a tag
List_biaoqian.add (biaoqian );
ListImageState. add (bgState );
ListBg. add (bqState );
}
The preceding method queries data from the database and adds the data that meets the requirements to the list.
Cursor cursor = db. query (TABLE_NAME, new String [] {
Biaoqian, bgState, bqState}, category = ?, New String [] {cap}, null );
This is a specific query statement. TABLE_NAME is the database table name, and new String [] is the columns to be queried. category =? It is a query condition, which is equivalent to where. The new String [] next to it is a specific query condition. This statement queries the values of biaoqian, bgState, and bqState of category = cap.
DBHelper dbHelper = new DBHelper (this, DATABASE_NAME, null, 2); parameter 2 in this sentence,
Is the database version number. When creating a database table, the default data version is 1. The output parameter is 2. The version is upgraded,
The onUpgrade method is executed. Note that the database version can only be upgraded or changed, but cannot be downgraded.
Otherwise, an error is reported.
Ps: at work, the application version is upgraded, the database version is not upgraded, and the table structure is changed,
The upgraded application is unavailable.