Problem while reading contacts with ContactsContract.Contacts.CONTENT_URI - android

I need to search a particular name from ma contacts... for that i gave the selection criteria like this
ContactsContract.Contacts.DISPLAY_NAME + " LIKE '"+constraint+"%'"
But for apostrophe(')... means ...when i gave apostrophe as constraint an (SQL error )exception is thrown.. for all other special characters , the search is working..
How can we handle constraint apostrophe(')... in search criteria.. boz we can have contact names having apostrophe in it (Eg: Jennifer'lo'pe_z)

How do I use a string literal that contains an embedded single-quote (') character?
The SQL standard specifies that single-quotes in strings are escaped by putting two single quotes in a row. SQL works like the Pascal programming language in the regard. SQLite follows this standard.
Example:
INSERT INTO xyz VALUES('5 O''clock');
SO, simply for your solution, use '' at the place of '. like
if(constraint.contains("'"))
constraint = constraint.replace("'", "''");
And then execute your query. This must help you.
Happy coding :)

Related

Natural sorting of alphanumeric values in sqlite using android

I have a list of names of starts with characters and end with numbers like: -
ka1, ka10, ka 2, ka, sa2, sa1, sa10, p1a10, 1kb, p1a2, p1a11, p1a.
I want to sort it in natural order, that is: -
1kb, ka, ka1, ka 2, ka10, p1a, p1a2, p1a10, p1a11, sa1, sa2, sa10.
The main problem I am seeing here is no delimiter between text and numeric part, there also a chance of without numeric part also.
I am using sqlite in android, I can do sorting using java after fetching points by cacheing cursor data, but I am using(recommended to use) cursor adapter.
Please suggest a query for sorting or is there any way to apply sorting in cursor?
I tried below query for Natural sorting:
SELECT
item_no
FROM
items
ORDER BY
LENGTH(item_no), item_no;
It worked for me in Sqlite db too. Please see this link, for more details.
I can propose using regex replacement adding zeros, creating temporary table of original and corresponding values, then follow this link for sorting it: http://www.saltycrane.com/blog/2007/12/how-to-sort-table-by-columns-in-python/
tip for regex add as many zeros after last letter, but limit the number of total digits for predicted maximum number of digits. If You need help with regex as well, provide exact info of valid and invalid values, so can halp with that too.
PS if want to be sure that zeros goes before last digits search for char from the end
Updated
You can use different ways - Some of are mentioned below:
BIN Way
SELECT
tbl_column,
BIN(tbl_column) AS binray_not_needed_column
FROM db_table
ORDER BY binray_not_needed_column ASC , tbl_column ASC
Cast Way
SELECT
tbl_column,
CAST(tbl_column as SIGNED) AS casted_column
FROM db_table
ORDER BY casted_column ASC , tbl_column ASC
or try the solution:
There are a whole lot of solutions out there if you hit up Google, and
you can, of course, just use the natsort() function in PHP, but it's
simple enough to accomplish natural sorting in MySQL: sort by length
first, then the column value.
Query: SELECT alphanumeric, integer FROM sorting_test ORDER BY LENGTH(alphanumeric), alphanumeric from here

SQLite select statement is not working when a column contains the '?' or the 'x' character

I noticed that whenever I try to execute an sqlite query on a column that contains strings in which the '?' (due to encoding errors) or the 'x' character is contained, the matching fails.
Does anyone know why? I suspect the second case has something to do with the relative hexadecimal symbol. This holds for either sqlite version 3.7.9 and 3.8.6.
Thanks in advance
Edit:
SELECT FM.Foodname FROM Foodtable AS FM WHERE Foodname MATCH 'Alom*' UNION ALL SELECT F2.Foodname FROM Foods_units_2 AS F2 JOIN Foodtable ON Foodtable.Foodname = F2.Foodname ORDER BY F2.Foodname;
The problem is withe last statement (Foodtable.Foodname = F2.Foodname) where the Foodtable contains all records and Foods_units_2 contains part of records with different units of measurement. In both tables there are Foodname strings that contain the characters '?' (due to encoding errors) and 'x'. In thoses cases the matching fails and sqlite replaces the wrong strings with others
For example in the first case I have "Cr?me Kαραμελέ" where '?' is supposed to be 'è' and in the second case "4?4 compact" where '?' is supposed to be 'x'.
Well, I think I made a mistake. The reason the matching failed was because the main table included records where '?' was present (encoding errors), while Foods_units_2 table did not.
Anyway, hank you guys for your promptness and sorry for the inconvenience.

Android sqlite query names with diacritics

I have the following problem:
I try to query from the database the names that contain characters with diacritics.
The selection is:
String like = "ë";
String selection = "LOWER(name) LIKE \"%" + like + "%\"";
The problem is even if i have rows that contain "Ë" the query is empty. Any ideas?
And I do not want to build a separate column for normalized names.
From SQLite DOC :-
(A bug: SQLite only understands upper/lower case for ASCII characters by default. The LIKE operator is case sensitive by default for unicode characters that are beyond the ASCII range. For example, the expression 'a' LIKE 'A' is TRUE but 'æ' LIKE 'Æ' is FALSE.)
So, you should replace diacritics before using with like. Remove diacritics from string in Java is good SO to do that. Or to use lower and either cases separate.
try this
String selection = "LOWER(name) LIKE \"'%" + like + "%'\"";

Android sql usable characters for creating table

What are the not suggested characters the user can include in a table name?
I give the user the opportunity to create tables in my app, but I want to prevent force closes deriving from including bad characters in the table name.
What are these characters? Any how can I include them in an array?
e.g. List<String> usable_chars = Arrays.asList(";", "'", "/");
but it gives me an error for "\" and """, what is the right syntaxis for these in a list?
Thanks
In java you will need to escape certain characters.
See "Escape Sequences"
http://docs.oracle.com/javase/tutorial/java/data/characters.html

What characters cannot be used for values in SQLite databases?

I'm making an Android app and I have used an SQLite database. But I found out if you type characters like single quotes ('), (also for using as the primary key) the data won't be saved/retrieved correctly.
Is it a problem with me or is it true? If its true are there any more characters like that?
Thanks.
#bdares and #mu Thanks for the tips, but can you please tell me how to use placeholders and/or prepared statements in SQLite?
I have always used direct String concatenation before but now, as it appears that's a bad practice, I would like to use prepared statements and/or placeholders.
Possibly you'll have problems with characters like ASCII STOP and such non-printing characters, but if you use prepared statements and parameter binding, you won't have any trouble even with characters like '.
If you don't want to use parameter binding and prepared statements, you can replace all of your input ' with \' and you'll be fine.
SQL typically uses ' as its special character to tell when a string literal starts or stops. If your input has this character, it will stop treating the current line as a string and start treating it as commands. This is not a good thing, security wise. It also keeps you from inputting that character unless you "escape" it by placing a backslash in front of it, which tells SQL to ignore the ' and continue treating the following characters as a string until an unescaped ' is met. Of course, backslash literals are also escaped as double-backslashes.
Prepared statements typically look like this:
String sql = "INSERT INTO MYTABLE (NAME, EMP_NO, DATE_HIRED) VALUES (?, ?, ?)";
PreparedStatement ps = sqlite.prepareStatement(sql);
ps.setString(1, myString);
ps.setInt(2, myInt);
ps.setDate(3, myDate);
ps.executeUpdate();
Unfortunately, I don't know exactly what library you'd be using to access sqlite from Android, so I can't give you more details at this time.
SQLite statements use quotes -- single or double -- for strings. If you need to INSERT a string with (') for example, you can use double quotes (") to wrap the string:
INSERT INTO my_table (some_column) VALUES("'a string'");
Or the other way around:
INSERT INTO my_table (some_column) VALUES('"a string"');
(Of course, you will need to escape any (") in your Java code.)
An alternative is to use a SQLiteStatment (Prepared statement) and bindString()
As for the "characters allowed", SQLite internally stores strings (type TEXT) as UTF-8 or UTF-16. Android's build uses the default of UTF-8. Therefor, you can store any string you like.
SQLite supports the data types TEXT (similar to String in Java), INTEGER (similar to long in Java) and REAL (similar to double in Java). All other types must be converted into on of these fields before saving them in the database. SQLight itself does not validate if the types written to the columns are actually of the defined type, you can write an integer into a string column.

Categories

Resources