How to escape special characters in sqlite in android? - 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

Related

How to display tabs(whitespace) coming from MySQL database correctly on both in Android and IOS app?

I have following data in my db row:
Espresso: 7,00
Double Espresso: 8,00
Ristretto: 7,00
Espresso Machiato: 8,00
Espresso Con Panna: 8,00
I write it on Word, then copy & paste to MySQL editor. When I save it, my IOS and Android apps cannot show the prices aligned because of the tab characters.
What is the best way to do that?
The best way- those 2 things are different data and are in different columns in your database, I would expect (if not, you need to fix your schema). So put the 2 strings in separate TextViews, and align the text view in xml.
First you can split the data from tab character and use formatting like below in java. I believe in objective-c it should be similar.
String ehe = String.format("%-20s : \t %4dTL \n","ehemehe",23);
String ehe2 = String.format("%-20s : \t %4dTL \n","ehemeheadawd",44);
System.out.println(ehe);
System.out.println(ehe2);
Output it produces is
ehemehe : 23TL
ehemeheadawd : 44TL
replace the "/t" chars with "" before you display the text. by using
String.replace("/t","");
Use a fixed-width font if you have precalculated the number of spaces.

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

Android SQLite LIKE escape wildcard

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.

how to add smilies/emotions in my edittext for a notes app in android?

can any one know about how to add/insert emotions/smiles to text(which ever i typed in my edit text for my notes). i have little confusion about if i want to add these smiles of type .png in to edit text type of string, is it possible? and also i have to save these input into sqlite database. normally i know to store string data taken from edit text.
but along with that text i want to also add smiles symbols where ever my cursor placed and to be store in sqlite data base. and get it back to read.
so guys any ideas, most welcome!
Try to use java (i.e. android) spannable method to implement smiley (i.e.) images for that. You will surely get it, search in google for "how to add images/smiley with java spannable method in android?" you will get good idea.
Reading your question the first thing I can think of is Mapping each image to a sequence of letters, for example :) is smiley.png etc. Now your database also has these smaller representation However while reading from the database you can convert those special character sequences to appropriate image.
For simplicity in seraching those Strings You can wrap them in some less used characters i.e. [ or { or <.

displaying special characters in android app from sqlite database

I have a Periodic table of elements app for android that stores most of it's data in string arrays. I am now trying to use an sqlite database instead of the arrays but am having a small problem. If I type 'android:text="¹"' directly into a TextView it will display a superscript 1(like this-> ¹), but if I store '¹' as text in a sqlite database and then use a cursor to populate that same TextView, instead of the superscript 1 being displayed I just see "¹" exactly as I typed it. How can I get the TextView to display special characters when being populated by a sqlite database? I have been struggling for a while and am stumped so any suggestions would be appreciated. Thanks!
Use the Java Unicode string notation for each special character when inserting them into your database.
For '¹', that would be: \u00b9.
Alternatively, to parse HTML tags and character entities like ¹ in a TextView, then you can probably wrap the String in a call to Html.fromHtml() before calling setText().

Categories

Resources