How to escape special characters like ' in sqlite in android How to escape special characters like ' in sqlite in android android android

How to escape special characters like ' in sqlite in android


The SQL standard specifies that single-quotes in strings are escaped by putting two single quotes in a row. SQL works like the Pascal programming language in the regard. SQLite follows this standard. Example:

INSERT INTO xyz VALUES('5 O''clock');

Ref : SQLite FAQ


The best way is to use a native Android method designed for exactly this purpose:

DatabaseUtils.sqlEscapeString(String)

Here is the documentation for it online:

The main advantage of using this method, in my opinion, is the self-documentation because of the clear method name.


What worked for me was

if (columnvalue.contains("'")) {    columnvalue = columnvalue.replaceAll("'", "''");}