Android SQLite LIKE escape wildcard - android

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.

Related

Combination of rules with Regex

In an android project, im trying to validate a password that the user inputs, and it must follow some rules
The rules are:
it must have 7 characters and 3 of the following conditions
**
-One lowercase character
-One uppercase character
-One number
-One special character
**
for example:
asd123!!!
PPPppp000
TTT999###
i was trying with this regex
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!#$%^&*-]).{7,}+$
but this enforces all rules at same time.
The approach is wrong here. The regex you created looks like a monster from under the bed, and is highly illegible even for someone regex-literate.
Why not split it into 4 (or as much as there are rules) regexes and check against whether 3 of them return a match? Not only will you make your regexes cleaner, but you will be able to add more rules if need be without changing whole regex.
You can also use inbuilt methods for checking (if applicable under Android development kit).
Some pseudocode would look like this:
result1 = Regex.IsMatch(password, rule1regex)
result2 = Regex.IsMatch(password, rule2regex)
...
resultN = Regex.IsMatch(password, rule3regex)
if(three_out_of_four_rules_apply)
password_valid = true
You can also apply method suggested in comments by #pskink and iterate over each character of a password and set the output accordingly.
Without going into the details of your lookaheads (which seem correct), here's how you would need to implement "three out of four criteria" in pure regex :
(?=.*A)(?=.*B)(?=.*C)|(?=.*A)(?=.*B)(?=.*D)|(?=.*A)(?=.*C)(?=.*D)|(?=.*B)(?=.*C)(?=.*D)
You can test it here.
Factorizing doesn't really make it better :
(?=.*A)(?:(?=.*B)(?=.*(?:C|D))|(?=.*C)(?=.*D))|(?=.*B)(?=.*C)(?=.*D)
I obviously recommend using a higher level language to implement these sorts of constraints.

android regular expression to match an address

In my android app i have to make a control on a edit text that contains an address,
for now my control is:
Indirizzo.toString().matches("[\\sa-zA-Z0-9]*")
But I must also add other characters to control,
such as: \ . n ° ,
How can I change the regular expression?
You might consider using something like so: [\sa-zA-Z0-9\\\.n°/]*.
That being said, that particular regular expression will also match an empty field. If you do not want this, simply replace the * (matches 0 or more repetitions of) with + (matches one or more repetitions of).
The \ is a special character, which needs to be escaped, hence the extra \ infront of it. The . is also a special character which also needs to be escaped.
So, in Java, you would need to use something like so: [\\sa-zA-Z0-9\\\\\.n°/]*
You can make the regex accept all ASCII characters by using the following character class:
[\x00-\x7F]
Or, if you'd like to match those specific characters only, that is \ . n ° ,, simply add them to the character class:
[\\sa-zA-Z0-9\.n°,]*
Also, you should use + rather than * to eliminate zero-char matches.

SQLite upper() alike function for international characters

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

How to avoid special characters in android SQLite?

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.

How to escape special characters in sqlite in android?

I ma trying to insert some data into the database. The data contains single quotes. I tried to escape them with back slash like "Scr\'anton" while inserting the data:
INSERT INTO employee VALUES(4,'Jim','Halpert','Assistant Regional Manager','Manage',2,'Scr\'anton','570-222-2121','570-999-1212','jim#dundermifflin.com','halpert.jpg')
I tired to use DatabaseUtils.sqlEscapeString() with no effect at all. What is the best way to escape characters?
use double single quotes 'Scr''anton'
Take a look at StringEscapeUtils (details here), found in the Lang component of Apache Commons. You can easily download this and add it to your project.
You can use the method update
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

Categories

Resources