2

ユーザー入力に基づいてデータベースにクエリを実行しようとしていますが、この例外が発生しました。原因がわかりません。

これはテーブルのスキーマです。

private static String CREATE_TABLE_ASSOCIATION = "create table "    
        + TABLE_ASSOCIATION + "( " + COLUMN_QUESTION_ID + " integer not null, "
        + COLUMN_ANSWER + " text not null, " + COLUMN_DATETIME + " text not null, " 
        + "FOREIGN KEY (" + COLUMN_QUESTION_ID + ") REFERENCES " + TABLE_QUESTION
        + "(" + COLUMN_ID + "), FOREIGN KEY (" + COLUMN_ANSWER + ") "
        + "REFERENCES "+ TABLE_ANSWER + "(" + COLUMN_ANSWER + "), " 
        + "FOREIGN KEY (" + COLUMN_DATETIME + ") REFERENCES " + TABLE_ANSWER
        + "(" + COLUMN_DATETIME + ") PRIMARY KEY (" + COLUMN_QUESTION_ID + "," + COLUMN_DATETIME + "));";

アクティビティのコードは次のとおりです。

public void query()
{
    quesNum = (EditText)findViewById(R.id.idBox);
    startingDate = (EditText)findViewById(R.id.startDate);
    endingDate = (EditText)findViewById(R.id.endDate);
    String num, start, end;

    num = quesNum.getText().toString();
    start = startingDate.getText().toString();
    end = endingDate.getText().toString();
    ArrayList<ArrayList<String>> result = datasource.getAnswer(Integer.parseInt(num), start, end);
    textview.setText(result.get(0).toString());
}


public void onClick(View view)
{

    switch (view.getId())
    {
        case R.id.add:
                changeText();
                break;
        case R.id.query:
            query();
            break;               
    }
}

これは、データベースを照会するために呼び出されるメソッドです。

public ArrayList<ArrayList<String>> getAnswer(int questionId, String startDate, String endDate)
{
    String query = "SELECT " + HealthyDroidQuizHelper.COLUMN_ANSWER + ", "
    + HealthyDroidQuizHelper.COLUMN_DATETIME + " FROM " + HealthyDroidQuizHelper.TABLE_ASSOCIATION
    + " WHERE " + HealthyDroidQuizHelper.COLUMN_QUESTION_ID + " = " + questionId + " AND "
    + HealthyDroidQuizHelper.COLUMN_DATETIME + " BETWEEN " + startDate + " AND " + endDate;

    ArrayList<ArrayList<String>> totalResults = new ArrayList<ArrayList<String>>();
    ArrayList<String> QuizResults = new ArrayList<String>();
    String ans, currDate, oldDate = null;

    Cursor cursor = database.rawQuery(query, null);
    cursor.moveToFirst();
    oldDate = cursor.getString(cursor.getColumnIndex(HealthyDroidQuizHelper.COLUMN_DATETIME));
    QuizResults.add(oldDate);
    while(!cursor.isAfterLast())
    {
        currDate = cursor.getString(cursor.getColumnIndex(HealthyDroidQuizHelper.COLUMN_DATETIME));
        if(!oldDate.equalsIgnoreCase(currDate))
        {
            QuizResults.add(currDate);
            oldDate = currDate;
        }

        ans = cursor.getString(cursor.getColumnIndex(HealthyDroidQuizHelper.COLUMN_ANSWER));
        QuizResults.add(ans);
        cursor.moveToNext();
    }

    totalResults.add(QuizResults);
    return totalResults;
}

最後に、これはLogcatの出力です。

05-09 19:07:32.897: D/AndroidRuntime(1229): Shutting down VM
05-09 19:07:32.897: W/dalvikvm(1229): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-09 19:07:32.897: E/AndroidRuntime(1229): Uncaught handler: thread main exiting due to uncaught exception
05-09 19:07:32.907: E/AndroidRuntime(1229): java.lang.IllegalStateException: Could not execute method of the activity
05-09 19:07:32.907: E/AndroidRuntime(1229):     at android.view.View$1.onClick(View.java:2031)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at android.view.View.performClick(View.java:2364)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at android.view.View.onTouchEvent(View.java:4179)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at android.view.View.dispatchTouchEvent(View.java:3709)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 
05-09 19:07:32.907: E/AndroidRuntime(1229):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at android.os.Looper.loop(Looper.java:123)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at android.app.ActivityThread.main(ActivityThread.java:4363)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at java.lang.reflect.Method.invokeNative(Native Method)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at java.lang.reflect.Method.invoke(Method.java:521)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at dalvik.system.NativeStart.main(Native Method)
05-09 19:07:32.907: E/AndroidRuntime(1229): Caused by: java.lang.reflect.InvocationTargetException
05-09 19:07:32.907: E/AndroidRuntime(1229):     at ca.bcit.comp4900.TestDatabaseActivity.onClick(TestDatabaseActivity.java:74)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at java.lang.reflect.Method.invokeNative(Native Method)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at java.lang.reflect.Method.invoke(Method.java:521)
05-09 19:07:32.907: E/AndroidRuntime(1229):     at android.view.View$1.onClick(View.java:2026)
05-09 19:07:32.907: E/AndroidRuntime(1229):     ... 22 more
05-09 19:07:32.907: E/AndroidRuntime(1229): Caused by: java.lang.NullPointerException
05-09 19:07:32.907: E/AndroidRuntime(1229):     at ca.bcit.comp4900.TestDatabaseActivity.query(TestDatabaseActivity.java:60)
05-09 19:07:32.907: E/AndroidRuntime(1229):     ... 26 more
05-09 19:07:32.937: I/dalvikvm(1229): threadid=7: reacting to signal 3
05-09 19:07:32.947: I/dalvikvm(1229): Wrote stack trace to '/data/anr/traces.txt'

前もって感謝します!

4

1 に答える 1

3

エラーは次のとおりです。

Caused by: android.database.sqlite.SQLiteException: near "AND": syntax error: , while compiling: SELECT answer, dateTime FROM association WHERE questionId = 1 AND dateTime BETWEEN AND

WHERE句に「...dateTimeBETWEENAND...」と表示されています。キーワード「BETWEEN」と「AND」の間にパラメータがありません。

startDateしたがって、 (そしておそらくendDate)はの空の文字列のように見えますgetAnswer()

于 2012-05-09T18:51:21.337 に答える