· 7 years ago · Feb 12, 2019, 02:44 AM
1public class DatabaseHelper_bp extends SQLiteOpenHelper {
2
3 private static final String DATABASE_NAME = "bpDB";
4 private static final int DATABASE_VERSION = 1;
5
6 // Database creation sql statement
7 private static final String DATABASE_CREATE = "create table bp_import ( _id integer primary key, datetime text not null, systolic text not null, diastolic text not null, pulses text not null, notes text not null);";
8
9 public DatabaseHelper_bp(Context context) {
10 super(context, DATABASE_NAME, null, DATABASE_VERSION);
11 }
12
13 // Method is called during creation of the database
14 @Override
15 public void onCreate(SQLiteDatabase database) {
16 database.execSQL(DATABASE_CREATE);
17 }
18
19 // Method is called during an upgrade of the database,
20 @Override
21 public void onUpgrade(SQLiteDatabase database, int oldVersion,
22 int newVersion) {
23 Log.w(DatabaseHelper_bp.class.getName(),
24 "Upgrading database from version " + oldVersion + " to "
25 + newVersion + ", which will destroy all old data");
26 database.execSQL("DROP TABLE IF EXISTS bp_import");
27 onCreate(database);
28 }
29}
30
31public class BpDAO {
32
33 private DatabaseHelper_bp dbHelper;
34
35 private SQLiteDatabase database;
36 /**
37 * bp table related constants.
38 */
39 public final static String bp_TABLE = "bp_import";
40 public final static String bp_ID = "_id";
41 public final static String bp_DT = "datetime";
42 public final static String bp_SYS = "systolic";
43 public final static String bp_DIA = "diastolic";
44 public final static String bp_PUL = "pulses";
45 public final static String bp_NOT = "notes";
46
47 /**
48 *
49 * @param context
50 */
51 public BpDAO(Context context) {
52 dbHelper = new DatabaseHelper_bp(context);
53 database = dbHelper.getWritableDatabase();
54 }
55
56 /**
57 * Creates a new blood pressure measure
58 *
59 * @param datetime
60 * @param systolic
61 * @param diastolic
62 * @param pulses
63 * @param notes
64 * @return
65 */
66 public long importBP(String datetime, String systolic, String diastolic,
67 String pulses, String notes) {
68 ContentValues values = new ContentValues();
69 values.put(bp_DT, datetime);
70 values.put(bp_SYS, systolic);
71 values.put(bp_DIA, diastolic);
72 values.put(bp_PUL, pulses);
73 values.put(bp_NOT, notes);
74 return database.insert(bp_TABLE, null, values);
75 }
76
77 public void close() {
78 database.close();
79 }
80
81 /**
82 * Fetch all bp
83 *
84 * @return
85 */
86 public Cursor fetchAll_bp() {
87 Cursor mCursor = database.query(true, bp_TABLE, new String[] { bp_SYS,
88 bp_DIA, bp_DT, bp_ID }, null, null, null, null, null, null);
89 if (mCursor != null) {
90 mCursor.moveToFirst();
91 }
92 return mCursor;
93 }
94}
95
96public class HistoryActivity extends ListActivity {
97
98private BpDAO dao;
99
100private SimpleCursorAdapter dbAdapter;
101
102@Override
103protected void onCreate(Bundle savedInstanceState) {
104 // TODO Auto-generated method stub
105 super.onCreate(savedInstanceState);
106 dao = new BpDAO(this);
107 Cursor bpList = dao.fetchAll_bp();
108 String[] from = new String[] { BpDAO.bp_SYS, BpDAO.bp_DIA, BpDAO.bp_DT };
109 int[] target = new int[] { R.id.bpSysHolder, R.id.bpDiaHolder,
110 R.id.bpDtHolder };
111 dbAdapter = new SimpleCursorAdapter(this, R.layout.history_bp, bpList,
112 from, target);
113 setListAdapter(dbAdapter);
114}
115
116@Override
117public void onListItemClick(ListView l, View view, int position, long id) {
118 // TODO Auto-generated method stub
119 super.onListItemClick(l, view, position, id);
120 Log.d("BPT", "Selected bp id =" + id);
121 // log says that i have selected an item with id : 11
122
123 Cursor selectedBpDetails = (Cursor) l.getItemAtPosition(position);
124
125 String bp_DT = selectedBpDetails.getString(selectedBpDetails
126 .getColumnIndex(BpDAO.bp_DT));
127 String bp_SYS = selectedBpDetails.getString(selectedBpDetails
128 .getColumnIndex(BpDAO.bp_SYS));
129 String bp_DIA = selectedBpDetails.getString(selectedBpDetails
130 .getColumnIndex(BpDAO.bp_DIA));
131 String bp_PUL = selectedBpDetails.getString(selectedBpDetails
132 .getColumnIndex(BpDAO.bp_PUL));
133 String bp_NOT = selectedBpDetails.getString(selectedBpDetails
134 .getColumnIndex(BpDAO.bp_NOT));
135
136 Log.d("BPT", "Selected bp details = { date=" + bp_DT + ", systolic="
137 + bp_SYS + ", diastolic=" + bp_DIA + ", pulses=" + bp_PUL
138 + ", notes=" + bp_NOT + " }");
139
140 Intent intent = new Intent(HistoryActivity.this, FromHistory.class);
141 intent.putExtra("bp_SYS", bp_SYS);
142 intent.putExtra("bp_DIA", bp_DIA);
143 intent.putExtra("bp_DT", bp_DT);
144 intent.putExtra("bp_PUL", bp_PUL);
145 intent.putExtra("bp_NOT", bp_NOT);
146 startActivity(intent);
147}
148
1491.close() was never explicitly called on database
1502.android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
1513.E/System(561): java.lang.IllegalStateException: Don't have database lock!
152
153public void close() {
154 database.close();
155}
156
157@Override
158protected void onDestroy() {
159 super.onDestroy();
160 dao.close();
161}
162
163selectedBpDetails.close();
164
165public class HistoryActivity extends ListActivity {
166 // ...
167 Cursor bpList;
168 // ...
169
170 @Override
171 protected void onCreate(Bundle savedInstanceState) {
172 // ...
173 bpList = dao.fetchAll_bp();
174 // ...
175 }
176
177 @Override
178 protected void onDestroy() {
179 super.onDestroy();
180 bpList.close();
181 }
182
183 @Override
184 public void onListItemClick(ListView l, View view, int position, long id) {
185 // ...
186 Cursor selectedBpDetails = (Cursor) l.getItemAtPosition(position);
187 // ...
188 selectedBpDetails.close();
189 }
190
191 // ...
192}