当前位置: 首页 > 知识库问答 >
问题:

靠近“where”:语法错误-SQLite

向和歌
2023-03-14

我有一个方法,如果检查两个文本字段存在,然后重新调整记录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)

我的select查询看起来很正常,但我不知道问题出在哪里?

共有2个答案

胡新
2023-03-14

删除第二个where,那么它就可以工作了。

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

啊,顺便说一句:您的查询构建似乎很容易受到SQL注入的影响!最好把它修好。

金骞尧
2023-03-14

查询中有2个WHERE子句。你只允许有一个。这样组合起来:

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

所以现在您的查询将是:

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

这现在是有效的。

 类似资料:
  • 我的SQL语句语法有问题,LogCat说问题就在哪里。我哪里错了? 下面是我的代码: 任何帮助或建议都将欣然接受,这是我的项目的最后一块拼图。当我做完这件事我就可以Rest了。提前道谢。

  • 私有静态final int DATABASE_VERSION=1;私有静态最终字符串DATABASE_NAME=“unitconversion.db”;

  • 问题内容: 我在堆栈中搜索,但没有一个达到最终答案。我的查询是这样的: 运行时,我收到语法分析错误: 我不知道该如何解决,问题出在哪里? 问题答案: 您需要稍微更改语法,请尝试以下操作:

  • 我的Android应用程序中没有内置以下Dao: 当我在Android Studio3中构建项目时,我在gradle构建步骤中得到以下错误: 如果我删除loadAll函数,一切都像魅力一样工作...这个函数有什么问题?

  • 我只是想创建一个包含一列的表,并向其中添加一些值,但我得到了两个错误。一种是取消对db.close()的注释;句柄关闭太快,另一个是下面是代码。

  • 首先,有类似的问题,比如1,2,3,4,5,但没有提到解决这个问题的答案。 LogCat错误 09-02 05:27:31.213: E/SQLiteDatabase(8442):错误插入日=2个月=9值=3.0年=2015 09-02 05:27:31.213: E/SQLiteDatabase(8442):android.database.sqlite.SQLiteExc0019:近空:语法错