編輯:Android 開發實例
本文介紹一個實用技巧:在 Android 建立或升級資料庫時,如何一併執行自訂的 SQL 語句。關鍵在於——若是要處理資料庫的建立或升級,請採用帶有 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 程式開發有所幫助。
Android 沉浸式狀態欄實戰:讓狀態欄換上你要的顏色
一、概述近來注意到新版 QQ 採用了沉浸式狀態欄的設計。先說結論,本篇要達成的效果如下圖:關於這個「狀態欄變色」到底該叫 Immersive Mode 還是 Transl
Android 接入新浪微博、騰訊微博與人人網的 OAuth 2.0 實作
許多 Android 應用會需要接入社交平台的分享、登入或發文能力。本文以新浪微博、騰訊微博與人人網為例,說明整合時最關鍵的授權流程,以及如何取得後續呼叫 API 所需的
手把手做 Android 登入頁:表單、驗證與錯誤嘗試的處理
登入畫面是許多 App 的第一道門。使用者要嘛成功進入,要嘛被擋在門外——而這中間的體驗細節,往往決定了對這個 App 的第一印象。本文從版面配置寫到程式邏輯,做出一個具
Android 本地化入門:用 values-語系 資料夾做好多國語言支援
一支 App 上線後,使用者可能來自任何國家。若畫面上永遠只有一種語言,對不熟悉該語言的人來說形同不可用。本地化要解決的就是這個問題:讓 App 依裝置的語言環境,自動呈