Android 从 SQLite 获取所有行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10111166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:31:29  来源:igfitidea点击:

Get all rows from SQLite

androidsqlite

提问by Yoo

I have been trying to get all rows from the SQLite database. But I got only last row from the following codes.

我一直在尝试从 SQLite 数据库中获取所有行。但我只从以下代码中得到最后一行。

FileChooser class:

文件选择器类:

public ArrayList<String> readFileFromSQLite() {

  fileName = new ArrayList<String>();

  fileSQLiteAdapter = new FileSQLiteAdapter(FileChooser.this);
  fileSQLiteAdapter.openToRead();
  cursor = fileSQLiteAdapter.queueAll();

  if (cursor != null) {
    if (cursor.moveToFirst()) {
      do {
        fileName.add(cursor.getString(cursor.getColumnIndex(FileSQLiteAdapter.KEY_CONTENT1)));
      } while (cursor.moveToNext());

    }
    cursor.close();
  }

  fileSQLiteAdapter.close();
  return fileName;
}

FileSQLiteAdapter class:

FileSQLiteAdapter 类:

public Cursor queueAll() {
  String[] columns = new String[] { KEY_ID, KEY_CONTENT1 };

  Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null,
                null, null, null, null);
  return cursor;
}

Please tell me where is my incorrect. Appreciate.

请告诉我哪里不正确。欣赏。

回答by Samir Mangroliya

try:

尝试:

Cursor  cursor = db.rawQuery("select * from table",null);

AND for List<String>:

并且对于List<String>

if (cursor.moveToFirst()) {
  while (!cursor.isAfterLast()) {
    String name = cursor.getString(cursor.getColumnIndex(countyname));

    list.add(name);
    cursor.moveToNext();
  }
}

回答by Suragch

Using Android's built in method

使用 Android 的内置方法

If you want every column and every row, then just pass in nullfor the SQLiteDatabasecolumnand selectionparameters.

如果你想每一列和每一行,那么就传递null了和参数。SQLiteDatabasecolumnselection

Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null, null);

More details

更多细节

The other answers use rawQuery, but you can use Android's built in SQLiteDatabase. The documentationfor querysays that you can just pass in nullto the selectionparameter to get all the rows.

其他答案使用rawQuery,但您可以使用 Android 的内置SQLiteDatabase. 该文档query说,你可以通过在nullselection参数来获取所有行。

selectionPassing null will return all rows for the given table.

selection传递 null 将返回给定表的所有行。

And while you can also pass in nullfor the columnparameter to get all of the columns (as in the one-liner above), it is better to only return the columns that you need. The documentationsays

虽然你也可以在传递nullcolumn参数来获取所有的列(如在一个班轮以上),最好是只返回列,你所需要的。该文件说:

columnsPassing null will return all columns, which is discouraged to prevent reading data from storage that isn't going to be used.

columns传递 null 将返回所有列,不鼓励这样做以防止从不会使用的存储中读取数据。

Example

例子

SQLiteDatabase db = mHelper.getReadableDatabase();
String[] columns = {
        MyDatabaseHelper.COLUMN_1,
        MyDatabaseHelper.COLUMN_2,
        MyDatabaseHelper.COLUMN_3};
String selection = null; // this will select all rows
Cursor cursor = db.query(MyDatabaseHelper.MY_TABLE, columns, selection,
        null, null, null, null, null);

回答by ArtiomLK

This is almost the same solution as the others, but I thought it might be good to look at different ways of achieving the same result and explain a little bit:

这与其他解决方案几乎相同,但我认为查看实现相同结果的不同方法并稍微解释一下可能会很好:

Probably you have the table name String variable initialized at the time you called the DBHandler so it would be something like;

可能您在调用 DBHandler 时初始化了表名 String 变量,因此它类似于;

private static final String MYDATABASE_TABLE = "anyTableName";

Then, wherever you are trying to retrieve all table rows;

然后,无论您在何处尝试检索所有表行;

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from " + MYDATABASE_TABLE, null);

List<String> fileName = new ArrayList<>();
if (cursor.moveToFirst()){
   fileName.add(cursor.getString(cursor.getColumnIndex(COLUMN_NAME)));
   while(cursor.moveToNext()){
      fileName.add(cursor.getString(cursor.getColumnIndex(COLUMN_NAME)));
   }
}
cursor.close();
db.close();

Honestly, there are many ways about doing this,

老实说,有很多方法可以做到这一点,

回答by Sam Khalil

I have been looking into the same problem! I think your problem is related to where you identify the variable that you use to populate the ArrayList that you return. If you define it inside the loop, then it will always reference the last row in the table in the database. In order to avoid this, you have to identify it outside the loop:

我一直在研究同样的问题!我认为您的问题与您识别用于填充返回的 ArrayList 的变量的位置有关。如果您在循环内定义它,那么它将始终引用数据库表中的最后一行。为了避免这种情况,您必须在循环之外识别它:

String name;
if (cursor.moveToFirst()) {

        while (cursor.isAfterLast() == false) {
            name = cursor.getString(cursor
                    .getColumnIndex(countyname));

            list.add(name);
            cursor.moveToNext();
        }
}

回答by Ferdous Ahamed

Update queueAll()method as below:

更新queueAll()方法如下:

public Cursor queueAll() {

     String selectQuery = "SELECT * FROM " + MYDATABASE_TABLE;
     Cursor cursor = sqLiteDatabase.rawQuery(selectQuery, null);

     return cursor;
}

Update readFileFromSQLite()method as below:

更新readFileFromSQLite()方法如下:

public ArrayList<String> readFileFromSQLite() {

    fileName = new ArrayList<String>();

    fileSQLiteAdapter = new FileSQLiteAdapter(FileChooser.this);
    fileSQLiteAdapter.openToRead();

    cursor = fileSQLiteAdapter.queueAll();

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do 
            {
                String name = cursor.getString(cursor.getColumnIndex(FileSQLiteAdapter.KEY_CONTENT1));
                fileName.add(name);
            } while (cursor.moveToNext());
        }
        cursor.close();
    }

    fileSQLiteAdapter.close();

    return fileName;
}

回答by roshan posakya

Cursor cursor = myDb.viewData();

        if (cursor.moveToFirst()){
              do {
                 String itemname=cursor.getString(cursor.getColumnIndex(myDb.col_2));
                 String price=cursor.getString(cursor.getColumnIndex(myDb.col_3));
                 String quantity=cursor.getString(cursor.getColumnIndex(myDb.col_4));
                 String table_no=cursor.getString(cursor.getColumnIndex(myDb.col_5));

                 }while (cursor.moveToNext());

                }

                cursor.requery();

回答by abc

public List<String> getAllData(String email)
{

    db = this.getReadableDatabase();
    String[] projection={email};

    List<String> list=new ArrayList<>();

    Cursor cursor = db.query(TABLE_USER, //Table to query
            null,    //columns to return
            "user_email=?",        //columns for the WHERE clause
            projection,        //The values for the WHERE clause
            null,       //group the rows
            null,       //filter by row groups
            null);
    //  cursor.moveToFirst();

    if (cursor.moveToFirst()) {
        do {

            list.add(cursor.getString(cursor.getColumnIndex("user_id")));
            list.add(cursor.getString(cursor.getColumnIndex("user_name")));
            list.add(cursor.getString(cursor.getColumnIndex("user_email")));
            list.add(cursor.getString(cursor.getColumnIndex("user_password")));
            // cursor.moveToNext();

        } while (cursor.moveToNext());
    }
    return list;
}