Android: Error: Couldn't read row 1, col 1 from CursorWindow
Android: Error: Couldn't read row 1, col 1 from CursorWindow
我的应用抛出异常:
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;
}
}
Table结构:
db.execSQL("CREATE TABLE IF NOT EXISTS answers (id INTEGER PRIMARY KEY AUTOINCREMENT, " + "id_question VARCHAR, answer VARCHAR);");
当我输入游标只搜索列时
- "answer" 结果是 [ttttt, null, null] - 其中 ttttt 是第一个答案
- "id_question" 结果为 [1, null, null]
- null 结果是 [1, 1, wwwww] - 其中 1 是 id_question 而 wwww 是最后一个答案
我做错了什么?
如有任何帮助,我将不胜感激。谢谢!
Cusror.getCount
return 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
}
....
}
这是您的解决方案:
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);
....
}
我的应用抛出异常:
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;
}
}
Table结构:
db.execSQL("CREATE TABLE IF NOT EXISTS answers (id INTEGER PRIMARY KEY AUTOINCREMENT, " + "id_question VARCHAR, answer VARCHAR);");
当我输入游标只搜索列时
- "answer" 结果是 [ttttt, null, null] - 其中 ttttt 是第一个答案
- "id_question" 结果为 [1, null, null]
- null 结果是 [1, 1, wwwww] - 其中 1 是 id_question 而 wwww 是最后一个答案
我做错了什么?
如有任何帮助,我将不胜感激。谢谢!
Cusror.getCount
return 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
}
....
}
这是您的解决方案:
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);
....
}