1

I am trying to run a select using sqllite. Its my first time using sqllite and I am getting this error over and over.

function ="Adding Event

CODE:

public List<Example> getExampleByFunctionList(String function)
{
    List<Example> examplelist = new ArrayList<Example>();
    String getQuery = "SELECT  * FROM " + MySQLiteHelper.TABLE_EXAMPLE+
        " where "+ MySQLiteHelper.COLUMN_EXAMPLE_FUNCTION +" = "+""+function+"";
    Cursor cursor = database.rawQuery(getQuery, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        Example example = cursorToExample(cursor);
        examplelist.add(example);
        cursor.moveToNext();
    }
    cursor.close();
    return examplelist;
}

ERROR:

02-12 12:37:29.218: E/AndroidRuntime(3165): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tutorial/com.tutorial.ManageCalendar}: android.database.sqlite.SQLiteException: near "Events": syntax error: , while compiling: SELECT * FROM example where examplefunction = Adding Events
MPelletier
  • 15,130
  • 14
  • 79
  • 128
user1163234
  • 2,223
  • 5
  • 31
  • 56

1 Answers1

1
... +" = '"+function+"'";
        ^^^          ^^^

You need to quote string literals in SQL statements (regardless of whether they contain spaces or not).

Or use prepared statements and bind variables, which is much safer against SQL injection.
See: How do I use prepared statements in SQlite in Android?

Community
  • 1
  • 1
Mat
  • 188,820
  • 38
  • 367
  • 383