所以基本上我两次查询数据库。我不明白这个错误真正来自哪里,因为我没有在任何地方关闭数据库。返回错误的代码是这样运行的。我已经检查过了,我刚刚看到了一个像我这样的案例。
BeaconHandler pullAllDB = new BeaconHandler(this);
try {
List<Beacon> beaconsShown = pullAllDB.getAllBeacons();
for (final Beacon bn : beaconsShown) {
try {
int messageCount = pullAllDB.getMessageCount();
Log.d("Message", messageCount + " Messages Found");
if (messageCount > 0) {
//Do Something
} else {
// Do Nothing
}
}
catch (Exception e) {
e.getStackTrace();
Log.e("Message", e.getMessage());
}
}
}
以及执行查询的代码...
public int getBeaconsCount() {
String countQuery = "SELECT * FROM " + TABLE_BASIC_BEACON;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
public int getMessageCount() {
String mcountQuery = "SELECT * FROM " + MESSAGE_BEACON;
SQLiteDatabase mdb = this.getReadableDatabase();
Cursor mcursor = mdb.rawQuery(mcountQuery, null);
mcursor.close();
// return count
return mcursor.getCount();
}