2015-10-05 42 views
0

查询我有以下表方案打印正确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的输出,我没有看到在代码中这可以来自任何方式。 任何想法?

+1

进行调试使用'DatabaseUtils#dumpCursor' – pskink

+0

哪里'iRow'从何而来? –

+0

你能给我一个很好的参考吗?我在网上找不到很多 – Helios83

回答

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

这是一个不同的列。
(这是不是有两个不同的密钥是一个好主意。你可能要下降entryid和使用_id代替。)

你得到17个Sophias因为你叫put_info()十七次。

+0

是的,这是真的,但我叫put_info()在创建我的应用程序。所以我希望每次打电话给应用程序时数据库都是新的。 – Helios83

0

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

+0

数据库的重点在于保存数据。并提出一个问题。使用“Ask Question”按钮。 –

+0

@CL。好吧,只有当我重新安装我的应用程序时,才会创建一个新的数据库,否则每次重新启动后它都会保存在内存中。谢谢 :) – Helios83