“s”附近的SQLite异常语法错误

时间:2014-02-10 00:22:26

标签: android sql

真的很抱歉,如果这是微不足道的,但我已经非常努力地找到这个方法中的错误,并且对sql来说还是一个新手。不知道这里出了什么问题。真的很感激帮助

public List getAllTask​​sbyLocation(String address){

    List<Task> tasklist = new ArrayList<Task>();
     String selectQuery = "SELECT * FROM " + TABLE_TASKS + " tskTble, "
                + TABLE_LOCATIONS + " locTble, " + TABLE_TLRELATIONSHIP
                + " tlRelation WHERE locTble." + KEY_MARKER_ADDRESS + " = '"
                + address + "'" + " AND locTble." + KEY_ID + " = "
                + "tlRelation." + KEY_LOCATION_ID + " AND tskTble." + KEY_ID
                + " = " + "tlRelation." + KEY_TASK_ID;

        // insert log here

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to the list

        if (c.moveToFirst()) {
            do {
                Task task = new Task();
                task.setID(c.getInt(c.getColumnIndex(KEY_ID)));
                task.setTaskTitle(c.getString(c.getColumnIndex(KEY_TASK_TITLE)));
                task.setTaskDifficulty(c.getString(c
                        .getColumnIndex(KEY_TASK_DIFFICULTY)));

                task.setDate(c.getString(c.getColumnIndex(KEY_TASK_DATE)));

                task.setTime(c.getString(c.getColumnIndex(KEY_TASK_TIME)));
                task.setNotes(c.getString(c.getColumnIndex(KEY_TASK_NOTES)));
                task.setNotification(c.getString(c
                        .getColumnIndex(KEY_TASK_NOTIFICATION)));

                tasklist.add(task);
            } while (c.moveToNext());

        }
        return tasklist;

}

02-10 00:18:24.785: E/AndroidRuntime(29232): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prototype3/com.example.prototype3.map.TaskList_Map}: android.database.sqlite.SQLiteException: near "s": syntax error (code 1): , while compiling: SELECT * FROM tasks tskTble, locations locTble, tasks_and_locations tlRelation WHERE locTble.address = '56 Pritchatt's Rd, Birmingham, United Kingdom' AND locTble.id = tlRelation.location_id AND tskTble.id = tlRelation.task_id
02-10 00:18:24.785: E/AndroidRuntime(29232):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at android.os.Looper.loop(Looper.java:176)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at android.app.ActivityThread.main(ActivityThread.java:5419)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at java.lang.reflect.Method.invokeNative(Native Method)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at java.lang.reflect.Method.invoke(Method.java:525)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at dalvik.system.NativeStart.main(Native Method)
02-10 00:18:24.785: E/AndroidRuntime(29232): Caused by: android.database.sqlite.SQLiteException: near "s": syntax error (code 1): , while compiling: SELECT * FROM tasks tskTble, locations locTble, tasks_and_locations tlRelation WHERE locTble.address = '56 Pritchatt's Rd, Birmingham, United Kingdom' AND locTble.id = tlRelation.location_id AND tskTble.id = tlRelation.task_id
02-10 00:18:24.785: E/AndroidRuntime(29232):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1118)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:691)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1436)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1375)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at com.example.prototype3.database.DatabaseHelper.getAllTasksbyLocation(DatabaseHelper.java:229)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at com.example.prototype3.map.TaskList_Map.loadTasksToMap(TaskList_Map.java:134)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at com.example.prototype3.map.TaskList_Map.onCreate(TaskList_Map.java:85)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at android.app.Activity.performCreate(Activity.java:5372)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
02-10 00:18:24.785: E/AndroidRuntime(29232):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
02-10 00:18:24.785: E/AndroidRuntime(29232):    ... 11 more

1 个答案:

答案 0 :(得分:5)

要将其插入表格的地址包含“'”(撇号),因此,在请求被执行时出错:地址(字符串)

  

'56 Pritchatt' s Rd,Birmingham,United Kingdom'

系统的地址为 56 Pritchatt ,其余部分导致错误

您不应在请求中使用带有撇号的地址(或字符串)。