How I should write my sql query in android that special characters work correct. For example when I filter String with like which has % symbol, it doesn't work correct.
For LIKE, you choose in the query what to escape them with;
SELECT * FROM Olle WHERE name LIKE 'Many ^% have fallen' ESCAPE '^';
...will only match the actual character % since it's been escaped with the ^ that is give in the ESCAPE clause.
SQLfiddle here.
Related
I am coding a dictionary project. Its can translate english to arabic or arabic to english. Words are stored in sqlite fts4 database.
Arabic letter in database stored html encoded like
غير
When i use fts4 query syntax in english to arabic for example => stor
SELECT * FROM fts_dic WHERE english MATCH '"^stor*"';
Returned results are good for me like
store
stored
storage
But when i search arabic to english for => غير
SELECT * FROM fts_dic WHERE english MATCH '"^غير*"';
Returned results
ظغير
֎غيرظ
But i want to see result only start with my searched html encoded text like
غيرخ
غيرٗ
I use "^" at the begining of the word to get this result as you see. In english to arabic works fine but arabic to english not working properly.
The FTS documentation says:
A term is a contiguous sequence of eligible characters, where eligible characters are all alphanumeric characters and all characters with Unicode codepoint values greater than or equal to 128. All other characters are discarded when splitting a document into terms. Their only contribution is to separate adjacent terms.
In other words, punctuation characters like &#; are completely ignored; what FTS sees are the three words 1594, 1610, and 1585.
In the FTS table, you should not HTML-encode anything; just use the plain Unicode characters.
Furthermore, ^ works only in FTS4 tables (which may not be available in all Android versions).
I newt o regular expressions and been using tutorials, but the regular express I have works sometimes, but doesn't all the time. I am getting my numbers out of the contact list from my android phone. I am trying to get rid of all spaces, '(', ')', and '-'
For example:
1. (555) 867-5309 -> 5558675309
2. 1555-555-5555 -> 15555555555
3. 555-555-5555 -> 5555555555
This is the line I am using
String formatphone = contactPhone.replaceAll("\\s()-","");
For some numbers it only returns number and sometimes it doesn't change the format.
Is it correct? Do i need to format something because I am taking it out of the phone's contact list?
Put the desired characters in a character class:
String formatphone = contactPhone.replaceAll("[ ()-]","");
Ensure that you put the hyphen - at either end.
Try using this:
String formatphone = contactPhone.replaceAll("^.*[\\s\\(\\)-].*", "");
As a regular expression you're defining a set using []. In that set you include any character you want to be replaced. As ( and ) are special meaning characters, you have to escape them. As the - is a special character used to design ranges, it has to be the last character of your set, so if nothing is behind it, it's not a range, but just that character (you could escape it too, though).
Actually question was asked several times, but I didn't manage to find answer.
There's set of SQLite table(s) which are read-only - I can't change their structure or redefine collation rules. Tables consisting some international characters (Russian/Chinese, etc).
I would like to get some case-insensitive selection like:
select name from names_table where upper(name) glob "*"+constraint.toUpperCase()+"*"
It works only when name is latin/ASCII charset, for international chars it doesn't work.
SQLite's manual reads:
The upper(X) function returns a copy of input string X in which all
lower-case ASCII characters are converted to their upper-case
equivalent.
So the question is: how to resolve this issue and make international chars in upper/lower case?
This is known problem in sqlite. You can redefine built-in functions via Android NDK. This is not a simple way. Look at this question
Notice that indexes of your tables will not work (for UDF) and query can be very slow.
Instead of it you can store your data (which you look for) in other column in ascii format.
For example:
"insert into names_table (name, name_ind) values ('"+name+"',"+"'"+toAsciiEquivalent(name)+"')"
name name_ind
----------------
Имя imya
Name name
ыыы yyy
and search string by column name_ind
select name from names_table where name_ind glob "*"+toAsciiEquivalent(constraint)+"*"
This solution requires more space for data, but it is simple and fast.
Instead of providing full Unicode case support by default, SQLite provides the ability to link against external Unicode comparison and conversion routines. The application can overload the built-in NOCASE collating sequence (using sqlite3_create_collation()) and the built-in like(), upper(), and lower() functions (using sqlite3_create_function()). The SQLite source code includes an "ICU" extension that does these overloads. Or, developers can write their own overloads based on their own Unicode-aware comparison routines already contained within their project.
Reference: http://www.sqlite.org/faq.html
On Android Jellybean 4.1 is it possible to escape a LIKE wildcard character and still have the like use the index?
Use:
where field like 'xyz\_abc'
as opposed to:
where field like 'xyz_abc'
Does escaping wildcards work on Android? And will it still use the index if the wildcard is escaped?
What I am currently doing is:
where field like 'xyz_abc' and lower(field) = lower('xyz_abc')
Which is horribly inefficient due to the wildcard character.
Thanks
You need to use the ESCAPE clause:
where field like 'xyz\_abc' escape '\'
See the section The LIKE and GLOB operators in the SQLite Documentation.
In the SQLite database, I have stored all values in uppercase.
How can I select the specified value in the database using lower case?
For anyone else that arrived here from Google looking how to select as upper case in SQLite, as expected, this will work:
sqlite> SELECT UPPER("Hello, WORLD!");
HELLO, WORLD!
https://sqlite.org/lang_corefunc.html#upper
SQLite has a LOWER function for this:
sqlite> SELECT LOWER("Hello, WORLD!");
hello, world!
The lower(X) function returns a copy of string X with all ASCII characters converted to lower case. The default built-in lower() function works for ASCII characters only. To do case conversions on non-ASCII characters, load the ICU extension.