-1

I want to read category id from men's wear from table category so i write query like this.

String q = "SELECT " + MENU_CAT_ID + " FROM " + CATEGORY_TABLE + " WHERE " + MENU_NAME + " = '" + name + "'";

but i got an error

android.database.sqlite.SQLiteException: near "s": syntax error (code 1): , while compiling: SELECT menu_cat_id FROM category_table WHERE menu_name = 'Men's  Wear'

what can i do this case?

chirag patel
  • 229
  • 4
  • 16

1 Answers1

1

You need to escape the single quote in Men's Wear by doubling the single quote like this Men''s Wear.

When you are building your sql String you can use DatabaseUtils.sqlEscapeString(...) to escape it for you like below:

String q = "SELECT " + MENU_CAT_ID + " FROM " + CATEGORY_TABLE 
         + " WHERE " + MENU_NAME + " = " + DatabaseUtils.sqlEscapeString(name);
George Mulligan
  • 11,813
  • 6
  • 37
  • 50