2014-03-05 56 views
1

任何人都可以點我在哪裏是語法錯誤嗎?SQLite的語法錯誤與Android

出於某種原因,我無法找到它。我不確定我的方法是否正確(我仍在測試)。

這裏是我的代碼:

final Cursor cursor; 

    final String[] PROJECTION_SEARCH_FOR_CONTACT = new String[] { 
     Contacts._ID, 
     Contacts.DISPLAY_NAME, 
     Contacts.PHOTO_ID, 
     "name_raw_contact_id", 
     Contacts.Data.DATA1 // Telephone number 
    }; 

    // Search by Display Name 
    if(where == 0) 
    { 
     cursor = contentResolver.query(Data.CONTENT_URI, 
             PROJECTION_SEARCH_FOR_CONTACT, 
             Contacts.DISPLAY_NAME + " LIKE '" + searchFor + "%' AND " + Data.MIMETYPE + " = " + Contacts.CONTENT_ITEM_TYPE, 
             null, 
             null); 
    } 

    // Search by Phone Number 
    else if(where == 1) 
    { 
     cursor = contentResolver.query(Data.CONTENT_URI, 
             PROJECTION_SEARCH_FOR_CONTACT, 
             Contacts.Data.DATA1+ " LIKE '" + searchFor + "%' AND " + Contacts.Data.MIMETYPE + " = " + Phone.CONTENT_ITEM_TYPE, 
             null, 
             null); 
    } 

    // Search by Email 
    else 
    { 
     cursor = contentResolver.query(Data.CONTENT_URI, 
             PROJECTION_SEARCH_FOR_CONTACT, 
             Contacts.Data.DATA1+ " LIKE '" + searchFor + "%' AND " + Contacts.Data.MIMETYPE + " = " + Email.CONTENT_ITEM_TYPE, 
             null, 
             null); 
    } 

這裏是異常錯誤。

對於where == 0

03-05 00:31:06.473: E/SQLiteLog(2012): (1) near ".": syntax error 
03-05 00:31:06.477: E/DatabaseUtils(2012): Writing exception to parcel 
03-05 00:31:06.477: E/DatabaseUtils(2012): android.database.sqlite.SQLiteException: near ".": syntax error (code 1): , while compiling: SELECT _id, display_name, photo_id, name_raw_contact_id, data1 FROM view_data data LEFT OUTER JOIN (SELECT data_usage_stat.data_id, SUM(data_usage_stat.times_used) as times_used, MAX(data_usage_stat.last_time_used) as last_time_used FROM data_usage_stat GROUP BY data_id) as data_usage_stat ON (data_usage_stat.data_id=data._id) WHERE (1) AND ((display_name LIKE 'c%' AND mimetype = vnd.android.cursor.item/contact)) 
03-05 00:31:06.477: E/DatabaseUtils(2012): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
03-05 00:31:06.477: E/DatabaseUtils(2012): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 
03-05 00:31:06.477: E/DatabaseUtils(2012): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 
03-05 00:31:06.477: E/DatabaseUtils(2012): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
03-05 00:31:06.477: E/DatabaseUtils(2012): at android.database.sqlite.SQLiteQueryBuilder.validateQuerySql(SQLiteQueryBuilder.java:412) 
03-05 00:31:06.477: E/DatabaseUtils(2012): at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:389) 
03-05 00:31:06.477: E/DatabaseUtils(2012): at com.android.providers.contacts.ContactsProvider2.query(ContactsProvider2.java:6438) 
03-05 00:31:06.477: E/DatabaseUtils(2012): at com.android.providers.contacts.ContactsProvider2.queryLocal(ContactsProvider2.java:6386) 
03-05 00:31:06.477: E/DatabaseUtils(2012): at com.android.providers.contacts.ContactsProvider2.query(ContactsProvider2.java:4999) 
03-05 00:31:06.477: E/DatabaseUtils(2012): at android.content.ContentProvider$Transport.query(ContentProvider.java:200) 
03-05 00:31:06.477: E/DatabaseUtils(2012): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112) 
03-05 00:31:06.477: E/DatabaseUtils(2012): at android.os.Binder.execTransact(Binder.java:404) 
03-05 00:31:06.477: E/DatabaseUtils(2012): at dalvik.system.NativeStart.run(Native Method) 

對於where == 1

03-05 00:32:21.922: E/SQLiteLog(2012): (1) near ".": syntax error 
03-05 00:32:21.938: E/DatabaseUtils(2012): Writing exception to parcel 
03-05 00:32:21.938: E/DatabaseUtils(2012): android.database.sqlite.SQLiteException: near ".": syntax error (code 1): , while compiling: SELECT _id, display_name, photo_id, name_raw_contact_id, data1 FROM view_data data LEFT OUTER JOIN (SELECT data_usage_stat.data_id, SUM(data_usage_stat.times_used) as times_used, MAX(data_usage_stat.last_time_used) as last_time_used FROM data_usage_stat GROUP BY data_id) as data_usage_stat ON (data_usage_stat.data_id=data._id) WHERE (1) AND ((data1 LIKE 'c%' AND mimetype = vnd.android.cursor.item/phone_v2)) 
03-05 00:32:21.938: E/DatabaseUtils(2012): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
03-05 00:32:21.938: E/DatabaseUtils(2012): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 
03-05 00:32:21.938: E/DatabaseUtils(2012): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 
03-05 00:32:21.938: E/DatabaseUtils(2012): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
03-05 00:32:21.938: E/DatabaseUtils(2012): at android.database.sqlite.SQLiteQueryBuilder.validateQuerySql(SQLiteQueryBuilder.java:412) 
03-05 00:32:21.938: E/DatabaseUtils(2012): at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:389) 
03-05 00:32:21.938: E/DatabaseUtils(2012): at com.android.providers.contacts.ContactsProvider2.query(ContactsProvider2.java:6438) 
03-05 00:32:21.938: E/DatabaseUtils(2012): at com.android.providers.contacts.ContactsProvider2.queryLocal(ContactsProvider2.java:6386) 
03-05 00:32:21.938: E/DatabaseUtils(2012): at com.android.providers.contacts.ContactsProvider2.query(ContactsProvider2.java:4999) 
03-05 00:32:21.938: E/DatabaseUtils(2012): at android.content.ContentProvider$Transport.query(ContentProvider.java:200) 
03-05 00:32:21.938: E/DatabaseUtils(2012): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112) 
03-05 00:32:21.938: E/DatabaseUtils(2012): at android.os.Binder.execTransact(Binder.java:404) 
03-05 00:32:21.938: E/DatabaseUtils(2012): at dalvik.system.NativeStart.run(Native Method) 

對於where == 2

03-05 00:33:13.844: E/SQLiteLog(2012): (1) near ".": syntax error 
03-05 00:33:13.852: E/DatabaseUtils(2012): Writing exception to parcel 
03-05 00:33:13.852: E/DatabaseUtils(2012): android.database.sqlite.SQLiteException: near ".": syntax error (code 1): , while compiling: SELECT _id, display_name, photo_id, name_raw_contact_id, data1 FROM view_data data LEFT OUTER JOIN (SELECT data_usage_stat.data_id, SUM(data_usage_stat.times_used) as times_used, MAX(data_usage_stat.last_time_used) as last_time_used FROM data_usage_stat GROUP BY data_id) as data_usage_stat ON (data_usage_stat.data_id=data._id) WHERE (1) AND ((data1 LIKE 'c%' AND mimetype = vnd.android.cursor.item/email_v2)) 
03-05 00:33:13.852: E/DatabaseUtils(2012): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
03-05 00:33:13.852: E/DatabaseUtils(2012): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 
03-05 00:33:13.852: E/DatabaseUtils(2012): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 
03-05 00:33:13.852: E/DatabaseUtils(2012): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
03-05 00:33:13.852: E/DatabaseUtils(2012): at android.database.sqlite.SQLiteQueryBuilder.validateQuerySql(SQLiteQueryBuilder.java:412) 
03-05 00:33:13.852: E/DatabaseUtils(2012): at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:389) 
03-05 00:33:13.852: E/DatabaseUtils(2012): at com.android.providers.contacts.ContactsProvider2.query(ContactsProvider2.java:6438) 
03-05 00:33:13.852: E/DatabaseUtils(2012): at com.android.providers.contacts.ContactsProvider2.queryLocal(ContactsProvider2.java:6386) 
03-05 00:33:13.852: E/DatabaseUtils(2012): at com.android.providers.contacts.ContactsProvider2.query(ContactsProvider2.java:4999) 
03-05 00:33:13.852: E/DatabaseUtils(2012): at android.content.ContentProvider$Transport.query(ContentProvider.java:200) 
03-05 00:33:13.852: E/DatabaseUtils(2012): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112) 
03-05 00:33:13.852: E/DatabaseUtils(2012): at android.os.Binder.execTransact(Binder.java:404) 
03-05 00:33:13.852: E/DatabaseUtils(2012): at dalvik.system.NativeStart.run(Native Method) 

回答

4
... mimetype = vnd.android.cursor.item/contact 

你需要把字符串字面量''單引號。例如,

... Data.MIMETYPE + " = '" + Contacts.CONTENT_ITEM_TYPE + "'" 

和類似的其他情況。

+0

謝謝你,這就是你(我,在這種情況下)發生時,要編寫它快速的測試目的。所以解決方法是使用光標上的選擇字段。像這樣:'光標= contentResolver.query(Data.CONTENT_URI, \t \t \t \t \t \t \t \t \t \t \t PROJECTION_SEARCH_FOR_CONTACT, \t \t \t \t \t \t \t \t \t \t \t Contacts.DISPLAY_NAME + 「LIKE「」 + searchFor + 「%」 AND 」+ Data.MIMETYPE +「=?」, \t \t \t \t \t \t \t \t \t \t \t新的String [] {Contacts.CONTENT_ITEM_TYPE}, \t \t \t \t \t \t \t \t \t \t \t NULL);' – dazito

+0

是的,這也將工作。使用'?'佔位符文字是一個好習慣,即使在這種情況下,沒有直接的好處(也不傷害)。 – laalto