基本getCount()和rawQuery()上的SQLiteException

时间:2018-09-26 01:55:56

标签: android sqlite

我有一个非常基本的泛型函数,可以为我获取表计数。在任何人谈论安全性和优化之前,这都是针对一个小型Android应用程序的,该应用程序具有设备本地的DB,并且任何给定的表都将具有1-100行。

public int getCount() {

    if (!database.isOpen()) {
        open();
    }

    String sql = "SELECT *  FROM debt";
    Cursor c = database.rawQuery(sql, null);

    int count = c.getCount(); // the line in question
    c.close();

    return count;
}

我在Android 8.0设备上收到以下异常:

Caused by: android.database.sqlite.SQLiteException: 
  at android.database.sqlite.SQLiteConnection.nativeExecuteForCursorWindow (Native Method)
  at android.database.sqlite.SQLiteConnection.executeForCursorWindow (SQLiteConnection.java:851)
  at android.database.sqlite.SQLiteSession.executeForCursorWindow (SQLiteSession.java:974)
  at android.database.sqlite.SQLiteQuery.fillWindow (SQLiteQuery.java:62)
  at android.database.sqlite.SQLiteCursor.fillWindow (SQLiteCursor.java:143)
  at android.database.sqlite.SQLiteCursor.getCount (SQLiteCursor.java:132)
  at android.database.AbstractCursor.moveToPosition (AbstractCursor.java:219)
  at android.database.AbstractCursor.moveToNext (AbstractCursor.java:268)
  at com.---.---.database.GasDataSource.getAllGasSpent (GasDataSource.java:171)
  at com.---.---.GasUtil.getUsedLastMonth (GasUtil.java:94)
  at com.---.---.DashboardFragment.onActivityCreated (DashboardFragment.java:88)
  at android.support.v4.app.Fragment.performActivityCreated (Fragment.java:2355)
  at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1451)
  at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState (FragmentManager.java:1759)
  at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1827)
  at android.support.v4.app.BackStackRecord.executeOps (BackStackRecord.java:797)
  at android.support.v4.app.FragmentManagerImpl.executeOps (FragmentManager.java:2596)
  at android.support.v4.app.FragmentManagerImpl.executeOpsTogether (FragmentManager.java:2383)
  at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute (FragmentManager.java:2338)
  at android.support.v4.app.FragmentManagerImpl.execPendingActions (FragmentManager.java:2245)
  at android.support.v4.app.FragmentManagerImpl.dispatchStateChange (FragmentManager.java:3248)
  at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated (FragmentManager.java:3200)
  at android.support.v4.app.FragmentController.dispatchActivityCreated (FragmentController.java:195)
  at android.support.v4.app.FragmentActivity.onStart (FragmentActivity.java:597)
  at android.support.v7.app.AppCompatActivity.onStart (AppCompatActivity.java:177)
  at android.app.Instrumentation.callActivityOnStart (Instrumentation.java:1334)
  at android.app.Activity.performStart (Activity.java:6997)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2838)

无论出于何种原因,由于某些原因,如今的Play控制台堆栈跟踪中都没有特定的异常消息。只是一般的SQLiteException。

调用此代码时,我99%可以肯定该表将始终存在(根据我的经验,您永远不能说100%)。我不知道还有什么地方可能出问题。如果表为空,则肯定不会这样做。我想知道还有什么可能会导致异常的错误?

(如果任何人都不能自力更生,只是想出一种更好的写法来避免发生异常,我也很高兴;我认为在这里try / catch是最后一个选择)

0 个答案:

没有答案