SQLiteException:near“=”:语法错误(代码1):(已关闭)

时间:2016-01-21 07:51:22

标签: android sqlite

每次运行时,App都会关闭。当我检查logcat时,这是继续弹出的错误。

android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: SELECT * FROM tblresultsWHEREtopid=1ANDusname=fbbh
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
        at android.app.ActivityThread.access$600(ActivityThread.java:165)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5391)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        at dalvik.system.NativeStart.main(Native Method)

以下是来自DBHELPER的我的代码段:TOP_ID初始化为字符串。

public List<Result> getAllResult() {


    String qry3 = "SELECT * FROM " + TABLE_RESULT + "WHERE" + TOP_ID + "=" + "1" + "AND" + USER_NAME + "=" + user;

    Log.e(LOG, qry3);

    SQLiteDatabase db = this.getReadableDatabase();

    Cursor c = db.rawQuery(qry3, null);

    if (c.moveToFirst()) {
        do {
            Result result = new Result();
            result.setResultID(c.getInt(c.getColumnIndex(RESULT_ID)));
            result.setUserName(c.getString(c.getColumnIndex(USER_NAME)));
            result.setAverage(c.getString(c.getColumnIndex(AVERAGE)));
            result.setTopicID(c.getString(c.getColumnIndex(TOP_ID)));

            results.add(result);
        } while (c.moveToNext());
    }

    return results;
}

2 个答案:

答案 0 :(得分:5)

我认为你的concat代码存在问题

sudo service docker start

我认为你应该在String qry3 = "SELECT * FROM " + TABLE_RESULT + "WHERE" + TOP_ID + "=" + "1" + "AND" + USER_NAME + "=" + user; TABLE_RESULT之间添加空格,并在&#34; WHERE&#34;之后添加一个空格。同样适用于WHERE的cocat。 另外,如果您的AND是字符串,则需要将其放在USER_NAME内 像

这样的东西
'

答案 1 :(得分:4)

将文字的间隙放在引号内:

String qry3 = "SELECT * FROM " + TABLE_RESULT + " WHERE " + TOP_ID + " = " + "1" + " AND " + USER_NAME + " = " + user;