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

编程入门 行业动态 更新时间:2024-10-26 20:32:31
本文介绍了SQLite仅获取最后一条记录,而不是所有记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在 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仅获取最后一条记录,而不是所有记录

本文发布于:2023-10-20 13:01:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1511039.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   SQLite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!