0

Possible Duplicate:
How to escape special characters like ' in sqlite in android

I'm using a form to store some values in an Android's SQLite database. If any of the fields contains the character ' an error occurs so I tried to escape it, but the error is still there.

This is my code:

public boolean createCalendar(String id, String name, String descr) {
    try{
        String newDescr = descr.replace("'", "\\'");
        db.execSQL("INSERT INTO Calendar (id, name, description) VALUES " +
                "('"+id+"','"+name+"','"+newDescr+"');");
       ...
    }
}

And this is the error:

sqlite returned: error code = 1, msg = near "siksjs": syntax error, db=/data/data/com.pfc.app/databases/SSDB
android.database.sqlite.SQLiteException: near "siksjs": syntax error: , while compiling: INSERT INTO Calendar (id, name, description) VALUES ('43779fcb-3d8c-650b-b529-50810506375e','kdldlls','ddk \' siksjs');

How am I supposed to store it?

Thanks!

Community
  • 1
  • 1
PX Developer
  • 8,065
  • 7
  • 42
  • 66
  • use `DatabaseUtils.sqlEscapeString(String)` [as per this answer](http://stackoverflow.com/a/27082084/383414). It was designed precisely for this reason. – Richard Le Mesurier Jul 02 '15 at 06:09

1 Answers1

0

Use

p_query = "select * from mytable where name_field = ?";
mDb.rawQuery(p_query, new String[] { uvalue });

Or

String newDescr = descr.replaceAll("'", "''");
Amit Hooda
  • 2,133
  • 3
  • 23
  • 37