gpt4 book ai didi

java - 如何将字符串数据从sqlite for android存储到整数数组中

转载 作者:行者123 更新时间:2023-12-01 11:33:11 25 4
gpt4 key购买 nike

我想将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();

我收到错误:

Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it

最佳答案

为什么你使用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;
}

希望这有帮助

关于java - 如何将字符串数据从sqlite for android存储到整数数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30259396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com