Thursday, February 16, 2012

Running a select from sqllite


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

1 comment:

  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?

    ReplyDelete