0

I dont have much knowledge in using the "Cursor" to read data of SQLite database but using some references and examples i compiled a few to setup this i dont why it would read from the database, i check if data is entered into the database using a database browser and its present can some one tell me how to make this work?

I have a separate class holding the db connections and queries, in that i have a function to get all fields which have a long value of a specifed value

public Cursor getAppointments(long date) throws SQLException
    {
    Cursor mCursor =
    db.query(true, DATABASE_TABLE, new String[] {
    KEY_ROWID,
    APP_TIME,
    APP_TITLE,
    },
    APP_DATE + "="  + date,
    null,
    null,
    null,
    null,
    null);
    if (mCursor != null) {
    mCursor.moveToFirst();
        }
    return mCursor;
    }

what this code is supposed to do is get all the records which have the corresponding date as the queried value and return.

Now in the main class i have this code to catch and display in a Textview, row by row.

 db.open();
       Display.setText("");
       Cursor c = db.getAppointments(SDate);

      if (c.moveToFirst())
          Display.setText(DisplayTitle(c));
       else
       Toast.makeText(this, "No title found",
       Toast.LENGTH_LONG).show();

       db.close();

and the display title method is

private String DisplayTitle(Cursor c) {

        String Final ="";

        Final = c.getString(1)  + " " +c.getString(2) + " " +  c.getString(3)  + "\n" ;

        return Final;

    }

Can someone tell me what are the changes necessary inorder to retrieve the records which have the same long value as the date and display it in the textview, there can be multiple records with the same long value, so have to retrieve all the records with the same long value for date and display it in the textview.

Cheers, Looing for a fast reply. :)

2 Answers 2

2

Use this simple approach of using cursor:

 String q = "SELECT * FROM DATABASE_TABLE where(APP_DATE like '"+date+"')";//your query
 Cursor c = db.rawQuery(q, null); //  db is a object of SQLiteDatabase type  

Then retrieve value of respective column using column name with getColumnIndex() method if you are not sure about column index like:

              if(c.getCount() == 0)
              {                   
                 //No entry found
              }
              else  {
                  c.moveToFirst();
                  do {
                      String mail = c.getString(c.getColumnIndex("KEY_ROWID"))+c.getString(c.getColumnIndex("APP_TIME"))+c.getString(c.getColumnIndex("APP_TITLE")); 
                        // do whatever you like with each record                    
                    } while (c.moveToNext());               
            c.close();
            db.close();

0

You have started reading the cursor from index 1 in DisplayTitle method. It should be 0. Means c.getString(0) and respectively.

2
  • i tried but i get array out of index bounds exceptions and says that the cursor is null.. :S
    – Shiva Bala
    Commented May 27, 2012 at 9:28
  • ok , then as imran ,mentioned above try for a raw query and check cursor size count.
    – Som
    Commented May 27, 2012 at 10:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.