Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从android sqlite数据库中为应用程序选择所有行时,它将返回所有条目的最后一个条目_Android_Sqlite_Select - Fatal编程技术网

从android sqlite数据库中为应用程序选择所有行时,它将返回所有条目的最后一个条目

从android sqlite数据库中为应用程序选择所有行时,它将返回所有条目的最后一个条目,android,sqlite,select,Android,Sqlite,Select,我目前正在开发一个android应用程序。我有一个sqlite数据库,它将文本(在我的应用程序中仅用作字符串)存储在四列中。我试图返回表中的所有行和列。我已经创建了数据并将其插入表中,并使用adb shell中的sqlite3验证了数据是否存在。我使用的语句与我在程序中使用的语句相同,它返回包含所有正确数据的所有行。在我的程序中,我通过遍历光标以ArrayList格式存储所有数据。它返回与行相对应的正确数量的ArrayList,但它们都只有最后一行的信息。这是我的密码: private stat

我目前正在开发一个android应用程序。我有一个sqlite数据库,它将文本(在我的应用程序中仅用作字符串)存储在四列中。我试图返回表中的所有行和列。我已经创建了数据并将其插入表中,并使用adb shell中的sqlite3验证了数据是否存在。我使用的语句与我在程序中使用的语句相同,它返回包含所有正确数据的所有行。在我的程序中,我通过遍历光标以
ArrayList
格式存储所有数据。它返回与行相对应的正确数量的
ArrayList
,但它们都只有最后一行的信息。这是我的密码:

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;
}
private static final String SELECT=“SELECT*FROM”+表名;
公共ArrayList allRecipes()
{
ArrayList结果=新建ArrayList();
ArrayList配方=新建ArrayList();
Cursor Cursor=db.rawQuery(SELECT,null);
if(cursor.moveToFirst())
{
做
{
配方。清除();
recipe.add(cursor.getString(1));
recipe.add(cursor.getString(2));
recipe.add(cursor.getString(3));
recipe.add(cursor.getString(4));
结果:添加(配方);
}while(cursor.moveToNext());
if(cursor!=null&!cursor.isClosed())
cursor.close();
}
返回结果;
}

然后,我在程序的另一部分中遍历ArrayList,其中包含的所有信息都只是输入到表中的最后一行的副本。我已经检查了我的另一个方法中的ArrayList,只要它收到它,它们都是相同的,所以我假设这一定是这个代码段中的一个问题。我还尝试了带有GROUPBY和ORDERBY子句的select语句,但仍然不起作用。使用具有正确参数的db.query()也会导致相同的问题。

之所以会出现这种情况,是因为您在数组列表中添加了指向配方的链接,并在循环中的每次迭代中更改了配方的值

你应该把代码改成这个

public ArrayList<ArrayList<String>> allRecipes()
{
   ArrayList<ArrayList<String>> results = new ArrayList<ArrayList<String>>();
   Cursor cursor = db.rawQuery(SELECT, null);
   if(cursor.moveToFirst())
   {
       do
       {
           ArrayList<String> recipe = new ArrayList<String>();
           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;
}
public ArrayList allRecipes()
{
ArrayList结果=新建ArrayList();
Cursor Cursor=db.rawQuery(SELECT,null);
if(cursor.moveToFirst())
{
做
{
ArrayList配方=新建ArrayList();
recipe.add(cursor.getString(1));
recipe.add(cursor.getString(2));
recipe.add(cursor.getString(3));
recipe.add(cursor.getString(4));
结果:添加(配方);
}while(cursor.moveToNext());
if(cursor!=null&!cursor.isClosed())
cursor.close();
}
返回结果;
}

最好是定义类配方,而不是使用数组列表