早些时候我问过这个question (经过一些谷歌搜索后,我阅读了使用你自己的 SQLite database for Android app 。
所以,我有一个简单的数据库,其中包含 2 列(_id 和引号)。我需要做的是从数据库中获取所有引号,并在 TextView 中逐一显示它们。
我的 DataBaseHelper.java 中有这个:
private SQLiteDatabase myDataBase;
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Cursor mCursor = myDataBase.query(true, DB_NAME, null, new String[] + "=" + "_id", null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
我该怎么办?谢谢
最佳答案
这个函数是我从数据库中获取数据的方式,其中“db”是数据库实例。 此函数将返回一个二维字符串数组,其中 [0][x] 是它获取的第一行,[1][x] 是第二行,依此类推; x 是列,在您的情况下 [0][1] 将是数据库中的第一个引号。
public String[][] customQuery(String query) {
Cursor cur = null;
try {
cur = db.rawQuery(query,null);
} catch(SQLException e) {
Log.d("DATABASE", e.toString());
}
String data[][] = new String[cur.getCount()][cur.getColumnCount()];
if (cur != null) {
int i = 0;
while (cur.moveToNext()) {
int j = 0;
while (j < cur.getColumnCount()) {
data[i][j] = cur.getString(j);
j++;
}
i++;
cur.moveToNext();
}
cur.close();
}
return data;
}
关于java - 如何从 sqlite 中检索列并将其存储到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8695134/