user2847219
user2847219

Reputation: 555

Android SQLiteException parameter

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

Upvotes: 2

Views: 39

Answers (1)

Debora Carnevali
Debora Carnevali

Reputation: 28

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 });

Upvotes: 1

Related Questions