The Android implementation executes statements when it creates or upgrades a database
- 2020-06-07 05:17:06
- OfStack
The example in this article describes the statement that Android executes when it creates or upgrades a database. If it is creating or upgrading a database, use the constructor with the List parameter, which executes when the database is created or upgraded.
The specific program code is as follows:
import java.util.List;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class SimpleSQLiteOpenHelper extends SQLiteOpenHelper {
private static final int INIT_VERSION = 1;
/**
* The statement executed when a database is created or upgraded.
*/
private List<String> sqlStatementExed;
/**
* If you are creating or upgrading a database, use the tape List A constructor for a parameter.
*
* @param context
* to use to open or create the database
* @param name
* of the database file, or null for an in-memory database
* @param factory
* to use for creating cursor objects, or null for the default
* @param version
* number of the database (starting at 1); if the database is
* older, onUpgrade(SQLiteDatabase, int, int) will be used to
* upgrade the database; if the database is newer,
* onDowngrade(SQLiteDatabase, int, int) will be used to
* downgrade the database
*/
public SimpleSQLiteOpenHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
sqlStatementExed = null;
}
/**
* with SQL The constructor of the statement. this SQL The statement is executed when the database is created or upgraded.
*
* @param context
* to use to open or create the database
* @param name
* of the database file, or null for an in-memory database
* @param factory
* to use for creating cursor objects, or null for the default
* @param version
* number of the database (starting at 1); if the database is
* older, onUpgrade(SQLiteDatabase, int, int) will be used to
* upgrade the database; if the database is newer,
* onDowngrade(SQLiteDatabase, int, int) will be used to
* downgrade the database
* @param sqlStatementExed
* Statement that will be executed when a database is created or upgraded.
*/
public SimpleSQLiteOpenHelper(Context context, String name,
CursorFactory factory, int version, List<String> sqlStatementExed) {
super(context, name, factory, version);
this.sqlStatementExed = sqlStatementExed;
}
/**
* If you are creating or upgrading a database, use the tape List A constructor for a parameter.
* @param context
* to use to open or create the database
* @param name
* of the database file, or null for an in-memory database
* @param version
* number of the database (starting at 1); if the database is
* older, onUpgrade(SQLiteDatabase, int, int) will be used to
* upgrade the database; if the database is newer,
* onDowngrade(SQLiteDatabase, int, int) will be used to
* downgrade the database
*/
public SimpleSQLiteOpenHelper(Context context, String name, int version) {
super(context, name, null, version);
sqlStatementExed = null;
}
/**
* If you are creating or upgrading a database, use the tape List A constructor for a parameter.
* @param context
* to use to open or create the database
* @param name
* of the database file, or null for an in-memory database
*/
public SimpleSQLiteOpenHelper(Context context, String name) {
super(context, name, null, INIT_VERSION);
sqlStatementExed = null;
}
/**
* If you are creating or upgrading a database, use the tape List A constructor for a parameter.
*
* @param context
* to use to open or create the database
* @param name
* of the database file, or null for an in-memory database
* @param version
* number of the database (starting at 1); if the database is
* older, onUpgrade(SQLiteDatabase, int, int) will be used to
* upgrade the database; if the database is newer,
* onDowngrade(SQLiteDatabase, int, int) will be used to
* downgrade the database
* @param sqlCreateStatement
* Statement to execute when creating or upgrading a database.
*/
public SimpleSQLiteOpenHelper(Context context, String name, int version,
List<String> sqlCreateStatement) {
super(context, name, null, version);
this.sqlStatementExed = sqlCreateStatement;
}
/**
* @param context
* @param name
* @param sqlCreateStatement
* Statement to execute when creating or upgrading a database.
*/
public SimpleSQLiteOpenHelper(Context context, String name,
List<String> sqlCreateStatement) {
super(context, name, null, INIT_VERSION);
this.sqlStatementExed = sqlCreateStatement;
}
/*
* (non-Javadoc)
* @see
* android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
* .SQLiteDatabase)
*/
@Override
@Deprecated
public void onCreate(SQLiteDatabase db) {
exeSqlStatementExed(db);
}
/*
* (non-Javadoc)
* @see
* android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
* .SQLiteDatabase, int, int)
*/
@Override
@Deprecated
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion) {
exeSqlStatementExed(db);
}
}
/**
* Performed when the database is initialized or upgraded SQL Statement..
*/
private void exeSqlStatementExed(SQLiteDatabase db) {
if (sqlStatementExed != null) {
for (String statement : sqlStatementExed) {
db.execSQL(statement);
}
}
}
}
It is hoped that the method described in this article will be helpful for you to develop Android program.