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.
If you are running the sqlite3 command-line access program you can type ". tables" to get a list of all tables. Or you can type ".
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(); } }
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With