游标从查询到表返回零行
我在我的应用程序中创建了一个 SQLiteDatabase 并用一些数据填充它。我可以使用终端连接到我的 AVD,当我发出 select * fromarticles 时;我得到了表中所有行的列表,一切看起来都很好。但是,在我的代码中,当我查询表时,我得到一个游标,其中包含我的表列,但数据行为零。这是我的代码。
mDbHelper.open();
Cursor articles = mDbHelper.fetchAllArticles();
startManagingCursor(articles);
Cursor feeds = mDbHelper.fetchAllFeeds();
startManagingCursor(feeds);
mDbHelper.close();
int titleColumn = articles.getColumnIndex("title");
int feedIdColumn = articles.getColumnIndex("feed_id");
int feedTitleColumn = feeds.getColumnIndex("title");
/* Check if our result was valid. */
if (articles != null) {
int count = articles.getCount();
/* Check if at least one Result was returned. */
if (articles.moveToFirst()) {
在上面的代码中,我的游标文章返回了 4 列,但是当我调用 getCount() 时,它返回零,即使我可以从命令行看到该表中的数百行数据。知道我在这里可能做错了什么吗?
另外..这是我的 fetchAllArticles 代码..
public Cursor fetchAllArticles() {
return mDb.query(ARTICLES_TABLE, new String[] {ARTICLE_KEY_ROWID, ARTICLE_KEY_FEED_ID, ARTICLE_KEY_TITLE,
ARTICLE_KEY_URL}, null, null, null, null, null);
}
I've created an SQLiteDatabase in my app and populated it with some data. I can connect to my AVD with a terminal and when I issue select * from articles; I get a list of all the rows in my table and everything looks fine. However, in my code when I query my table, I get a cursor back that has my tables columns, but zero rows of data. Here is my code..
mDbHelper.open();
Cursor articles = mDbHelper.fetchAllArticles();
startManagingCursor(articles);
Cursor feeds = mDbHelper.fetchAllFeeds();
startManagingCursor(feeds);
mDbHelper.close();
int titleColumn = articles.getColumnIndex("title");
int feedIdColumn = articles.getColumnIndex("feed_id");
int feedTitleColumn = feeds.getColumnIndex("title");
/* Check if our result was valid. */
if (articles != null) {
int count = articles.getCount();
/* Check if at least one Result was returned. */
if (articles.moveToFirst()) {
In the above code, my Cursor articles returns with my 4 columns, but when I call getCount() it returns zero, even though I can see hundreds of rows of data in that table from command line. Any idea what I might be doing wrong here?
Also.. here is my code for fetchAllArticles..
public Cursor fetchAllArticles() {
return mDb.query(ARTICLES_TABLE, new String[] {ARTICLE_KEY_ROWID, ARTICLE_KEY_FEED_ID, ARTICLE_KEY_TITLE,
ARTICLE_KEY_URL}, null, null, null, null, null);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试移动 mDbHelper.close();到最后
另外 - 你可以发布 fetch 方法 src 吗?
try moving mDbHelper.close(); to the end
Also - could you post fetch method src ?