Get all rows from SQLite

前端 未结 7 1844
执笔经年
执笔经年 2020-12-14 14:14

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

FileChooser class:

p         


        
7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-14 14:47

    Using Android's built in method

    If you want every column and every row, then just pass in null for the SQLiteDatabase column and selection parameters.

    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 documentation for query says that you can just pass in null to the selection parameter to get all the rows.

    selection Passing null will return all rows for the given table.

    And while you can also pass in null for the column parameter to get all of the columns (as in the one-liner above), it is better to only return the columns that you need. The documentation says

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

    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);
    

提交回复
热议问题