ContentProvider (contacts) - no such column: metadata_dirty - android

I'm trying to get all contacts of a specific type from the phone like following:
Cursor cursor = context.getContentResolver().query(
ContactsContract.RawContacts.CONTENT_URI,
null,
ContactsContract.RawContacts.ACCOUNT_TYPE + "='com.whatsapp'",
null,
ContactsContract.RawContacts.CONTACT_ID + " ASC");
But this line already throws an exception (custom rom, nougat => maybe it's related to this?). I only got this error from one user yet and I'm stuck here, does anyone know how to solve that? Is there an alternative way to query all contacts?
My exception looks like following:
Exception: android.database.sqlite.SQLiteException: no such column: metadata_dirty (code 1): , while compiling:
SELECT sort_key, send_to_voicemail, pinned, display_name, metadata_dirty,
phonebook_label_alt, version, phonebook_bucket, _id, custom_ringtone,
times_contacted, account_type_and_data_set, sync4, dirty, sync2,
contact_id, raw_contact_is_user_profile, aggregation_mode, data_set,
phonebook_label, account_type, sync3, display_name_alt, phonetic_name,
last_time_contacted, display_name_source, backup_id, sort_key_alt,
phonebook_bucket_alt, deleted, starred, account_name, sync1, sourceid,
phonetic_name_style
FROM view_raw_contacts_restricted AS view_raw_contacts
WHERE (1)
AND ((account_type='com.whatsapp'))
ORDER BY contact_id ASC
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java)
at android.content.ContentProviderProxy.query(ContentProviderNative.java)
at android.content.ContentResolver.query(ContentResolver.java)
at android.content.ContentResolver.query(ContentResolver.java)
...

I don't see anything wrong with your query, it seems like this is an internal bug in the user's custom ROM.
METADATA_DIRTY is a new column in Android N:
https://developer.android.com/reference/android/provider/ContactsContract.RawContactsColumns.html#METADATA_DIRTY
So it seems like the system is Nougat, but the Contacts DB is based on an older version of Android.

Related

sqlite exception error SELECT command

Caused by: android.database.sqlite.SQLiteException: near "FROM": syntax error (code 1): , while compiling: SELECT _id, FROM TRACKS WERE _id=9
String test = "SELECT _id, FROM TRACKS WERE _id="+"9";
Cursor cursor = database.rawQuery(test, null);
Did not see the Point :( any help
Maybe there is a error in my Statement but it didn't work
No comma after id and WERE should be WHERE.
Also consider using [rawQuery()](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#rawQuery(java.lang.String, java.lang.String[], android.os.CancellationSignal)] with selectionArgs to avoid having to concatenate query and values as you did in +id=+9.
SELECT _id, FROM TRACKS WERE _id="+"9"
'WERE' should be WHERE

getting sqLiteException when useing getContentResolver().query()

I'm trying to get data from sqlite database.Tried using thiscontext.getContentResolver().query() method and I got following SqlException
near ":23": syntax error (code 1): , while compiling: SELECT wol_wait, mac_addr, _id, address, name, wol_port, user, timeout, pass FROM hosts WHERE (mac_addr=00:23:15:97:ce:a0) ORDER BY name ASC
this is the code that I’m using
Cursor c=context.getContentResolver().query(HostProvider.Hosts.CONTENT_URI,null, Hosts.MAC_ADDR +"=" +String.valueOf(mhost.mac_addr),null,HostProvider.Hosts.NAME + " ASC");
how can I fix this issue?
String literals such as MAC addresses need to be in 'single quotes', or better yet, use ? placeholders and variable binding. Replace
Hosts.MAC_ADDR +"=" +String.valueOf(mhost.mac_addr),null
with
Hosts.MAC_ADDR +"=?", new String[] { String.valueOf(mhost.mac_addr) }
The String.valueOf is possibly not needed.

SQLite query not work on Android rawQuery

Edit: Problem solved, query is correct. My problem is;
I work with local database. And i'm not reach database directly from assets folder. My code copy database from assets folder to SD card when database not exits on SD card. Therefore my database changes only effect database in asset folder. And i was tried right query on old database.
Sorry for the question.
I tried my SQLite query on DB Browser for SQLite and it's worked.
But on Android same query not worked.
String[] a = new String[1];
a[0] = yazilanKelime + '%';
Cursor friendCursor = db.rawQuery( "SELECT * FROM kelimeler WHERE kelime LIKE ? ORDER BY sayi DESC LIMIT 10", a);
If i remove "ORDER BY sayi DESC" part, it's work. Where am I doing wrong?
UPDATE: Return this Exception: android.database.sqlite.SQLiteException: no such column: sayi (code 1): , while compiling: SELECT * FROM kelimeler WHERE kelime LIKE ? ORDER BY sayi DESC LIMIT 10
DB Schema:
As the exception says, no such column: sayi means that the column sayi does not exists in your table kelimeler. Check this first.

SQLiteException thrown when searching for contact

I used the ContactsQuery to search for the contact name on the device.
My code is quite similar to this sampleContactsActivity on github:
However, the application throws the SQLiteException when I enter number key on the contact search bar.
The Selection query I used was:
final static String SELECTION =
(Helper.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME) +
"<>''" + " AND " + Contacts.IN_VISIBLE_GROUP + "=1";
When I enter number key '6', the error messages that shown in log are:
android.database.sqlite.SQLiteException: near ":6": syntax error (code 1): , while compiling: SELECT _id, lookup, display_name, photo_thumb_uri, sort_key FROM view_contacts_restricted JOIN (SELECT contact_id AS snippet_contact_id FROM search_index WHERE search_index MATCH content:6* OR name:1E* UNION SELECT contact_id AS snippet_contact_id FROM phone_lookup JOIN raw_contacts ON ( raw_contacts._id=raw_contact_id) WHERE normalized_number LIKE '%6%') ON (_id=snippet_contact_id) WHERE ((display_name<>'' AND in_visible_group=1)) ORDER BY sort_key
This issue only happens to one of the Samsung Galaxy III devices.
This is a bug in the code that formats the SQL query, which is in the contacts provider implementation, which is part of that device's firmware.
Update the Android version on that device, if possible, or avoid number searches.
I resolved the problem by converting the number to string before formatting the SQL query search. I suspect that on that specific device, the contacts provider implementation doesn't automatically convert the number to string, and ends up with a SQL syntax error.
I also noticed that special character keys were problematic as well. So I simply add '' to all none-character inputs

how to use update query in android?

I am trying to update cached_name column of callog.calls table in android,but getting sql lite exception .I have been trying this for long but could not able to come up with anything.I have searched a lot but could not find sufficient information about update statement.kindly help.
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(CallLog.Calls.CONTENT_URI,
null, null, null, null);
ContentValues value=new ContentValues();
value.put("CallLog.Calls.CACHED_NAME","frank");
cr.update(CallLog.Calls.CONTENT_URI,value,CallLog.Calls.CACHED_NAME, null);
Error:
11-25 20:11:17.755: ERROR/Database(106): Error updating CallLog.Calls.CACHED_NAME=frank using UPDATE calls SET CallLog.Calls.CACHED_NAME=? WHERE name
11-25 20:11:17.776: ERROR/DatabaseUtils(106): Writing exception to parcel
11-25 20:11:17.776: ERROR/DatabaseUtils(106): android.database.sqlite.SQLiteException: near ".": syntax error: , while compiling: UPDATE calls SET CallLog.Calls.CACHED_NAME=? WHERE name
The syntax of your call to update is wrong. It needs to look like this:
cr.update(CallLog.Calls.CONTENT_URI,value,null, null);
that will update all the values in the table. To update a specific row do:
cr.update(CallLog.Calls.CONTENT_URI,value,CallLog.Calls.CACHED_NAME +" = ?", new String[]{oldName});
where oldName is the value to use in your WHERE clause.
I will suggest you to go through these links, for better understanding all the concepts of using Sqlite in Android.
http://www.vogella.de/articles/AndroidSQLite/article.html
http://www.devx.com/wireless/Article/40842/1954

Categories

Resources