lundi 5 octobre 2015

print correctly a query in sqllite android

I have the following table scheme

public final class FeedReaderContract {
// To prevent someone from accidentally instantiating the contract class,
// give it an empty constructor.
public FeedReaderContract() {
}

/* Inner class that defines the table contents */
public static abstract class FeedEntry implements BaseColumns {
    public static final String TABLE_NAME = "entry";
    public static final String COLUMN_NAME_ENTRY_ID = "entryid";
    public static final String COLUMN_NAME_TITLE = "title";
}

}

and I initialize the table with the following function

public void put_info(){
    SQLiteDatabase db = mDbHelper.getWritableDatabase();

    ContentValues values = new ContentValues();
    int id = 999;
    String title = "Sophia";
    values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID, id);
    values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, title);

    long newRowId;
    newRowId = db.insert(
            FeedReaderContract.FeedEntry.TABLE_NAME,
            null,
            values);

}

I try to make a request like this:

Cursor cursor = db.query(
            FeedReaderContract.FeedEntry.TABLE_NAME,  // The table to query
            projection,                               // The columns to return
            null,                                // The columns for the WHERE clause
            null,                            // The values for the WHERE clause
            null,                                     // don't group the rows
            null,                                    // don't filter by row groups
            sortOrder                                 // The sort order
    );

where

        String[] projection = {
            FeedReaderContract.FeedEntry._ID,
            FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE,
    };

then I try to print the query in the following way:

for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
        result = result + cursor.getString(iRow) + " " + cursor.getString(iName) + "\n";
    }
    System.out.println(result);

My problem is that I get a strange output like

10-05 03:04:55.005 19717-19717/com.mycompany.antibes I/System.out: 1 title
10-05 03:04:55.005 19717-19717/com.mycompany.antibes I/System.out: 2 Sophia
10-05 03:04:55.005 19717-19717/com.mycompany.antibes I/System.out: 3 Sophia
10-05 03:04:55.005 19717-19717/com.mycompany.antibes I/System.out: 4 Sophia
10-05 03:04:55.005 19717-19717/com.mycompany.antibes I/System.out: 5 Sophia
10-05 03:04:55.005 19717-19717/com.mycompany.antibes I/System.out: 6 Sophia
10-05 03:04:55.005 19717-19717/com.mycompany.antibes I/System.out: 7 Sophia
10-05 03:04:55.005 19717-19717/com.mycompany.antibes I/System.out: 8 Sophia
10-05 03:04:55.005 19717-19717/com.mycompany.antibes I/System.out: 9 Sophia
10-05 03:04:55.005 19717-19717/com.mycompany.antibes I/System.out: 10 Sophia
10-05 03:04:55.005 19717-19717/com.mycompany.antibes I/System.out: 11 Sophia
10-05 03:04:55.005 19717-19717/com.mycompany.antibes I/System.out: 12 Sophia
10-05 03:04:55.005 19717-19717/com.mycompany.antibes I/System.out: 13 Sophia
10-05 03:04:55.005 19717-19717/com.mycompany.antibes I/System.out: 14 Sophia
10-05 03:04:55.005 19717-19717/com.mycompany.antibes I/System.out: 15 Sophia
10-05 03:04:55.005 19717-19717/com.mycompany.antibes I/System.out: 16 Sophia
10-05 03:04:55.005 19717-19717/com.mycompany.antibes I/System.out: 17 Sophia
10-05 03:04:55.005 19717-19717/com.mycompany.antibes I/System.out: 18 Sophia

while I would like my output to be something like:

title 
999
sophia

That's also what I would expect from my query, since my database is done of only one row and the request should give me back all the rows (because the argument where I select the rows is null).

I don't understand as well the series of numbers from 1 to 18 in the output, I don't see any way in the code where this can come from. Any ideas?

Aucun commentaire:

Enregistrer un commentaire