- 嵌入式資料庫引擎,免設定、免管理,適合內嵌於其他軟體
- 整個資料庫就是一個檔案 ,可在不同環境裡移動與複製
- 體積小 ,適用於資源有限的系統
- 最重要的是,完全免費
在 Android 裡使用 SQLite 最重要的一個 class 就是 SQLiteOpenHelper,只要繼承 SQLiteOpenHelper 並定義 onCreate(...) 與 onUpgrade(...) 就可以了。
public class ActionLogHelper extends SQLiteOpenHelper { private static final String TAG = "ActionLog"; // 當 DB_FILE 指定的檔案不存在時 // SQLite 會呼叫 onCreate(...) 來建立所需要的 table private static final String DB_FILE = "actionlog.db"; // 當資料庫結構有修改時,DB_VERSION要加 1 // SQLite 才會呼叫 onUpgrade(...) 來修改資料庫 private static final int DB_VERSION = 1; public static final String TABLE_NAME = "action_log"; public ActionLogHelper(Context context) { super(context, ActionLogHelper.DB_FILE, null, ActionLogHelper.DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { Log.d(TAG, "onCreate"); StringBuilder sql = new StringBuilder(); sql.append("CREATE TABLE " + ActionLogHelper.TABLE_NAME + " ("); sql.append("ID INTEGER PRIMARY KEY AUTOINCREMENT"); sql.append(", ACTION TEXT NOT NULL"); sql.append(", TIME INTEGER NOT NULL"); sql.append(");"); db.execSQL(sql.toString()); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.d(TAG, "onUpgrade"); // 第一次執行,不用修改資料庫 } }再來就是 Activity 了。
public class DB extends Activity {
private ActionLogHelper helper;
private SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.helper = new ActionLogHelper(this);
this.addActionLog("onCreate");
this.queryAndPrintActionLogs();
}
private void addActionLog(String action) {
// 新增一筆 ActionLog,因為是新增,所以使用寫入模式
SQLiteDatabase db = this.helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("action", action);
values.put("time", System.currentTimeMillis());
db.insertOrThrow(ActionLogHelper.TABLE_NAME, null, values);
}
private void queryAndPrintActionLogs() {
// 查詢資料庫,因為是查詢,所以使用唯讀模式
SQLiteDatabase db = this.helper.getReadableDatabase();
Cursor cursor = db.query(ActionLogHelper.TABLE_NAME, new String[] {
"id", "action", "time"
}, null, null, null, null, "time DESC");
// 因為 cursor 是活的,所以將其 life cycle 交給 activity 管理
this.startManagingCursor(cursor);
// 使用 StringBuilder 輸出到畫面上
StringBuilder data = new StringBuilder();
data.append("Action Logs:\n");
while (cursor.moveToNext()) {
data.append(cursor.getLong(0));
data.append(" - ");
data.append(cursor.getString(1));
data.append(" @ ");
data.append(df.format(new Date(cursor.getLong(2))));
data.append("\n");
}
((TextView) this.findViewById(R.id.msg)).setText(data.toString());
}
@Override
protected void onDestroy() {
super.onDestroy();
this.addActionLog("onDestroy");
this.queryAndPrintActionLogs();
}
@Override
protected void onStart() {
super.onStart();
this.addActionLog("onStart");
this.queryAndPrintActionLogs();
}
@Override
protected void onRestart() {
super.onRestart();
this.addActionLog("onRestart");
this.queryAndPrintActionLogs();
}
@Override
protected void onResume() {
super.onResume();
this.addActionLog("onResume");
this.queryAndPrintActionLogs();
}
@Override
protected void onPause() {
super.onPause();
this.addActionLog("onPause");
this.queryAndPrintActionLogs();
}
@Override
protected void onStop() {
super.onStop();
this.addActionLog("onStop");
this.queryAndPrintActionLogs();
}
}資料庫檔案在哪裡?
打開 Eclipse 的 File Explorer View(Eclipse > Window >Show View > Other... > Android > File Explorer),展開目錄 data / data / [your package] / databases,就可以看到 actionlog.db。

您好,請問
回覆刪除當資料庫結構有修改時,DB_VERSION要加 1
是指原本就有的Table新增欄位或修改型態
還是新增Table也算@@?
謝謝
你試試看就知道。
刪除