查询联系人数据库中的电子邮件时出现语法错误

时间:2012-01-03 22:23:46

标签: android sqlite contacts syntax-error

我正在尝试通过联系人数据库搜索电子邮件地址并获取该人的联系人ID(如果找到)但每次尝试时都会出现语法错误

01-03 17:15:10.574: E/AndroidRuntime(7907): java.lang.RuntimeException: Unable to start receiver com.app.notifyme.GmailReciever: android.database.sqlite.SQLiteException: near "@gmail": syntax error: , while compiling: SELECT raw_contact_id FROM view_data data WHERE (1) AND ((data1=johnsmith@gmail.com))
01-03 17:15:10.574: E/AndroidRuntime(7907):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2125)
01-03 17:15:10.574: E/AndroidRuntime(7907):     at android.app.ActivityThread.access$1500(ActivityThread.java:122)
01-03 17:15:10.574: E/AndroidRuntime(7907):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-03 17:15:10.574: E/AndroidRuntime(7907):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 17:15:10.574: E/AndroidRuntime(7907):     at android.os.Looper.loop(Looper.java:137)
01-03 17:15:10.574: E/AndroidRuntime(7907):     at android.app.ActivityThread.main(ActivityThread.java:4340)
01-03 17:15:10.574: E/AndroidRuntime(7907):     at java.lang.reflect.Method.invokeNative(Native Method)
01-03 17:15:10.574: E/AndroidRuntime(7907):     at java.lang.reflect.Method.invoke(Method.java:511)
01-03 17:15:10.574: E/AndroidRuntime(7907):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-03 17:15:10.574: E/AndroidRuntime(7907):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-03 17:15:10.574: E/AndroidRuntime(7907):     at dalvik.system.NativeStart.main(Native Method)
01-03 17:15:10.574: E/AndroidRuntime(7907): Caused by: android.database.sqlite.SQLiteException: near "@gmail": syntax error: , while compiling: SELECT raw_contact_id FROM view_data data WHERE (1) AND ((data1=jtopor0910@gmail.com))
01-03 17:15:10.574: E/AndroidRuntime(7907):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:179)
01-03 17:15:10.574: E/AndroidRuntime(7907):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
01-03 17:15:10.574: E/AndroidRuntime(7907):     at android.content.ContentProviderProxy.query(ContentProviderNative.java:358)
01-03 17:15:10.574: E/AndroidRuntime(7907):     at android.content.ContentResolver.query(ContentResolver.java:310)
01-03 17:15:10.574: E/AndroidRuntime(7907):     at com.app.notifyme.GmailReciever.createNotification(GmailReciever.java:362)
01-03 17:15:10.574: E/AndroidRuntime(7907):     at com.app.notifyme.GmailReciever.onReceive(GmailReciever.java:111)
01-03 17:15:10.574: E/AndroidRuntime(7907):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2118)

这是我的查询

Cursor contact = context.getContentResolver().query(Data.CONTENT_URI,new String[] {Data.RAW_CONTACT_ID},Data.DATA1  + "=" + from,null,null);

我的语法有什么问题?它是否与@

有关

2 个答案:

答案 0 :(得分:2)

尝试在电子邮件中加上引号,即将方法调用中的第三个参数更改为

Data.DATA1  + "='" + from + "'"

答案 1 :(得分:2)

以利亚回答:Data.DATA1 + "='" + from + "'" ......嗯......有效(呵呵)

问题...如果from包含'''会怎样?

对于这个问题(以及更好/通用的答案),我们可以使用:

Cursor contact = context.getContentResolver().query(Data.CONTENT_URI,new String[]{Data.RAW_CONTACT_ID},Data.DATA1 + "=?",new String[]{from},null);

selection = Data.DATA1 +“=?”和selectionArgs = new String [] {from} (相反为null)