我的应用程序正在向我抛出异常:
Error: Couldn't read row 1, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
这是我的功能:
static public String[] getData(Context ctx) {
String[] result = null;
try {
SQLiteDatabase db = ctx.openOrCreateDatabase("mydatabase", 0,
null);
Cursor c = db.query("answers", new String[]{"answer"}, "id_question='1'", null, null, null, null);
if (c.getCount() == 0) {
return null;
}
result = new String[c.getCount()];
c.moveToFirst();
for (int i = 0; i < c.getCount(); i++) {
result[i]=c.getString(i);
Log.v(TAG, "Answer "+c.getString(i));//responds just on 1st time
c.moveToNext();
}
c.close();
db.close();
return result;
} catch (Exception e) {
Log.e(TAG,
"Database.getData(Context) - Error: "
+ e.getMessage());
return result;
}
}
表格结构:
db.execSQL("CREATE TABLE IF NOT EXISTS answers (id INTEGER PRIMARY KEY AUTOINCREMENT, " + "id_question VARCHAR, answer VARCHAR);");
当我放入光标搜索专栏
时我做错了什么?
我将不胜感激任何帮助。谢谢!
答案 0 :(得分:2)
Cusror.getCount
返回Cursor中的行数。使用Cursor.getColumnCount()
从行中获取列:
result = new String[c.getColumnCount()];
for (int i = 0; i < c.getCount(); i++) {
for(int j=0;j<c.getColumnCount();j++){
result[j]=c.getString(j); << here pass column index
}
....
}
答案 1 :(得分:1)
这是你的解决方案:
result = new String[c.getColumnCount()];
for (int i = 0; i < c.getCount(); i++) {
for(int ii=0;ii<c.getColumnCount();ii++){
result[ii]=c.getString(1);
....
}