查询我有以下表方案打印正确sqllite机器人
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' – pskink
哪里'iRow'从何而来? –
你能给我一个很好的参考吗?我在网上找不到很多 – Helios83