在sqllite android中正确打印查询

时间:2015-10-05 07:44:56

标签: android sqlite println

我有以下表格方案

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

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

我也不理解输出中1到18的一系列数字,我在代码中看不到任何可以来自的方法。 有什么想法吗?

2 个答案:

答案 0 :(得分:1)

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

这是一个不同的栏目 (拥有两个不同的密钥并不是一个好主意。您可能希望放弃entryid并改为使用_id。)

你得到了十七个Sophias,因为你打了17次put_info()

答案 1 :(得分:0)

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