Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

android - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try:

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

AND for List<String>:

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...