繁体   English   中英

仅返回SQLite Select Statement中的Last值

[英]Returning only Last value in SQLite Select Statement

在这里,我试图从SQLite中的表中获取所有数据。 但每次它返回我只有两次最后一行(我的表只包含两行)。

e.g
---------------------+
| Id| name | mobile  |                  
|--------------------|
|1  | abc  | 123456  |
|--------------------|
|2  | xyz  | 789654  |
+--------------------+

我的代码返回此:

01-11 00:14:59.291: D/Result:(27629): TID 12274, Name: Tablets , Image: [B@4275d578
01-11 00:14:59.291: D/Result:(27629): TID 12274, Name: Tablets , Image: [B@4275d578

在这里,我粘贴我的查询代码:

public List<ProductCategoryDatabaseRetrieve> getProductCategoryData() {
    List<ProductCategoryDatabaseRetrieve> productCategoryDatabaseRetrieve = new ArrayList<ProductCategoryDatabaseRetrieve>();
    ProductCategoryDatabaseRetrieve prodCatDB = new ProductCategoryDatabaseRetrieve();
    SQLiteDatabase sqliteDB = dbHandler.getWritableDatabase();
    String[] columns = { DatabaseHandler._TID,
            DatabaseHandler.TID,
            DatabaseHandler.PRODUCT_CATEGORY_NAME,
            DatabaseHandler.PRODUCT_CATEGORY_IMAGE };
    Cursor cursor = sqliteDB.query(DatabaseHandler.PRODUCT_CATEGORY_TABLE,
            columns, null, null, null, null, null);
    if (cursor.getCount() > 0 && cursor !=null) {
        while (cursor.moveToNext()) {
            prodCatDB.set_tid(cursor.getInt(cursor.getColumnIndex(DatabaseHandler._TID)));
            prodCatDB.setTid(String.valueOf(cursor.getInt(cursor
                    .getColumnIndex(DatabaseHandler.TID))));
            prodCatDB.setProductCategoryName(cursor.getString(cursor
                    .getColumnIndex(DatabaseHandler.PRODUCT_CATEGORY_NAME)));
            prodCatDB.setProductCategoryImage(cursor.getBlob(cursor
                    .getColumnIndex(DatabaseHandler.PRODUCT_CATEGORY_IMAGE)));
            productCategoryDatabaseRetrieve.add(prodCatDB);
    }
    } 
    dbHandler.close();
    return productCategoryDatabaseRetrieve;
}

非常感谢考虑。

那是因为您正在实例化ProductCategoryDatabaseRetrieve prodCatDB = new ProductCategoryDatabaseRetrieve(); while循环之外,然后在每次循环迭代时替换它的属性值。

移动ProductCategoryDatabaseRetrieve prodCatDB = new ProductCategoryDatabaseRetrieve(); while循环一样

while (cursor.moveToNext()) {
        ProductCategoryDatabaseRetrieve prodCatDB = new ProductCategoryDatabaseRetrieve();//Instantiate here with each iteration.
        prodCatDB.set_tid(cursor.getInt(cursor.getColumnIndex(DatabaseHandler._TID)));
        prodCatDB.setTid(String.valueOf(cursor.getInt(cursor
                .getColumnIndex(DatabaseHandler.TID))));
        prodCatDB.setProductCategoryName(cursor.getString(cursor
                .getColumnIndex(DatabaseHandler.PRODUCT_CATEGORY_NAME)));
        prodCatDB.setProductCategoryImage(cursor.getBlob(cursor
                .getColumnIndex(DatabaseHandler.PRODUCT_CATEGORY_IMAGE)));
        productCategoryDatabaseRetrieve.add(prodCatDB);
}

此外,在if语句中, cursor != null是无用的。 游标永远不会为null,即使它是cursor.getCount()也会在到达cursor != null之前抛出空指针异常。 删除cursor != null ,您不需要它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM