E/SQLiteLog:(1)无法识别的标记:“:”

Mac*_*122 -1 java sqlite android exception android-sqlite

我试图查询一个字符串,其中的值对应于 sqlite/android studio 中的 url 地址,但它不断抛出异常:

E/SQLiteLog: (1) unrecognized token: ":"
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.project, PID: 4525
    android.database.sqlite.SQLiteException: unrecognized token: ":" (code 1): , while compiling: SELECT  * FROM URLS_ITEMS WHERE title = Google AND url = http://www.google.com;
    #################################################################
    Error Code : 1 (SQLITE_ERROR)
    Caused By : SQL(query) error or missing database.
        (unrecognized token: ":" (code 1): , while compiling: SELECT  * FROM URLS_ITEMS WHERE title = Google AND url = http://www.google.com;)
    #################################################################
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1096)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:661)
Run Code Online (Sandbox Code Playgroud)

这是抛出异常的代码片段:

public boolean isUrlAlreadyStored(String title, String url) {
        ArrayList<URLItem> urlDetailList = new ArrayList<URLItem>();
        String query = "SELECT  * FROM " + DATABASE_TABLE_URLS_ITEMS + " WHERE title = " + title + " AND url = " + url + ";";
        Cursor cursor = database.rawQuery(query, null);
Run Code Online (Sandbox Code Playgroud)

我想我应该转义字符/令牌:但不知道如何转义。我尝试过: url = url.replaceAll(":", ":") 但显然这不是正确的方法。

有谁知道如何查询诸如“ https://www.stackoverflow.com ”之类的值/字符串?

小智 5

您可以为 SQLite 的字符串数据类型添加“'”单引号。

String query = "SELECT  * FROM " + DATABASE_TABLE_URLS_ITEMS + " WHERE title = '" + title + "' AND url = '" + url + "';";
Run Code Online (Sandbox Code Playgroud)