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
Related
My SQLLite query is not working properly. See below:
UUID i=UUID.randomUUID();
String Beregero ="INSERT INTO
contacts(id,uuid,name,phone,email,street,city,state,zip) " +
" VALUES(3,"+"'"+i.toString()+"'"+",'Patrice
Beregeron','978-555-1212','pBeregero#BostonBruins.com'," +
"'1 causeway street','Boston','Mass','01236');";
db.execSQL(Beregero);
I am receiving the following error in my log:
(table contacts has no column named uuid (code 1): , while
compiling: INSERT INTO contacts(id,uuid,name,phone,email,
street,city,state,zip) VALUES(3,'12ee5bbf-dabb-4d95-bfe7-6e6f14702add',
'Patrice Beregeron','978-555-1212','pBeregero#BostonBruins.com',
'1 causeway street','Boston','Mass','01236');)
#################################################################
The exception says it all, your table has no column name uuid created. So uninstall the app, then run it again. This error occurs only if the column is not generated.
The error message in you log clearly states the problem. The contacts table does not have a column uuid. You now could do the following:
Check if you have got right lower case vs. upper case. The column in question might be UUID instead of uuid (I don't know SQLite, but case matters in many database systems).
If the column really is not in the table yet, then add it (either by code or by hand). Read SQLite's documentation to learn how to that (I can't help you here because I don't know SQLite).
Uninstalling and re-installing the app would help only if the app would generate the database upon installing or during the first run. Since we don't know anything about the app, this might or might not be the case.
You have not added the column "uuid" in the create table "contacts" query like this :
String createTableContacts = "create table contacts ( ' uuid text ')";
I want to write an sqlite query in which a record will be displayed for example i have a contact table and account table i want to fetch a acount name and contact name from table i want to group_concat the contact name and it should not be repeated so i return a query:
select a.account_name, group_concat(DISTINCT c.contact_name) from account_table a join contact_table c on a.account_id = c.account_id;
this query execute perfectly know what i want to do is to get group_concat distinct name in asc order so i written the query :
select a.account_name, group_concat(DISTINCT c.contact_name order by c.contact_name) from account_table a join contact_table c on a.account_id = c.account_id;
its giving me error at order by
10-25 10:29:25.601: E/SQLiteLog(2214): (1) near "order": syntax error
can any one tell me how to solve the error.
You can't use ORDER BY clause inside aggregate function call.
Try:
select a.account_name, group_concat(DISTINCT c.contact_name)
from account_table a
join contact_table c on a.account_id = c.account_id
order by c.contact_name;
I have been trying to execute a query:-
String selectQuery="SELECT "+ROLE+" FROM "+TABLE_EMPLOYEE+ " WHERE "+USER_ID+ "='"+userId+"' AND "+PASSWORD+"='"+password+"';";
cursorObj = dbObj.rawQuery(selectQuery, null);
This will result to:-
SELECT Role FROM employee WHERE User_Id='HondaSE' AND Password='456';
The logcat says:-
01-08 12:05:10.070: W/System.err(9318): android.database.sqlite.SQLiteException: no such column:
HondaQE: , while compiling: SELECT Role FROM employee WHERE User_Id=HondaQE AND Password=123;
I have tried to run the query with double quotes as well, for userId and password. resulting in:-
SELECT Role FROM employee WHERE User_Id="HondaSE" AND Password="456";
However both the queryies work perfectly fine when executed in SQLITE Data browser.
Both respond with same error.
Your logcat shows as below.
while compiling: SELECT Role FROM employee WHERE User_Id=HondaQE AND Password=123;
there is no single quotes around strings in User_Id=HondaQE AND Password=123.
You weren't compiling your raw query correctly. I would advise you to use "query" instead and use selection args to avoid these mistakes and possible sql injections.
Rewrite like this
String selectQuery="SELECT "+ROLE+" FROM TABLE_EMPLOYEE " WHERE USER_ID ='" + userId + "' AND PASSWORD = '" +password+ "'";
I had the closed the db before accessing the cursor. This was the error.
After accessing the cursor, the db is to be closed. Error solved.
I'm working with the Android SQLite db, and I have a table with messages in it. Each message has a ContactId. I need to get a list of the newest message associated with each ContactId.
My query looks like this so far:
SELECT * FROM messages GROUP_BY ContactId HAVING ( COUNT(ContactId) = 1 ) ORDER_BY MessageTime DESC
However when I run the query I get this exception:
near "ContactId": syntax error: , while compiling: SELECT * FROM messages GROUP_BY ContactId HAVING ( COUNT(ContactId) = 1 ) ORDER_BY MessageTime DESC
Here's the table definition in case it helps:
create table messages (_id integer primary key autoincrement, ContactId text not null, ContactName text not null, ContactNumber text not null, isFrom int not null, Message text not null, MessageTime int not null);
NOTE: My answer below does not appear to be working as of SQLite 3.7.5, I suggest using the "JOIN" query suggested by Larry.
You are close. What you need to do is sort all the records first, using a subquery table, and then group them. The values that are returned in the result set will be from the last row in each group. So you actually want newer messages to appear at the bottom if you are trying to get the newset message. The "GROUP BY" already ensures you get one row per ContactId.
SELECT * FROM (SELECT * FROM messages ORDER BY MessageTime) GROUP BY ContactId
The HAVING clause is not needed. I haven't used it before but according to the docs the HAVING clause will discard whole groups that don't match, but it doesn't sound like you want any groups discarded, you want results from every ContactId.
Also note there is no underscore in "ORDER BY" or "GROUP BY".
Here are two ways to do it.
This query builds a list of the most recent times for each user, then JOINs that back to the message table to get the message information:
SELECT M1.* FROM messages M1 JOIN
(SELECT ContactId, MAX(MessageTime) AS MessageTime FROM messages GROUP BY ContactId) M2
ON M1.ContactID = M2.ContactID AND M1.MessageTime = M2.MessageTime;
This query does something slightly different. It looks at each message and asks if there exists any later message for the same contact. If not, the row must be the most recent one:
SELECT M1.* FROM messages M1
WHERE NOT EXISTS (SELECT * FROM M2
WHERE M2.ContactID = M1.ContactID AND M2.MessageTime > M1.MessageTime)
I am trying to get all Inbox Messages using ContentResolver based on message ids stored in another table..
I want to exclude all Inbox SMSs whose message id is not in my custom table(spam_msgids).
What i am doing is (I dont know whether this is correct or not):
Uri uriSms=Uri.parse("content://sms/inbox");
Cursor cursor = context.getContentResolver().query(uriSms, null,"_id NOT IN (SELECT msg_id FROM spam_msgids)",null,null);
But its giving me an error though table is already created ...
Error : 07-19 17:04:16.412: ERROR/DatabaseUtils(141):
android.database.sqlite.SQLiteException: no such table: spam_msgids: , while compiling: SELECT * FROM sms WHERE (type=1) AND (_id NOT IN (SELECT msg_id FROM spam_msgids)) ORDER BY date DESC
Can anyone please tell me where i am going wrong ?
You can fetch all IDs from you database, build a comma separated string and pass it to NOT IN so that it would look like:
Cursor cursor = context.getContentResolver().query(uriSms, null,"_id NOT IN (" + stringOfCommaSeparatedStringOfIDs + ")",null,null);
You are querying from android internal database and surely that database doesn't include spam_msgids