SQLite只获取最后一条记录而不是所有记录

时间:2016-07-28 06:44:56

标签: android sqlite join left-join

我在 ecare 表格中有 4条记录,其中2 records状态为已发送= 1 ,其他 2条记录状态为sent = 0

记录

1st Record : Track 1 [sent = 1]

2nd Record : Track 2 [sent = 1]

3rd Record : Track 3 [sent = 0]

4th Record : Track 4 [sent = 0]

但只获取最后记录(即 - Track 4 )而不是所有两个记录这些状态已发送= 0

这是SQLite查询,我用它来从数据库中获取数据

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;
    }

已更新

 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;
}

日志说:

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

我想,查询wrong,因为我只获得了光标

中的最后一条记录

更新的记录

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]

但只获得最后记录(即 - Track 5 )而非所有三个记录这些状态已发送= 0

日志说:

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

更新的查询 - 从查询中删除了GROUP BY

现在获取每条记录,即使属于 sent = 1 ,但我只想获取属于 sent = 0 的数据

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' ";

3 个答案:

答案 0 :(得分:3)

您遗失的startManagingCursor()方法。

  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();
      }

注意: 不推荐startManagingCursor(),因为它在主线程上执行操作,这可能会冻结UI并提供糟糕的用户体验。您应该使用CursorLoader代替LoaderManager

答案 1 :(得分:0)

我会尝试:

String sql = "SELECT p.sent,e.*, e.no _id 
              FROM ecare e, pweb p 
              WHERE e.h_id=p.h_id 
              AND e.sent = '0' ";

答案 2 :(得分:0)

我试图重新创建你的数据库,这是对的,有什么问题,似乎有效。

CREATE TABLE ecare (_id INTEGER PRIMARY KEY,h_id  INTEGER ,no  INTEGER);
CREATE TABLE pweb (_id INTEGER PRIMARY KEY,h_id  INTEGER ,sent  INTEGER);
sqlite> insert into ecare values(1, 10,1);
sqlite> insert into ecare values(2, 20,2);
sqlite> insert into ecare values(3, 30,3);
sqlite> insert into ecare values(4, 40,4);
sqlite>
sqlite> insert into pweb values(1, 10,1);
sqlite> insert into pweb values(2, 20,1);
sqlite> insert into pweb values(3, 30,0);
sqlite> insert into pweb values(4, 40,0);
sqlite> select * from ecare;
1|10|1
2|20|2
3|30|3
4|40|4
sqlite> select * from pweb;
1|10|1
2|20|1
3|30|0
4|40|0
sqlite> 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';
0|3|30|3|3
0|4|40|4|4
sqlite>