国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android實(shí)現(xiàn)創(chuàng)建或升級數(shù)據(jù)庫時執(zhí)行語句

2020-04-11 11:45:20
字體:
供稿:網(wǎng)友

本文實(shí)例講述了Android創(chuàng)建或升級數(shù)據(jù)庫時執(zhí)行的語句,如果是創(chuàng)建或升級數(shù)據(jù)庫,請使用帶List參數(shù)的構(gòu)造方法,帶SQL語句的構(gòu)造方法將在數(shù)據(jù)庫創(chuàng)建或升級時執(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)建或升級數(shù)據(jù)庫時執(zhí)行的語句。 */ private List<String> sqlStatementExed; /** * 如果是創(chuàng)建或升級數(shù)據(jù)庫,請使用帶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語句的構(gòu)造方法。此SQL語句將在數(shù)據(jù)庫創(chuàng)建或升級時執(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ù)庫創(chuàng)建或升級的時候?qū)?zhí)行的語句。 */ public SimpleSQLiteOpenHelper(Context context, String name,  CursorFactory factory, int version, List<String> sqlStatementExed) { super(context, name, factory, version); this.sqlStatementExed = sqlStatementExed; } /** * 如果是創(chuàng)建或升級數(shù)據(jù)庫,請使用帶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)建或升級數(shù)據(jù)庫,請使用帶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)建或升級數(shù)據(jù)庫,請使用帶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)建或升級數(shù)據(jù)庫時要執(zhí)行的語句。 */ 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)建或升級數(shù)據(jù)庫時要執(zhí)行的語句。 */ 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); } } /** * 初始化或升級數(shù)據(jù)庫時執(zhí)行的SQL語句。。 */ private void exeSqlStatementExed(SQLiteDatabase db) { if (sqlStatementExed != null) {  for (String statement : sqlStatementExed) {  db.execSQL(statement);  } } }}

希望本文所述方法對于大家進(jìn)行Android程序開發(fā)能夠起到一定的幫助作用。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 吉安市| 广元市| 迁西县| 永靖县| 那曲县| 兴隆县| 天台县| 肥西县| 嘉定区| 抚松县| 古田县| 龙川县| 临颍县| 昌乐县| 海宁市| 吉木乃县| 务川| 浏阳市| 宜兴市| 宜良县| 阜宁县| 郴州市| 唐海县| 桦甸市| 大埔县| 新疆| 吴川市| 阳城县| 怀宁县| 越西县| 乳源| 临猗县| 青海省| 延长县| 左贡县| 泸水县| 张家界市| 湖州市| 垫江县| 汤原县| 华阴市|