Iterating over rows from Sqlite-query

I have a table that I want to populate with the result of a database query. I use select all and the query returns four rows of data.

I use this code to populate text views inside table rows.

Cursor c = null; c = dh.getAlternative2(); startManagingCursor(c); // the desired columns to be bound String[] columns = new String[] {DataHelper.KEY_ALT}; // the XML defined views which the data will be bound to int[] to = new int[] { R.id.name_entry}; SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.list_example_entry, c, columns, to); this.setListAdapter(mAdapter); 

I want to be able to separate four different KEY_ALT values ​​and choose where they go. I want them to fill in four different text images instead of the one in my example above. How can I iterate over the resulting cursor?

Regards, AK

+49
android sqlite cursor
Feb 07 2018-11-11T00:
source share
7 answers

You can use the code below to go through the cursor and save them in an array of strings, and after you can set them in four text

 String array[] = new String[cursor.getCount()]; i = 0; cursor.moveToFirst(); while (!cursor.isAfterLast()) { array[i] = cursor.getString(0); i++; cursor.moveToNext(); } 
+81
Feb 07 '11 at 11:17
source share
β€” -

Cursor objects returned by database queries are placed before the first record, so iteration can be simplified to:

 while (cursor.moveToNext()) { // Extract data. } 

Link from SQLiteDatabase .

+106
Oct 05
source share
 for (boolean hasItem = cursor.moveToFirst(); hasItem; hasItem = cursor.moveToNext()) { // use cursor to work with current item } 
+29
Jul 22 2018-11-21T00:
source share

Iteration can be performed as follows:

 Cursor cur = sampleDB.rawQuery("SELECT * FROM " + Constants.TABLE_NAME, null); ArrayList temp = new ArrayList(); if (cur != null) { if (cur.moveToFirst()) { do { temp.add(cur.getString(cur.getColumnIndex("Title"))); // "Title" is the field name(column) of the Table } while (cur.moveToNext()); } } 
+19
Feb 07 '11 at 11:14
source share

I agree with chiranjib, my code is as follows:

 if(cursor != null && cursor.getCount() > 0){ cursor.moveToFirst(); do{ //do logic with cursor. }while(cursor.moveToNext()); } 
+3
Apr 24 '13 at 1:49 on
source share

Found a very easy way to iterate over the cursor

 for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){ // access the curosr DatabaseUtils.dumpCurrentRowToString(cursor); final long id = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID)); } 
+3
Jan 28 '14 at 12:47 on
source share
 public void SQLfunction() { SQLiteDatabase db = getReadableDatabase(); SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); String[] sqlSelect = {"column1","column2" ...}; String sqlTable = "TableName"; String selection = "column1= ?"; //optional String[] selectionArgs = {Value}; //optional qb.setTables(sqlTable); final Cursor c = qb.query(db, sqlSelect, selection, selectionArgs, null, null, null); if(c !=null && c.moveToFirst()){ do { //do operations // example : abcField.setText(c.getString(c.getColumnIndex("ColumnName"))) } while (c.moveToNext()); } } 

NOTE. To use SQLiteQueryBuilder () you need to add

compile 'com.readystatesoftware.sqliteasset: sqliteassethelper: +' in your evaluation file

0
Nov 24 '17 at 7:26
source share



All Articles