问题
I am trying to do a search through the contacts database for an email address and get the contact ID of that person if it finds it but everytime I try I get a syntax error
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)
this is my query
Cursor contact = context.getContentResolver().query(Data.CONTENT_URI,new String[] {Data.RAW_CONTACT_ID},Data.DATA1 + "=" + from,null,null);
what is wrong with my syntax? does it have something to do with the@
?
回答1:
Try putting quotes around your email, ie, change the third parameter in your method call to
Data.DATA1 + "='" + from + "'"
回答2:
elijah answer: Data.DATA1 + "='" + from + "'"
... well ... works (hehe)
question ... what if from
contains '''
?
for this question(and as better/generic answer) we can use:
Cursor contact = context.getContentResolver().query(Data.CONTENT_URI,new String[]{Data.RAW_CONTACT_ID},Data.DATA1 + "=?",new String[]{from},null);
selection = Data.DATA1 + "=?" and selectionArgs = new String[]{from}(instead null)
来源:https://stackoverflow.com/questions/8719499/syntax-error-when-querying-contacts-database-for-an-email