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 子句显示“...dateTime BETWEEN AND...”关键字“BETWEEN”和“AND”之间缺少一个参数。

所以看起来startDate(也许也是endDate)是getAnswer().

于 2012-05-09T18:51:21.337 回答