Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> 如何進行Android數據庫操作

如何進行Android數據庫操作

編輯:高級開發

強烈建議,在自己android數據庫接收或發出一個系統action的時候,要名副其實。比如你響應一個vIEw動作,做的確實edit的勾當,你發送一個pick消息,其實你想讓別人做edit的事,這樣都會造成混亂。

一個好的習慣是創建一個輔助類來簡化你的android數據交互。考慮創建一個數據庫適配器,來添加一個與數據庫交互的包裝層。它應該提供直觀的、強類型的方法,如添加、刪除和更新項目。數據庫適配器還應該處理查詢和對創建、打開和關閉數據庫的包裝。

它還常用靜態的android數據庫常量來定義表的名字、列的名字和列的索引。下面的代碼片段顯示了一個標准數據庫適配器類的框架。它包括一個SQLiteOpenHelper類的擴展類,用於簡化打開、創建和更新數據庫。

  1. import android.content.Context;
  2. import android.database.*;
  3. import android.database.sqlite.*;
  4. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  5. import android.util.Log;
  6. public class MyDBAdapter
  7. { // The name and column index of each column in your database.
  8. public static final String KEY_NAME=”name”;
  9. public static final int NAME_COLUMN = 1;
  10. // TODO: Create public fIEld for each column in your table.
  11. // SQL Statement to create a new database.
  12. private static final String DATABASE_CREATE = “create table “ +
  13. DATABASE_TABLE + “ (“ + KEY_ID + “ integer primary key autoincrement, “ +
  14. KEY_NAME + “ text not null);”;
  15. // Variable to hold the database instance
  16. private SQLiteDatabase db;
  17. // Context of the application using the database.
  18. private final Context context;
  19. // Database open/upgrade helper
  20. private myDbHelper dbHelper;
  21. public MyDBAdapter(Context _context) {
  22. context = _context;
  23. dbHelper = new myDbHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
  24. }
  25. public MyDBAdapter open() throws SQLException {
  26. db = dbHelper.getWritableDatabase();
  27. return this;
  28. }
  29. public void close() {
  30. db.close();
  31. }
  32. public long insertEntry(MyObject _myObject) {
  33. ContentValues contentValues = new ContentValues();
  34. // TODO fill in ContentValues to represent the new row
  35. return db.insert(DATABASE_TABLE, null, contentValues);
  36. }
  37. public boolean removeEntry(long _rowIndex) {
  38. return db.delete(DATABASE_TABLE, KEY_ID + “=” + _rowIndex, null) > 0;
  39. }
  40. public Cursor getAllEntrIEs () {
  41. return db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME},
  42. null, null, null, null, null);
  43. }
  44. public MyObject getEntry(long _rowIndex) {
  45. MyObject objectInstance = new MyObject();
  46. // TODO Return a cursor to a row from the database and
  47. // use the values to populate an instance of MyObject
  48. return objectInstance;
  49. }
  50. public int updateEntry(long _rowIndex, MyObject _myObject) {
  51. String where = KEY_ID + “=” + _rowIndex;
  52. ContentValues contentValues = new ContentValues();
  53. // TODO fill in the ContentValue based on the new object
  54. return db.update(DATABASE_TABLE, contentValues, where, null);
  55. }
  56. private static class myDbHelper extends SQLiteOpenHelper
  57. {
  58. public myDbHelper(Context context, String name, CursorFactory factory, int version) {
  59. super(context, name, factory, version);
  60. }
  61. // Called when no database exists in
  62. // disk and the helper class needs
  63. // to create a new one.
  64. @Override
  65. public void onCreate(SQLiteDatabase _db) {
  66. _db.execSQL(DATABASE_CREATE);
  67. }
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved