How to Escape Special Characters Like ' in SQLite in Android

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



Leave a reply



Submit