1

我有一个方法,如果检查两个文本字段存在然后重新调整记录 ID。我的方法如下所示:

int getQueryMatch(String word, String meter) {
    SQLiteDatabase db = this.getReadableDatabase();

    String selectQuery = "SELECT *  FROM " + TABLE_HISTORY + " WHERE " + QUERYWORD +  "= '" + word + "' AND WHERE " + DISTANCEQ + "= '"+ meter +"' "  ;
    Cursor cursor = db.rawQuery(selectQuery, null);
    int getidd = cursor.getColumnIndex(KEY_ID) ;


    Log.v("Match query ", "return? " + getidd );
    if (cursor != null)
        cursor.moveToFirst();

    // return History
    return getidd;
}

我的桌子看起来像这样:

    String CREATE_HISTORYS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_HISTORY + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
            QUERYWORD + " TEXT," + DISTANCEQ + " TEXT" + ","  + KEY_TIMESTAMP + ")";
    db.execSQL(CREATE_HISTORYS_TABLE);

现在当我像 getQueryMatch(Mystr1, Mystr2); 一样运行该方法时

我收到了这个错误:

10-18 22:04:04.294: E/AndroidRuntime(13736): FATAL EXCEPTION: AsyncTask #1
10-18 22:04:04.294: E/AndroidRuntime(13736): java.lang.RuntimeException: An error occured while executing doInBackground()
10-18 22:04:04.294: E/AndroidRuntime(13736):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at java.lang.Thread.run(Thread.java:856)
10-18 22:04:04.294: E/AndroidRuntime(13736): Caused by: ****android.database.sqlite.SQLiteException: near "WHERE": syntax error (code 1): , while compiling: SELECT *  FROM history WHERE query= 'Information Pillar' AND WHERE distance= '20'****
10-18 22:04:04.294: E/AndroidRuntime(13736):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at com.example.phooogle.DatabaseHandler.getQueryMatch(DatabaseHandler.java:149)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at com.example.phooogle.GoogleMapsAppActivity$InitTask.doInBackground(GoogleMapsAppActivity.java:233)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at com.example.phooogle.GoogleMapsAppActivity$InitTask.doInBackground(GoogleMapsAppActivity.java:1)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-18 22:04:04.294: E/AndroidRuntime(13736):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

我的选择查询对我来说看起来不错,但我不知道问题出在哪里?

4

4 回答 4

6

删除第二个WHERE,然后它将起作用。

String selectQuery = "SELECT *  FROM " + TABLE_HISTORY + " WHERE " + QUERYWORD +  "= '" + word + "' AND WHERE " + DISTANCEQ + "= '"+ meter +"' "  ;
                                                                                                        ^^^^^
                                                                                                   here's the error

啊,顺便说一句:您的查询构建似乎很容易受到SQL 注入的影响!更好地解决这个问题。

于 2012-10-18T11:09:01.767 回答
5

您的查询中有 2 个 WHERE 子句。你只能拥有一个。像这样组合它们:

String selectQuery = "SELECT *  FROM " + TABLE_HISTORY + " WHERE " + QUERYWORD +  "= '" + word + "' AND " + DISTANCEQ + "= '"+ meter +"' "  ;

所以现在你的查询是:

SELECT *  FROM history WHERE query= 'Information Pillar' AND distance= '20'`

这现在是有效的。

于 2012-10-18T11:09:58.990 回答
2

使用后无需WHERE第二次AND
使用:

String selectQuery = "SELECT *  FROM " + TABLE_HISTORY + " WHERE " + QUERYWORD +  "= '" + word + "' AND " + DISTANCEQ + "= '"+ meter +"' "  ;
于 2012-10-18T11:09:48.833 回答
1

您正在使用 2 个 WHERE 语句;但是,这是不允许的,您必须删除最后一个 WHERE 语句,因为您已经有一个 AND 语句。

于 2012-12-18T08:12:05.917 回答