我在 ecare 表中总共有 4条记录,其中2 records状态为已发送= 1 ,而其他 2条记录状态为sent = 0
I have total 4 records in ecare table, where 2 records status is sent = 1, and other 2 records status is 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]但是仅获取最后条记录(即-曲目4 )而不是所有两个记录的状态为已发送= 0
But getting only last record (i.e. - Track 4) not all two records those status is sent = 0
这是SQLite查询,我正在使用该查询从数据库中获取数据
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; }已更新
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我想 query 中有一些wrong,因为我在 Cursor
I guess, there is something wrong with query, because I am getting only last record in Cursor
更新记录
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
But getting only last record (i.e. - Track 5) not all three records those status is sent = 0
日志说:
D/records:: [Getting only LAST record here] the 5th one更新查询-从查询中删除了GROUP BY
UPDATED QUERY - Removed GROUP BY from query
现在获取每条记录,甚至那些属于 sent = 1 的记录,而我只想获取仅属于 sent = 0 的数据
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' ";推荐答案
您缺少的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代替.
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.
更多推荐
SQLite仅获取最后一条记录,而不是所有记录
发布评论