0

さて、私のタイトルはそれを言っていると思います...次のコードを使用して:

public int getCorrectAnswersPrev(int level, String player) {
        int count = 0;
        level = level - 1;
        Cursor c = myDataBase.rawQuery(
                "SELECT COUNT(*) FROM ANSWERS WHERE LEVEL =" + level
                        + " AND ANSWERCORRECT = 1 AND PLAYER=" + player, null);
        c.moveToFirst();
        count = c.getInt(0);
        c.close();
        myDataBase.close();

        return count;

    }

プレイヤーが特定の質問に答えなかったことがあるので、レベルの最初に特定のアイテムがないことを意味します...カウントを0として初期化しますが、このエラーが発生しますが、それでも実行されることを理解していますクエリを実行し、エラーを返します。しかし、データベースにアイテムがない場合にこれを防ぐ方法はありますか?

編集:

    11-16 13:34:30.188: E/AndroidRuntime(10206): FATAL EXCEPTION: main
11-16 13:34:30.188: E/AndroidRuntime(10206): android.database.sqlite.SQLiteException: no such column: Bog: , while compiling: SELECT COUNT(*) FROM ANSWERS WHERE LEVEL =1 AND ANSWERCORRECT = 1 AND PLAYER=Bog
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1324)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at com.hup.game.db.DataBaseHelper.getCorrectAnswers(DataBaseHelper.java:251)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at com.hup.game.LevelSelectorAdapter.getView(LevelSelectorAdapter.java:75)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.widget.AbsListView.obtainView(AbsListView.java:1432)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.widget.GridView.onMeasure(GridView.java:935)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.view.View.measure(View.java:8319)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.view.View.measure(View.java:8319)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.view.View.measure(View.java:8319)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.view.View.measure(View.java:8319)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.os.Looper.loop(Looper.java:130)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at android.app.ActivityThread.main(ActivityThread.java:3683)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at java.lang.reflect.Method.invokeNative(Native Method)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at java.lang.reflect.Method.invoke(Method.java:507)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:861)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619)
11-16 13:34:30.188: E/AndroidRuntime(10206):    at dalvik.system.NativeStart.main(Native Method)
4

4 に答える 4

3

連結後の値playerはおそらく列名として扱われるため、次の代わりに:

Cursor c = myDataBase.rawQuery(
    "SELECT COUNT(*) FROM ANSWERS WHERE LEVEL =" + level
    + " AND ANSWERCORRECT = 1 AND PLAYER=" + player, null);

パラメータを使用して試してください。

Cursor c = myDataBase.rawQuery(
    "SELECT COUNT(*) FROM ANSWERS WHERE LEVEL = ? " +
    " AND ANSWERCORRECT = 1 AND PLAYER = ?",
    new String[] { String.valueOf(level), player }
);                      
于 2012-11-16T12:34:00.260 に答える
3
SELECT COUNT(*) FROM ANSWERS WHERE LEVEL =1 AND ANSWERCORRECT = 1 AND PLAYER=Bog

エラーをスローします。

そのはず

SELECT COUNT(*) FROM ANSWERS WHERE LEVEL =1 AND ANSWERCORRECT = 1 AND PLAYER='Bog'
于 2012-11-16T12:37:53.017 に答える
1

レコードがない場合は、をCursor返しますnull

次のようc == nullに使用する前にテストする必要があります。moveToFirst()

if(c != null){
    c.moveToFirst();


}
于 2012-11-16T12:36:26.807 に答える
0
Use "ifnull"

例:

  Cursor c = myDataBase.rawQuery(select ifnull(max(tm.no),0) as serial_no "
                        + "from table_bun as tb inner join table_mar"
                        + " as tm on tb.id = tm.id "
                        + "where tb.bid = " + bId
                        + " and tb.sid =  " + SId
                        + " and tb.eby  = '" + UId + "'", null);
于 2012-11-16T12:32:58.907 に答える