【问题标题】:how to store string data into integer array from sqlite for android如何将字符串数据从sqlite for android存储到整数数组中
【发布时间】:2015-05-15 21:11:18
【问题描述】:

我想将 sqlite 数据转换成整数数组。

public Integer[] getch() {

        SQLiteDatabase database = this.getReadableDatabase();
        Cursor cursor = database.rawQuery("SELECT sum(sales) FROM sales group by outlet_code order by ordered_date", null);
        // String[] array = new String[crs.getCount()];
        int columnIndex = 3;
        Integer[] in = new Integer[cursor.getCount()];

        if (cursor.moveToFirst())
        {
            for (int i = 0; i < cursor.getCount(); i++)
            {
                in[i] = cursor.getInt(columnIndex);
                cursor.moveToNext();
            }
        }
        cursor.close();
        return in;
    }

我需要以下格式的结果:

int[] income = { 2000,2500,2700,3000,2800,3500,3700,3800, 0,0,0,0};

Integer[] income =  controller.getch();

我收到了错误:

无法从 CursorWindow 读取第 0 行第 3 列。确保光标是 在从中访问数据之前正确初始化

【问题讨论】:

标签: java android arrays


【解决方案1】:

你为什么使用columnIndex = 3,你的sql查询将只返回1列即Sum(sales),所以你应该将你的columnIndex值设置为0

试试这个

public Integer[] getch() {

    SQLiteDatabase database = this.getReadableDatabase();
    Cursor cursor = database.rawQuery("SELECT sum(sales) FROM sales group by outlet_code order by ordered_date", null);
    // String[] array = new String[crs.getCount()];
    int columnIndex = 0;
    Integer[] in = new Integer[cursor.getCount()];

    if (cursor.moveToFirst())
    {
        for (int i = 0; i < cursor.getCount(); i++)
        {
            in[i] = cursor.getInt(columnIndex);
            cursor.moveToNext();
        }
    }
    cursor.close();
    return in;
}

希望对你有帮助

【讨论】:

  • 然后请通过点击答案旁边的“右标记”图标将其标记为正确/接受答案。谢谢
猜你喜欢
  • 1970-01-01
  • 2015-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-03
  • 2012-10-09
  • 2019-11-23
相关资源
最近更新 更多