Android實現建立或升級資料庫時執行語句_Android

來源:互聯網
上載者:User

本文執行個體講述了Android建立或升級資料庫時執行的語句,如果是建立或升級資料庫,請使用帶List參數的構造方法,帶SQL語句的構造方法將在資料庫建立或升級時執行。

具體程式碼如下:

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; /** * 建立或升級資料庫時執行的語句。 */ private List<String> sqlStatementExed; /** * 如果是建立或升級資料庫,請使用帶List參數的構造方法。 *  * @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語句的構造方法。此SQL語句將在資料庫建立或升級時執行。 *  * @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 *      在資料庫建立或升級的時候將執行的語句。 */ public SimpleSQLiteOpenHelper(Context context, String name,  CursorFactory factory, int version, List<String> sqlStatementExed) { super(context, name, factory, version); this.sqlStatementExed = sqlStatementExed; } /** * 如果是建立或升級資料庫,請使用帶List參數的構造方法。 * @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; } /** * 如果是建立或升級資料庫,請使用帶List參數的構造方法。 * @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; } /** * 如果是建立或升級資料庫,請使用帶List參數的構造方法。 *  * @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 *      在建立或升級資料庫時要執行的語句。 */ 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 *      在建立或升級資料庫時要執行的語句。 */ 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); } } /** * 初始化或升級資料庫時執行的SQL語句。。 */ private void exeSqlStatementExed(SQLiteDatabase db) { if (sqlStatementExed != null) {  for (String statement : sqlStatementExed) {  db.execSQL(statement);  } } }}

希望本文所述方法對於大家進行Android程式開發能夠起到一定的協助作用。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.