我在SQLite数据库处理程序类中有一个函数,该函数递归调用自己并更新每个事务的计数器,但问题是计数器函数也使用了SQLite函数,并且抛出了游标窗口错误,并说我有一个打开的游标
我的计数功能被多次调用
public String PendingTransCount(){
String selectQuery = "SELECT * FROM " + TABLE_TRANSACTIONS_LOG;
final SQLiteDatabase database = this.getReadableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if(cursor.getCount() > 0){
int count = cursor.getCount();
cursor.close();
return String.valueOf(count);
}else{
cursor.close();
return "";
}
}
my main Function
final int[] trans_counter = {0};
int cursor_count = 0;
Log.i("Tag", "getAllTransactions called");
String selectQuery = "SELECT * FROM " + TABLE_TRANSACTIONS_LOG + " LIMIT 100";
final SQLiteDatabase database = this.getWritableDatabase();
final SQLiteDatabase database1 = this.getWritableDatabase();
final Cursor getAllCursor = database.rawQuery(selectQuery, null);
cursor_count = getAllCursor.getCount();
if (getAllCursor.moveToFirst()) {
do {
try {
final String tr_id = getAllCursor.getString(getAllCursor.getColumnIndex("tr_id"));
final String cardholder_id = getAllCursor.getString(getAllCursor.getColumnIndex("cardholder_id"));
final String uid = getAllCursor.getString(getAllCursor.getColumnIndex("uid"));
final String cid = getAllCursor.getString(getAllCursor.getColumnIndex("cid"));
final String tr_status = getAllCursor.getString(getAllCursor.getColumnIndex("tr_status"));
final String tr_latitude = getAllCursor.getString(getAllCursor.getColumnIndex("tr_latitude"));
final String tr_longitude = getAllCursor.getString(getAllCursor.getColumnIndex("tr_longitude"));
final String tr_type = getAllCursor.getString(getAllCursor.getColumnIndex("tr_type"));
final String operator_id = getAllCursor.getString(getAllCursor.getColumnIndex("operator_id"));
final String site_id = getAllCursor.getString(getAllCursor.getColumnIndex("site_id"));
final String expiry_date = getAllCursor.getString(getAllCursor.getColumnIndex("expiry_date"));
final String tr_date = getAllCursor.getString(getAllCursor.getColumnIndex("tr_date"));
final String cardholder_name = getAllCursor.getString(getAllCursor.getColumnIndex("cardholder_name"));
final String company_name = getAllCursor.getString(getAllCursor.getColumnIndex("company_name"));
final String site_name = getAllCursor.getString(getAllCursor.getColumnIndex("site_name"));
final String operator_name = getAllCursor.getString(getAllCursor.getColumnIndex("operator_name"));
final String device_imei = getAllCursor.getString(getAllCursor.getColumnIndex("device_imei"));
final String notes = getAllCursor.getString(getAllCursor.getColumnIndex("notes"));
final String method = getAllCursor.getString(getAllCursor.getColumnIndex("method"));
final String tag_no = getAllCursor.getString(getAllCursor.getColumnIndex("tag_no"));
final String sop = getAllCursor.getString(getAllCursor.getColumnIndex("sop"));
final String access = getAllCursor.getString(getAllCursor.getColumnIndex("access"));
Log.i("Tag", "All okay");
try {
Call<General_Trans_Response> callRepos = new Retrofit_Api_Client().createService(Retrofit_Api_Interface.class).SaveTransaction(
tr_id,cardholder_name,cardholder_id,tr_status,tr_latitude,tr_longitude,tr_type,operator_id,site_id,expiry_date,tr_date,company_name,
site_name,uid,operator_name,cid,device_imei,notes,method,tag_no,sop,access);
final int finalCursor_count = cursor_count;
final Callback repos = new Callback<General_Trans_Response>() {
@Override
public void onResponse(Call<General_Trans_Response> call, Response<General_Trans_Response> obj) {
String id = obj.body().getTrans_id();
UpdateTransactionLog(TABLE_TRANSACTIONS_LOG_TODAY);
Log.i("Tag", "id= " + id);
database1.delete(TABLE_TRANSACTIONS_LOG, "tr_id" + "=" + id, null);
trans_counter[0]++;
if(trans_counter[0] == finalCursor_count){
getAllCursor.close();
database.close();
database1.close();
Log.i("Tag", "trans_count= " + trans_counter[0] + " cursor_count= " + finalCursor_count);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Boolean exist = getPendingTransactionCount();
if(exist){
try {
getAllTransactions();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}else{
try {
getAllTrips();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
},3000);
}
}
@Override
public void onFailure(Call<General_Trans_Response> call, Throwable t) {
}
};
callRepos.enqueue(repos);
} catch (Exception e) {
insertErrorLog(this.getClass().getSimpleName() + " => " + e, context.getResources().getString(R.string.data_save_error), "E139A");
Log.e("data", "getAllTransactions: " + e.getStackTrace() );
}
} catch (Exception e) {
insertErrorLog(this.getClass().getSimpleName() + " => " + e, context.getResources().getString(R.string.data_save_error), "E139A");
Log.e("data", "getAllTransactions: " + e.getStackTrace() );
}
} while (getAllCursor.moveToNext());
}
getAllCursor.close();