【问题标题】:android.database.sqlite.SQLiteDatabase - can't retrieve all entries from the databaseandroid.database.sqlite.SQLiteDatabase - 无法从数据库中检索所有条目
【发布时间】:2013-12-11 22:24:04
【问题描述】:

上下文:我可以使用 public Cursor getEntry(long rowId)public Cursor getEntry(String category) 等其他方法检索条目,但由于某种原因 public Cursor getAllEntries() 不起作用。我收到错误Cursor - Invalid statement in fillWindow()

代码:

//---retrieves all entries---
    public Cursor getAllEntries() throws SQLException
    {
        Log.d(BudgetConstants.DEBUG_TAG, "DBAdapter - DatabaseHelper - getAllEntries();");
        return db.query(DATABASE_TABLE,
                new String[] {
                KEY_ROWID,
                KEY_TYPE,
                KEY_DATE,
                KEY_CATEGORY,
                KEY_AMOUNT},
                null, null, null, null, null);
    }

完整代码:
ListActivity(从我调用数据库的地方)-http://pastebin.com/UGSvhsfd
DBAdapter(我的数据库接口)-http://pastebin.com/jqtLiPHB
DBAdapter(我的朋友 DBAdapter 应该可以正常工作) - http://pastebin.com/QTRF958d

EDIT1: 调用数据库的代码:

private void updateGUI(int displayScope) {
        Cursor c;
        ArrayList<Entry> entries;           // NOTE: making an Entry ArrayList due to the fact that rows in Cursors cannot be deleted
        switch(displayScope) {
        case BudgetConstants.DISPLAY_ALL:
            c = getAllTransactions();
            //boolean b = c.moveToFirst();
            //Log.d(BudgetConstants.DEBUG_TAG, "movetofirst: "+b);
            entries = extractEntries(c, BudgetConstants.FILTER_LIMIT_ALL);
            displayEntries(entries);
            break;
        case BudgetConstants.DISPLAY_MONTH:
            c = getAllTransactions();
            entries = extractEntries(c, BudgetConstants.FILTER_LIMIT_MONTH);
            displayEntries(entries);
            break;
        }
    }

private ArrayList<Entry> extractEntries(Cursor c, int filter) {
        ArrayList<Entry> res = new ArrayList<Entry>();
        Calendar cal = Calendar.getInstance();
        int currMonth = cal.get(Calendar.MONTH)+1;
        int currYear = cal.get(Calendar.YEAR);

        if (c.moveToFirst()) {  // <------ getting an error here: Cursor - Invalid statement in fillWindow()
            do {
                if (filter==BudgetConstants.FILTER_LIMIT_MONTH) {
                    String entryDate = c.getString(BudgetConstants.DB_DATE);
                    int entryMonth = getMonth(entryDate);
                    int entryYear = getYear(entryDate);
                    if (!((entryMonth==currMonth) && (entryYear==currYear)))
                        continue;
                } else if (filter==BudgetConstants.FILTER_LIMIT_ALL)
                    res.add(new Entry(c.getString(BudgetConstants.DB_DATE), c.getString(BudgetConstants.DB_TYPE), c.getString(BudgetConstants.DB_AMOUNT)));
            } while (c.moveToNext());
        }
        return res;
    }

【问题讨论】:

    标签: android android-sqlite android-cursor sqlite


    【解决方案1】:

    db.close(); 移动到private void displayEntries(ArrayList&lt;Entry&gt; entries) 的最后一行,似乎成功了。

    【讨论】:

      猜你喜欢
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-20
      • 1970-01-01
      相关资源
      最近更新 更多