源碼下載(免下載積分):下載
對於儲存重複或者結構化的資料資料,使用資料庫很好的。在android對於資料庫的API在
android.database.sqlite包中。
建立並操作資料庫:
建立:
1. 繼承SQLiteOpenHelper
//繼承SQLiteOpenHelper類,public class DictionaryOpenHelper extends SQLiteOpenHelper{
2. 定義相關的成員變數和常量
public static final String DABASENAME = "dictionary"; private static final int DATABASE_VERSION = 1; private SQLiteDatabase mDatabase; private Context mContext; //定義table相關的內容 /* * 實現BaseColumns介面,內部類中會有一個關鍵字_ID。android中的一些類希望有_ID, *例如cursor,但它不是必須的,但能夠協助資料庫和android架構更好的協調 */ public static class DictionaryEntry implements BaseColumns { public static final String TAB_NAME = "words"; public static final String COLUMN_WORD = "word"; public static final String COLUMN_DEFINATION = "defination"; } //用於建立資料庫 private static final String SQL_CREATE_ENTRIES = "CREATE TABLE " +DictionaryEntry.TAB_NAME+"("+DictionaryEntry._ID +" INTEGER PRIMARY KEY,"+DictionaryEntry.COLUMN_WORD+"," +DictionaryEntry.COLUMN_DEFINATION + ")"; public DictionaryOpenHelper(Context context) { super(context, DABASENAME, null, DATABASE_VERSION); }
3. 實現SQLiteOpenHelper的相關的函數
//當第一建立資料庫時會被調用 @Override public void onCreate(SQLiteDatabase db) { // db.execSQL(SQL_CREATE_ENTRIES); //讀取檔案並添加多行 loadDictionary(); } private void loadDictionary() { new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub loadWords(); } }); } private void loadWords() { //讀取資源 final Resources resources = mContext.getResources(); InputStream inputStream = resources.openRawResource(R.raw.definitions); Scanner scanner = new Scanner(inputStream); while(scanner.hasNextLine()) { String string = scanner.nextLine(); String[] strings = string.split("-"); if (strings.length < 2) { continue; } loadWord(strings[0].trim(), strings[1].trim()); } } //添加行 private void loadWord(String word,String defination) { ContentValues contentValues = new ContentValues(); contentValues.put(DictionaryEntry.COLUMN_WORD, DictionaryEntry.COLUMN_DEFINATION); mDatabase.insert(DictionaryEntry.TAB_NAME, null, contentValues); } //當資料庫升級的時候會被調用 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub db.execSQL(SQL_CREATE_ENTRIES); onCreate(db); }
注意:
- 資料存放區在內部儲存中,資料是安全的,因為其他的應用程式不能夠訪問資料庫
- 在調用getWritableDatabase() or getReadableDatabase()來操作長時間的操作,
要使用後台線程,例如AsyncTask或者IntentService
操作
- 資訊存入資料庫
//1. 建立ContentValues對象並添加資訊 ContentValues contentValues = new ContentValues(); contentValues.put(DictionaryEntry.COLUMN_WORD, DictionaryEntry.COLUMN_DEFINATION); //2. 添加到資料庫中 mDatabase.insert(DictionaryEntry.TAB_NAME, null, contentValues);
-
從資料庫中讀取資料
使用query()方法來檢索資料,並返回一個Cursor對象
mDatabase.query( table, //table名 columns, //返回的關鍵字 selection, //where語句的關鍵字 selectionArgs, //where的值 groupBy, //用於聲明怎麼去組織行 having, //用於聲明那個行組應該包含到Cursor中 orderBy); //怎麼哪個關鍵字排序
-
從資料庫中刪除資料
從資料庫表中刪除資料,需要指定selection,用於指定要刪除哪一行,
mDatabase.delete(table, //table名 whereClause, //用於操作的WHERE子句 whereArgs); //WHERE子句的參數
-
從資料庫中更新資料
當需要修改資料庫表中的資料時,使用update()方法
mDatabase.update(table, //要更新的table名 values, //ContentValues的對象,用於指定更新的資料 whereClause, //where子句,用於指定哪一個行(記錄)需要修改 whereArgs); //where子句的值
例子並未添加所有操作。
資料庫的調試:
Android SDK中包含一個工具是sqlite3,這個工具能夠查看資料庫表中內容,使用這個工具只需簡單的命令列
~$ adb devices
List of devices attached
emulator-5554 device
~$ adb -s emulator-5554 shell
#sqlite3 /data/data/com.example.mydictionary/databases/dictionary
sqlite> .table
以後就能根據資料相關的命令查看資料庫表中的資料了,
參考資料:
http://developer.android.com/training/basics/data-storage/databases.html http://developer.android.com/guide/topics/data/data-storage.html