我尝试了以下代码,但是当我点击查看数据库中存储的内容时,应用程序崩溃了。
DATABASE_TABLE = "EntriesTable"
String[] columns = new String[] { KEY_ROWID, KEY_STUDENT, KEY_AGE,
KEY_NUMBER, KEY_CLASS };
Cursor c = ourDatabase.rawQuery("select " + columns + " from " + DATABASE_TABLE , null);
String result = "";
int IROW = c.getColumnIndex(KEY_ROWID);
int ISTUDENT = c.getColumnIndex(KEY_STUDENT);
int IAGE = c.getColumnIndex(KEY_AGE);
int INUMBER = c.getColumnIndex(KEY_ID_NUMBER);
int ICLASS = c.getColumnIndex(KEY_CLASS);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + c.getString(IROW) + " " + c.getString(ISTUDENT) + " " + " " + c.getString(IAGE) + " " + " " + c.getString(INUMBER) + " " + " " + c.getString(ICLASS)
+ "\n";
}
return result;
LogCat:
E/AndroidRuntime(336):
Caused by:
android.database.sqlite.SQLiteException: unrecognized token: "[Ljava.lang.String;@4053eb90 from EntriesTable": , while compiling: select [Ljava.lang.String;@4053eb90 from EntriesTable
01-04 09:59:06.396: E/AndroidRuntime(336):
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
答案 0 :(得分:2)
您正在传递一个字符串数组来代替字符串。
改变这个:
Cursor c = ourDatabase.rawQuery("select " + columns + " from " + DATABASE_TABLE , null);
用这个:
Cursor c = ourDatabase.rawQuery("select " + StringUtils.join(columns, ",") + " from " + DATABASE_TABLE , null);