Question Detail

I am getting the count of rows I have in Location Table. I am using this query
 

public int getContactsCount() {
    String countQuery = "SELECT  * FROM locationList";
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();
}

and

getting this error
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stayapt/com.stayapt.SplashActivity}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM locationList at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:5292) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM locationList_stay at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55) at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:58) at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:159) at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:148) at com.stayapt.localdatabase.DatabaseHandler.getContactsCount(DatabaseHandler.java:148) at com.stayapt.SplashActivity.onCreate(SplashActivity.java:58) at android.app.Activity.performCreate(Activity.java:5264) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)  at android.app.ActivityThread.access$800(ActivityThread.java:151)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)  at android.os.Handler.dispatchMessage(Handler.java:110)  at android.os.Looper.loop(Looper.java:193)  at android.app.ActivityThread.main(ActivityThread.java:5292)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)  at dalvik.system.NativeStart.main(Native Method)


Thread Reply

Anonymous

- 1 years ago

public int getContactsCount() {
    String countQuery = "SELECT  * FROM locationList";
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();
}

Anonymous

- 1 years ago

Cursor.close should be in end after the cursor.getCount(). 

Anonymous

- 1 years ago

public int getLogsCount(){
    ourDatabase = getWritableDatabase();
    int numRows = (int) DatabaseUtils.longForQuery(ourDatabase, "SELECT COUNT(*) FROM 

    locationList", null);
    ourDatabase.close();
    return numRows;

}

Don't forget to import android.database.DatabaseUtils;

Anonymous

- 1 years ago

public int getLogsCount(){ ourDatabase = getWritableDatabase(); int numRows = (int) DatabaseUtils.longForQuery(ourDatabase, "SELECT COUNT(*) FROM locationList", null); ourDatabase.close(); return numRows; }

Anonymous

- 1 years ago

public int rowCount(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.longForQuery(db, "SELECT COUNT(*) FROM locationList", null);
db.close();
return numRows;

}