У меня есть следующая схема таблицы
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";
}
}
и я инициализирую таблицу следующей функцией
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);
}
Я пытаюсь сделать такой запрос:
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
);
куда
String[] projection = {
FeedReaderContract.FeedEntry._ID,
FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE,
};
затем я пытаюсь напечатать запрос следующим образом:
String result = "";
int iRow = cursor.getColumnIndex(FeedReaderContract.FeedEntry._ID);
int iName = cursor.getColumnIndex(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
result = result + cursor.getString(iRow) + " " + cursor.getString(iName) + "\n";
}
System.out.println(result);
Моя проблема в том, что я получаю странный вывод, например
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
в то время как я хотел бы, чтобы мой вывод был примерно таким:
title
999
sophia
Это также то, что я ожидал от моего запроса, поскольку моя база данных состоит только из одной строки, и запрос должен вернуть мне все строки (поскольку аргумент, в котором я выбираю строки, равен нулю).
Я также не понимаю ряд чисел от 1 до 18 в выводе, я не вижу в коде, откуда это может взяться. Любые идеи?
DatabaseUtils#dumpCursor
- person pskink   schedule 05.10.2015iRow
? - person CL.   schedule 05.10.2015DatabaseUtils.dumpCursor(cursor)
и все - person pskink   schedule 05.10.2015