如何進行Android數據庫操作
作者:佚名
當程序有一個消息希望發出去的時候,它需要將消息封裝成一個Android數據庫,并發送。這時候,應該是有一個統一的中心。
強烈建議,在自己Android數據庫接收或發出一個系統action的時候,要名副其實。比如你響應一個view動作,做的確實edit的勾當,你發送一個pick消息,其實你想讓別人做edit的事,這樣都會造成混亂。
一個好的習慣是創建一個輔助類來簡化你的Android數據庫交互。考慮創建一個數據庫適配器,來添加一個與數據庫交互的包裝層。它應該提供直觀的、強類型的方法,如添加、刪除和更新項目。數據庫適配器還應該處理查詢和對創建、打開和關閉數據庫的包裝。
它還常用靜態的Android數據庫常量來定義表的名字、列的名字和列的索引。下面的代碼片段顯示了一個標準數據庫適配器類的框架。它包括一個SQLiteOpenHelper類的擴展類,用于簡化打開、創建和更新數據庫。
- import android.content.Context;
- import android.database.*;
- import android.database.sqlite.*;
- import android.database.sqlite.SQLiteDatabase.CursorFactory;
- import android.util.Log;
- public class MyDBAdapter
- { // The name and column index of each column in your database.
- public static final String KEY_NAME=”name”;
- public static final int NAME_COLUMN = 1;
- // TODO: Create public field for each column in your table.
- // SQL Statement to create a new database.
- private static final String DATABASE_CREATE = “create table “ +
- DATABASE_TABLE + “ (“ + KEY_ID + “ integer primary key autoincrement, “ +
- KEY_NAME + “ text not null);”;
- // Variable to hold the database instance
- private SQLiteDatabase db;
- // Context of the application using the database.
- private final Context context;
- // Database open/upgrade helper
- private myDbHelper dbHelper;
- public MyDBAdapter(Context _context) {
- context = _context;
- dbHelper = new myDbHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- public MyDBAdapter open() throws SQLException {
- db = dbHelper.getWritableDatabase();
- return this;
- }
- public void close() {
- db.close();
- }
- public long insertEntry(MyObject _myObject) {
- ContentValues contentValues = new ContentValues();
- // TODO fill in ContentValues to represent the new row
- return db.insert(DATABASE_TABLE, null, contentValues);
- }
- public boolean removeEntry(long _rowIndex) {
- return db.delete(DATABASE_TABLE, KEY_ID + “=” + _rowIndex, null) > 0;
- }
- public Cursor getAllEntries () {
- return db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME},
- null, null, null, null, null);
- }
- public MyObject getEntry(long _rowIndex) {
- MyObject objectInstance = new MyObject();
- // TODO Return a cursor to a row from the database and
- // use the values to populate an instance of MyObject
- return objectInstance;
- }
- public int updateEntry(long _rowIndex, MyObject _myObject) {
- String where = KEY_ID + “=” + _rowIndex;
- ContentValues contentValues = new ContentValues();
- // TODO fill in the ContentValue based on the new object
- return db.update(DATABASE_TABLE, contentValues, where, null);
- }
- private static class myDbHelper extends SQLiteOpenHelper
- {
- public myDbHelper(Context context, String name, CursorFactory factory, int version) {
- super(context, name, factory, version);
- }
- // Called when no database exists in
- // disk and the helper class needs
- // to create a new one.
- @Override
- public void onCreate(SQLiteDatabase _db) {
- _db.execSQL(DATABASE_CREATE);
- }
【編輯推薦】
責任編輯:chenqingxiang
來源:
博客園