Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
774 views
in Technique[技术] by (71.8m points)

android - SQLite Getting only last record not all the Records

I have total 4 records in ecare table, where 2 records status is sent = 1, and other 2 records status is sent = 0

RECORDS

1st Record : Track 1 [sent = 1]

2nd Record : Track 2 [sent = 1]

3rd Record : Track 3 [sent = 0]

4th Record : Track 4 [sent = 0]

But getting only last record (i.e. - Track 4) not all two records those status is sent = 0

Here is the SQLite query, which I am using to fetch data from database

public synchronized List<String> getECareData() {

        String sql = "SELECT p.sent,e.*, e.no _id from ecare e LEFT JOIN pweb p ON e.h_id=p.h_id WHERE p.sent = '0' GROUP BY e.h_id";

        List<String> records = new ArrayList<String>();
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            records.add(cursor.getString(0));
        }
        return records;
    }

UPDATED

 public synchronized List<String> getECareData() {

    String sql = "SELECT p.sent,e.*, e.no _id from ecare e LEFT JOIN pweb p ON e.h_id=p.h_id WHERE p.sent = '0' GROUP BY e.h_id";

    List<String> records = new ArrayList<String>();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(sql, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            records.add(cursor.getString(0));
        } while (cursor.moveToNext());
    }

    Log.d("records:", records.toString());

    return records;
}

Log says:

D/records:: [Getting only LAST record here] the 4th one

I guess, there is something wrong with query, because I am getting only last record in Cursor

UPDATED RECORDS

1st Record : Track 1 [sent = 1]

2nd Record : Track 2 [sent = 1]

3rd Record : Track 3 [sent = 0]

4th Record : Track 4 [sent = 0]

5th Record : Track 5 [sent = 0]

But getting only last record (i.e. - Track 5) not all three records those status is sent = 0

Log says:

D/records:: [Getting only LAST record here] the 5th one

UPDATED QUERY - Removed GROUP BY from query

And now getting each and every record, even those belongs to sent = 1 whereas I just want to fetch data that belongs to sent = 0 only

String sql = "SELECT p.sent,e.*, e.no _id from ecare e LEFT JOIN pweb p ON e.h_id=p.h_id WHERE p.sent = '0' ";
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your missing startManagingCursor() method.

  Cursor cursor = db.rawQuery(sql, null);
  cursor.getCount();
  startManagingCursor(cursor);//the important line
      cursor.moveToFirst();
      if(cursor.getCount() == 0)
      {
           //error no records
      }
      else
      {
      for(int i=0;i<cursor.getCount();i++)
      {
              records.add(cursor.getString(0));
          adapter.add(aString);
          cursor.moveToNext();
      }
      cursor.moveToFirst();
      }

NOTE: startManagingCursor() is deprecated because it does operations on the main thread which can freeze up the UI and deliver a poor user experience. You should use a CursorLoader with a LoaderManager instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...