Make sure to always close the database

This commit is contained in:
cpfeiffer 2015-06-04 23:45:46 +02:00
parent 7ad38c5e1a
commit 2f1908e480
1 changed files with 32 additions and 33 deletions

View File

@ -51,8 +51,7 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper {
public void addGBActivitySample(GBActivitySample GBActivitySample) { public void addGBActivitySample(GBActivitySample GBActivitySample) {
SQLiteDatabase db = this.getWritableDatabase(); try (SQLiteDatabase db = this.getWritableDatabase()) {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(KEY_TIMESTAMP, GBActivitySample.getTimestamp()); values.put(KEY_TIMESTAMP, GBActivitySample.getTimestamp());
values.put(KEY_PROVIDER, GBActivitySample.getProvider()); values.put(KEY_PROVIDER, GBActivitySample.getProvider());
@ -61,12 +60,11 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper {
values.put(KEY_TYPE, GBActivitySample.getType()); values.put(KEY_TYPE, GBActivitySample.getType());
db.insert(TABLE_GBACTIVITYSAMPLES, null, values); db.insert(TABLE_GBACTIVITYSAMPLES, null, values);
db.close(); }
} }
public void addGBActivitySample(int timestamp, byte provider, short intensity, byte steps, byte type) { public void addGBActivitySample(int timestamp, byte provider, short intensity, byte steps, byte type) {
SQLiteDatabase db = this.getWritableDatabase(); try (SQLiteDatabase db = this.getWritableDatabase()) {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(KEY_TIMESTAMP, timestamp); values.put(KEY_TIMESTAMP, timestamp);
values.put(KEY_PROVIDER, provider); values.put(KEY_PROVIDER, provider);
@ -75,18 +73,18 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper {
values.put(KEY_TYPE, type); values.put(KEY_TYPE, type);
db.insert(TABLE_GBACTIVITYSAMPLES, null, values); db.insert(TABLE_GBACTIVITYSAMPLES, null, values);
db.close(); }
} }
public ArrayList<GBActivitySample> getGBActivitySamples(int timestamp_from, int timestamp_to, byte provider) { public ArrayList<GBActivitySample> getGBActivitySamples(int timestamp_from, int timestamp_to, byte provider) {
if (timestamp_to == -1) { if (timestamp_to == -1) {
timestamp_to = 2147483647; // dont know what happens when I use more than max of a signed int timestamp_to = Integer.MAX_VALUE; // dont know what happens when I use more than max of a signed int
} }
ArrayList<GBActivitySample> GBActivitySampleList = new ArrayList<GBActivitySample>(); ArrayList<GBActivitySample> GBActivitySampleList = new ArrayList<GBActivitySample>();
String selectQuery = "SELECT * FROM " + TABLE_GBACTIVITYSAMPLES String selectQuery = "SELECT * FROM " + TABLE_GBACTIVITYSAMPLES
+ " where (provider=" + provider + " and timestamp>=" + timestamp_from + " and timestamp<=" + timestamp_to + ") order by timestamp"; + " where (provider=" + provider + " and timestamp>=" + timestamp_from + " and timestamp<=" + timestamp_to + ") order by timestamp";
SQLiteDatabase db = this.getWritableDatabase(); try (SQLiteDatabase db = this.getWritableDatabase()) {
Cursor cursor = db.rawQuery(selectQuery, null); Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
@ -100,6 +98,7 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper {
GBActivitySampleList.add(GBActivitySample); GBActivitySampleList.add(GBActivitySample);
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
}
return GBActivitySampleList; return GBActivitySampleList;
} }