2

I'm working very well with the SQLite database, but now I have a problem while using a query. For example, if use a query like this:

SQLiteDatabase db = mHelper.getReadableDatabase();

    String parameter = mypar;
    String sql = SELECT color, name FROM table WHERE name = '" + parameter + "';
    Cursor c = db.rawQuery(sql, null);
   while (c.moveToNext()) {
   String color = c.getString(0);
   String name = c.getString(1);
}
        c.close();
        db.close();

Everything works fine

But if the parameter has an apex

String parameter = mypar';

I get an exception

Caused by: android.database.sqlite.SQLiteException: near "r": syntax error (code 1):

How can I solve this problem? Thank you

2
  • pos the exception and full code, Commented Mar 31, 2016 at 13:48
  • how are you executing the query? are you using .rawQuery? if so, you should use .query, and pass it as a parameter. It is also good to prevent sql injections. Commented Mar 31, 2016 at 13:50

1 Answer 1

1

this is my solution, works fine!! try it

String parameter = mypar';
String sql = SELECT color, name FROM table WHERE name = ?;
Cursor c = db.rawQuery(sql, new String[] { parameter });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.