Kindly note that I am trying to retrieve values from table in SQLite DB in my android app, on a button click event and present it through a ListView.I am able to insert records into the table on same event.Although it throws the following error:
`05-15 11:54:16.721: W/System.err(1834): android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
05-15 11:54:16.721: W/System.err(1834): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:434)
05-15 11:54:16.721: W/System.err(1834): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
05-15 11:54:16.721: W/System.err(1834): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
`
Follwing is the chunk of code I am using to Insert,Retrieve and Display the records from table in ListView:
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
event=spinner.getSelectedItem().toString();
message=ed1.getText().toString();
MainActivity.myDataBase.openDatabase("/data/data/org.mithil.appname/databases/tablename", null,MainActivity.myDataBase.OPEN_READWRITE);
MainActivity.myDataBase.execSQL("INSERT INTO " +
MainActivity.tablename +
" Values ('"+ finalnumber +"','"+ name +"','"+ message +"','"+ event +"','"+ ldate +"');");
Cursor cu = MainActivity.myDataBase.rawQuery("SELECT Name FROM " + MainActivity.tablename + " WHERE Date =" + ldate +";" , null);
cu.moveToFirst();
Log.d("first rec", cu.toString());
while(cu!=null)
{
try{
lstitem.add(cu.getString(0));
cu.moveToNext();
}
catch(Exception e)
{
e.printStackTrace();
}
}
All this is happening in a button click.The insertions are getting reflected in table but the 'Select' is not.I reckon something is wrong with moving the cursor.The table consist of five fields off which I want to retrieve only one ie. 'Name' field and add the name into the ListView.Kindly correct me where I am going wrong.Thanks a lot.