How to escape special characters like ' in sqlite in android
First replace char with this
difficulty=difficulty.replaceAll("'","\\'");
then pass it in your query
"select * from Questions_answers where CHAPTERS='"+difficulty+"'";
Edit :
q = "select * from Questions_answers where CHAPTERS = ?";
database.rawQuery(q, new String[] { difficulty});
Escape special characters in sqlite
You should be using the method update
instead of generating the SQL yourself. The built-in methods will do all the needed escaping for you.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
How to escape special characters in sqlite in android?
use double single quotes 'Scr''anton'
How to avoid special characters in android SQLite?
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.
SQLite query that contains special characters
String literal values in SQL need to be in 'single quotes'
.
However, it's better to use ?
placeholders for literals and bind the values, e.g.
Cursor c = db.rawQuery("SELECT * FROM " + DataBaseHelper.TABLE_COM + " WHERE "
+ DataBaseHelper.COLUMN_USER_ID + " = ?", new String[] { value });
How to find list of escape character for sqlite
SQL does not use backslashes for escaping.
When string literals are written directly in the SQL command, they are delimited with single quotes; any single quote inside the string must be doubled.:
cursor = db.rawQuery("SELECT * FROM MyTable WHERE Text = 'with '' quote'",
null);
Table/column names can be delimited with double quotes; any double quote in the table/column name must be doubled.
If you use parameters (which is stronly recommended), you do not need to escape anything:
cursor = db.rawQuery("SELECT * FROM MyTable WHERE Text = ?",
new String[]{ "with ' quote" });
Related Topics
Android: Taking Complete Control of Phone(Kiosk Mode), Is It Possible? How
Calling Activity Class Method from Service Class
Android Canvas: Drawing Too Large Bitmap
Android Inject_Events Permission
Get Contact Info from Android Contact Picker
Could Not Find Com.Android.Tools.Build:Gradle:3.0.0-Alpha1 in Circle Ci
Pass Data from Activity to Service Using an Intent
Spinner: Get State or Get Notified When Opens
Published App on Play Store Can't Communicate with Google Maps API and Facebook API
Too Many Field References: 70613; Max Is 65536
What Does Google-Services.JSON Really Do
How to Connect Android App to MySQL Database
Android: Remove Notification from Notification Bar
Refreshing Oauth Token Using Retrofit Without Modifying All Calls
Android Paint: .Measuretext() VS .Gettextbounds()
Why Extend the Android Application Class