![](/img/trans.png)
[英]TinkerPop multiple .by() only returning the last .by() value
[英]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.