Thứ Tư, 20 tháng 6, 2012

Làm việc với Database trên Android (P4)

Ngoài ra ta cần có một lớp khai báo một số các phương thức utility để dễ dàng làm việc với Database. Bạn hãy xem ví dụ để thấy cụ thể các phương thức này.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.tannm.doan.taskmanager.database;
 
import android.database.Cursor;
 
public class DataTable {
 // fields
 private DataRow dataRow = null;
 
 // methods
 public DataTable(DataRow dataRow) {
  this.dataRow = dataRow;
 }
 
 public Database getUserDb() {
  return dataRow.getUserDb();
 }
 
 public String getTableName() {
  return dataRow.getTableName();
 }
 
 public DataRow getDataRow() {
  return dataRow;
 }
 
 public boolean createTable() {
  if (getUserDb().tableExists(getTableName())) {
   return true;
  } else {
   return getUserDb()
     .execSQL(
       getSqlTableDefinition(getTableName(), dataRow
         .getTableDef()));
  }
 }
 
 public String getSqlTableDefinition(String sTableName,
   DataField[] vecTableDef) {
  String def = "CREATE TABLE " + sTableName + " (";
  for (int i = 0; i < vecTableDef.length; i++) {
   def += vecTableDef[i].getColumnDefinition();
   if (i < (vecTableDef.length - 1))
    def += ", ";
  }
  def += ")";
  return def;
 }
 
 public long insertValues() {
  long lRowId = getUserDb().getSQLiteDb().insert(getTableName(), null,
    dataRow.getContentValues());
  return lRowId;
 }
 
 public long updateValues(long lRowId) {
  String sWhere = String.format("_ID = %d", lRowId);
  long lRowsUpdated = getUserDb().getSQLiteDb().update(getTableName(),
    dataRow.getContentValues(), sWhere, null);
  return lRowsUpdated;
 }
 
 public long deleteDataRow(long lRowId) {
  String sWhere = String.format("_ID = %d", lRowId);
  long lRowsUpdated = getUserDb().getSQLiteDb().delete(getTableName(),
    sWhere, null);
  return lRowsUpdated;
 }
 
 public Cursor locateDataRow(long lRowId) {
  final String s = "select * from %s where _ID = %d";
  String sql = String.format(s, getTableName(), lRowId);
  Cursor cr = getUserDb().getSQLiteDb().rawQuery(sql, null);
  // if cursor valid, set first data row as current
  if ((cr != null) && (cr.getCount() > 0))
   cr.moveToFirst();
  return cr;
 }
 
 public Cursor locateAlarmDataRow(int iType, long lRefID) {
  final String s = "select * from %s where Type = %d and RefID = %d";
  String sql = String.format(s, getTableName(), iType, lRefID);
  Cursor cr = getUserDb().getSQLiteDb().rawQuery(sql, null);
  // if cursor valid, set first data row as current
  if ((cr != null) && (cr.getCount() > 0))
   cr.moveToFirst();
  return cr;
 }
 
 public Database.Result updateData(boolean bInsertMode, long lEditRowId) {
  Database.Result result = Database.Result.errUnknown;
  if (getUserDb().isOpened()) {
   try {
    dataRow.setValuesForDataRow();
   } catch (Exception e) {
    return Database.Result.errCantSetValuesForDataRow;
   }
   // select update mode
   if (bInsertMode) {
    // insert new data row
    long lRowId = insertValues();
    if (lRowId > 0) {
     result = Database.Result.Success;
    } else {
     result = Database.Result.errCantInsertNewData;
    }
   } else {
    // update existing data row
    long lRowsUpdated = updateValues(lEditRowId);
    if (lRowsUpdated == 1) {
     result = Database.Result.Success;
    } else {
     result = Database.Result.errCantUpdateData;
    }
   }
  } else {
   result = Database.Result.errNoDbAccess;
  }
  return result;
 }
 
 public Database.Result deleteData(long iRowId) {
  Database.Result result = Database.Result.errUnknown;
  if (getUserDb().isOpened()) {
   if (getUserDb().tableExists(getTableName())) {
    long lRowsDeleted = deleteDataRow(iRowId);
    if (lRowsDeleted == 1) {
     result = Database.Result.Success;
    } else {
     result = Database.Result.errCantDeleteData;
    }
   } else {
    result = Database.Result.errTableNotExists;
   }
  } else {
   result = Database.Result.errNoDbAccess;
  }
  return result;
 }
 
 public Database.Result getRowDataForEdit(long lRowId) {
  Database.Result result = Database.Result.errUnknown;
  // get requested data row
  Cursor cr = locateDataRow(lRowId);
  if (cr == null) {
   result = Database.Result.errCantGetData;
  } else {
   if (cr.getCount() > 0) {
    if (dataRow.getValuesFromCursor(cr)) {
     try {
      dataRow.getValuesFromDataRow();
     } catch (Exception e) {
      return Database.Result.errCantGetValuesFromDataRow;
     }
     result = Database.Result.Success;
    } else {
     result = Database.Result.errCantGetDataFromTable;
    }
    cr.close();
   } else {
    result = Database.Result.errCantFindData;
   }
  }
  return result;
 }
}

Trên đây là toàn bộ kinh nghiệm làm việc với Database mà tôi học được trong quá trình làm việc thực tế. Mong giúp đỡ những người mới làm việc với SQLite trên Android. Chúc các bạn thành công.

Không có nhận xét nào: