本文實(shí)例講述了Android創(chuàng)建或升級(jí)數(shù)據(jù)庫(kù)時(shí)執(zhí)行的語(yǔ)句,如果是創(chuàng)建或升級(jí)數(shù)據(jù)庫(kù),請(qǐng)使用帶List參數(shù)的構(gòu)造方法,帶SQL語(yǔ)句的構(gòu)造方法將在數(shù)據(jù)庫(kù)創(chuàng)建或升級(jí)時(shí)執(zhí)行。
具體程序代碼如下:
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; /** * 創(chuàng)建或升級(jí)數(shù)據(jù)庫(kù)時(shí)執(zhí)行的語(yǔ)句。 */ private List<String> sqlStatementExed; /** * 如果是創(chuàng)建或升級(jí)數(shù)據(jù)庫(kù),請(qǐng)使用帶List參數(shù)的構(gòu)造方法。 * * @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; } /** * 帶SQL語(yǔ)句的構(gòu)造方法。此SQL語(yǔ)句將在數(shù)據(jù)庫(kù)創(chuàng)建或升級(jí)時(shí)執(zhí)行。 * * @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 * 在數(shù)據(jù)庫(kù)創(chuàng)建或升級(jí)的時(shí)候?qū)?zhí)行的語(yǔ)句。 */ public SimpleSQLiteOpenHelper(Context context, String name, CursorFactory factory, int version, List<String> sqlStatementExed) { super(context, name, factory, version); this.sqlStatementExed = sqlStatementExed; } /** * 如果是創(chuàng)建或升級(jí)數(shù)據(jù)庫(kù),請(qǐng)使用帶List參數(shù)的構(gòu)造方法。 * @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; } /** * 如果是創(chuàng)建或升級(jí)數(shù)據(jù)庫(kù),請(qǐng)使用帶List參數(shù)的構(gòu)造方法。 * @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; } /** * 如果是創(chuàng)建或升級(jí)數(shù)據(jù)庫(kù),請(qǐng)使用帶List參數(shù)的構(gòu)造方法。 * * @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 * 在創(chuàng)建或升級(jí)數(shù)據(jù)庫(kù)時(shí)要執(zhí)行的語(yǔ)句。 */ 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 * 在創(chuàng)建或升級(jí)數(shù)據(jù)庫(kù)時(shí)要執(zhí)行的語(yǔ)句。 */ 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); } } /** * 初始化或升級(jí)數(shù)據(jù)庫(kù)時(shí)執(zhí)行的SQL語(yǔ)句。。 */ private void exeSqlStatementExed(SQLiteDatabase db) { if (sqlStatementExed != null) { for (String statement : sqlStatementExed) { db.execSQL(statement); } } }}
希望本文所述方法對(duì)于大家進(jìn)行Android程序開(kāi)發(fā)能夠起到一定的幫助作用。
新聞熱點(diǎn)
疑難解答
圖片精選