mardi 30 juin 2015

How to get List of SQLite columns records in android?

I create a sqlite database name Bands and a table name PinkFloyd.

pId   pName    pFamily

1     David    Gilmour
2     Roger    Waters
3     Richard  Wright

now I want to get all columns of PinkFloyd records.

I create a method in my BandsDataSource.java to get all columns records:

private static final String[] pinkfloydColumns = {
            IronClubDBOpenHelper.COLUMN_P_ID,
            IronClubDBOpenHelper.COLUMN_P_NAME,
            IronClubDBOpenHelper.COLUMN_P_FAMILY
    };

public List<BandsTablesModel> findAll() {

        List<BandsTablesModel> bandsTablesModelList = new ArrayList<BandsTablesModel>();
        Cursor cursor = database.query(BandsDBOpenHelper.TABLE_PINKFLOYD, pinkfloydColumns, null, null, null, null, null);
        Log.i(LOGTAG, "Returned " + cursor.getCount() + " rows.");
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {

                BandsTablesModel bandsTablesModel = new BandsTablesModel();
                BandsTablesModel.setPId(cursor.getInt(cursor.getColumnIndex(BandsDBOpenHelper.COLUMN_P_ID)));
                BandsTablesModel.setPName(cursor.getString(cursor.getColumnIndex(BandsDBOpenHelper.COLUMN_P_NAME)));
                BandsTablesModel.setPFamily(cursor.getString(cursor.getColumnIndex(BandsDBOpenHelper.COLUMN_P_FAMILY)));
                bandsTablesModelList.add(BandsTablesModel); 

                Log.i(LOGTAG, "Returned " + cursor.getInt(cursor.getColumnIndex(BandsDBOpenHelper.COLUMN_P_ID)) + " rows");  
                Log.i(LOGTAG, "Returned " + cursor.getString(cursor.getColumnIndex(BandsDBOpenHelper.COLUMN_P_NAME)) + " rows");  
                Log.i(LOGTAG, "Returned " + cursor.getString(cursor.getColumnIndex(BandsDBOpenHelper.COLUMN_P_FAMILY)) + " rows"); 
            }
        }
        return BandsTablesModelList;
    }

when I call the findall() method, it gives me nothing in Logcat!

Returned 3 rows.
Returned 0 rows
Returned bandName rows
Returned bandFamily rows
Returned 0 rows
Returned bandName rows
Returned bandFamily rows
Returned 0 rows
Returned bandName rows
Returned bandFamily rows

my Bands database and pinkfloyd Table has records, so how it's possible?

Logcat should be like this:

Returned 3 rows.
Returned 1 rows
Returned David rows
Returned Gilmour rows
Returned 2 rows
Returned Roger rows
Returned Waters rows
Returned 3 rows
Returned Richard rows
Returned Wright rows

Thank you for helping.

Aucun commentaire:

Enregistrer un commentaire