在 sqlite android 中正确打印查询

print correctly a query in sqllite android

我有以下table方案

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";
}

}

然后我用下面的函数

初始化table
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

这也是我对查询的期望,因为我的数据库已完成 只有一行,请求应该给我所有的行 (因为我 select 行的参数为空)。

我也不明白输出中从 1 到 18 的数字系列,我在代码中看不出有任何来源。 有什么想法吗?

values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID, id);
...
int iRow = cursor.getColumnIndex(FeedReaderContract.FeedEntry._ID);

这是一个不同的专栏。
(拥有两个不同的键不是一个好主意。您可能想删除 entryid 并改用 _id。)

你得到十七个索菲亚,因为你打了十七次put_info()

其实我觉得我有点理解这个问题了。事实是,每次我再次启动应用程序时,查询的结果都会多一行。 这意味着我的数据库保存在内存中,不会在每次启动应用程序后被销毁。所以现在我的问题是如何在应用程序的每个 运行 处创建一个新数据库。我应该在哪里修改我的代码?