【发布时间】:2011-11-27 20:11:54
【问题描述】:
我目前正在开发一个安卓应用程序。我有一个 sqlite 数据库,它将文本(我只是在我的应用程序中用作字符串)存储在四列中。我正在尝试从表中返回所有行和列。我已经创建了数据并将其插入到表中,并使用 adb shell 中的 sqlite3 验证了它是否存在。我使用与我在程序中使用的语句相同的语句,它返回包含所有正确数据的所有行。在我的程序中,我通过遍历游标以ArrayList<ArrayList<String>> 格式存储所有数据。它返回与行相对应的正确数量的ArrayList<String>,但它们都只有最后一行的信息。这是我的代码:
private static final String SELECT = "SELECT * FROM " + TABLE_NAME;
public ArrayList<ArrayList<String>> allRecipes()
{
ArrayList<ArrayList<String>> results = new ArrayList<ArrayList<String>>();
ArrayList<String> recipe = new ArrayList<String>();
Cursor cursor = db.rawQuery(SELECT, null);
if(cursor.moveToFirst())
{
do
{
recipe.clear();
recipe.add(cursor.getString(1));
recipe.add(cursor.getString(2));
recipe.add(cursor.getString(3));
recipe.add(cursor.getString(4));
results.add(recipe);
}while(cursor.moveToNext());
if(cursor != null && !cursor.isClosed())
cursor.close();
}
return results;
}
然后,我在程序的另一部分中遍历 ArrayList,其中包含的所有信息只是输入到表中的最后一行的副本。我一收到它就在我的其他方法中检查了 ArrayLists,它们都是一样的,所以我假设它一定是这个代码段中的一个问题。我还尝试了带有 group by 和 order by 子句的 select 语句,但它仍然不起作用。使用带有正确参数的 db.query() 也会导致同样的问题。
【问题讨论】: