我有一个“小”问题,我有 SQLite 数据库,我需要从表“Champions”列“displayName”中获取所有值(文本)到字符串,并通过 id 调用它们以在 TextView 中显示?我已经 2 小时找不到了...有什么解决办法吗? (我需要从一列中获取字符串,按 id 对它们进行排序。加载 Activity 时会在 textview 中显示该文本值,由 id 调用。)有什么好的解决方案吗?非常感谢您的每一个回复。
抱歉英语不好,问题很愚蠢,但我是编程新手(只是java,我对使用 Visual Basic 有点擅长)。
编辑:或者只是将列导出到 xml 字符串。我可以这样做吗?谢谢
最佳答案
使用DataBaseHelper:
DataBaseHelper myDbHelper = new DataBaseHelper(this.context, "animals.db");
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
Toast.makeText(context,
context.getString(R.string.error), Toast.LENGTH_SHORT).show();
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
} catch (SQLException sqle) {
Toast.makeText(context,
context.getString(R.string.error), Toast.LENGTH_SHORT).show();
throw sqle;
}
和光标:
SQLiteDatabase db = myDbHelper.getWritableDatabase();
Cursor c = db.query("animals", null, null, null, null, null, null);
ArrayList<Animals> animals = new ArrayList<Animals>();
int idColIndex = c.getColumnIndex("_id");
int nameColIndex = c.getColumnIndex("name");
ArrayList<String> nameList = new ArrayList<String>();
if (c.moveToFirst()) {
do {
String name = c.getString(nameColIndex);
nameList.add(name);
} while (c.moveToNext());
}
c.close();
db.close();
关于java - Android - SQLite 数据库到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25791543/