Yoo Yoo - 1 month ago 23
Android Question

Get all rows from SQLite

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

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:

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.

Answer

try

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

AND for List<String>

if (cursor .moveToFirst()) {

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

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