-1

ContentObserver在我的项目中,当联系人表发生更改时,有一段代码正在等待调用。发生这种情况时,我会一一检查表中的每个联系人是否已更改或已创建。然后,根据这一点,我将更改保存在自己的表上。但它给了我错误。这是我的代码:

while (phones.moveToNext()) {
            try {
                name = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                phoneNumber = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                JSONObject jobject = db.select_contact(name, phoneNumber);

                String type1 = jobject.getString("type");

                if (type1.equals("upload")) {
                    Log.i("********", "Upload on the db" + name + "  "
                            + phoneNumber);
                    db.contact_table(name, phoneNumber);
                } else if (type1.equals("change_name")) {
                    db.update_name(name, phoneNumber);
                    Log.i("********", "Change name and upload " + name);
                } else if (type1.equals("change_phone")) {
                    db.update_name(name, phoneNumber);
                    Log.i("********", "Change phone and upload "
                            + phoneNumber);
                }

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

还有我把它上传到我自己创建的表的部分:

public long update_name(String name, String number) {
    ContentValues cv = new ContentValues();
    cv.put(NAME, name);
    return ourDatabase.update(DATABASE_TABLE_4, cv, NUMBER + "=" + number,
            null);
}

public long update_number(String name, String number) {
    ContentValues cv = new ContentValues();
    cv.put(NUMBER, number);
    return ourDatabase
            .update(DATABASE_TABLE_4, cv, NAME + "=" + name, null);
}

问题是在数据库上的上传更改部分,它给了我这个错误:

07-07 09:28:41.412: W/System.err(11667): android.database.sqlite.SQLiteException: near "629": syntax error: , while compiling: UPDATE contacts SET name=? WHERE phone=+34 629 21 00 80
07-07 09:28:41.412: W/System.err(11667):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
07-07 09:28:41.412: W/System.err(11667):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
07-07 09:28:41.412: W/System.err(11667):    at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
07-07 09:28:41.412: W/System.err(11667):    at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
07-07 09:28:41.412: W/System.err(11667):    at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
07-07 09:28:41.412: W/System.err(11667):    at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:84)
07-07 09:28:41.412: W/System.err(11667):    at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1936)
07-07 09:28:41.412: W/System.err(11667):    at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1887)
07-07 09:28:41.412: W/System.err(11667):    at com.background.Database.update_name(Database.java:215)
07-07 09:28:41.417: W/System.err(11667):    at com.extract.Contacts.onChange(Contacts.java:69)
07-07 09:28:41.417: W/System.err(11667):    at android.database.ContentObserver$NotificationRunnable.run(ContentObserver.java:43)
07-07 09:28:41.417: W/System.err(11667):    at android.os.Handler.handleCallback(Handler.java:605)
07-07 09:28:41.417: W/System.err(11667):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-07 09:28:41.417: W/System.err(11667):    at android.os.Looper.loop(Looper.java:137)
07-07 09:28:41.417: W/System.err(11667):    at android.app.ActivityThread.main(ActivityThread.java:4507)
07-07 09:28:41.422: W/System.err(11667):    at java.lang.reflect.Method.invokeNative(Native Method)
07-07 09:28:41.422: W/System.err(11667):    at java.lang.reflect.Method.invoke(Method.java:511)
07-07 09:28:41.422: W/System.err(11667):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-07 09:28:41.422: W/System.err(11667):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-07 09:28:41.422: W/System.err(11667):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

2

这个:

WHERE phone=+34 629 21 00 80 

应该是这样的:

WHERE phone="+34 629 21 00 80"
于 2012-07-07T07:44:25.400 回答
0

你的语法是错误的。要在 SQL 中测试字符串值,您应该使用“Like”运算符。

你的 Where 声明应该是这样的:

WHERE phone LIKE "+34 629 21 00 80"

这是正确的答案:

public long update_name(String name, String number) {
    ContentValues cv = new ContentValues();
    cv.put(NAME, name);
    return ourDatabase.update(DATABASE_TABLE_4, cv, NUMBER + " LIKE ?",
        new String[]{number});
}

public long update_number(String name, String number) {
    ContentValues cv = new ContentValues();
    cv.put(NUMBER, number);
    return ourDatabase
        .update(DATABASE_TABLE_4, cv, NAME + " LIKE ?", new String[]{name});
}
于 2012-07-07T11:30:47.917 回答