我在嘗試從SQLite讀取數據時遇到問題,我想將所有信息存儲到已定義的對象(節點)數組中。獲取所有行SQLite Android Studio
我的構造和結構,其目標是:
public node(int id, String name, String address, double x, double y, String type, String date){
//Initialization
//....
}
注:此對象的類顯然已經定義的屬性,setter和getter方法。數據庫與該對象具有相同的結構(Int,String,String,Double,Double,String,String)。
所以,我的方法來獲取所有的行,並將它們存儲到我的數組:
public nodo[] getNodes() {
String selectQuery = "SELECT * FROM " + NAME_NODES;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
node[] nodes = new node[cursor.getCount()];
int con = 0;
if (cursor.moveToFirst()) {
while (cursor.moveToNext()) {
node temp = new node(
cursor.getInt(1),
cursor.getString(2),
cursor.getString(3),
cursor.getDouble(4),
cursor.getDouble(5),
cursor.getString(6),
cursor.getString(7));
nodes[con++] = temp;
}
}
cursor.close();
db.close();
Log.d(TAG, "SQLite data: " + nodes.toString());
return nodes;
}
然後在其他類我把這個以前的方法,是這樣的:
node[] nodes = db.getNodes();
String temp = node[0].getName();
但經過我得到一個錯誤:
<code>E/CursorWindow: Failed to read row 1, column 7 from a CursorWindow which has 2 rows, 7 columns.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.motusk.AppMonitoreo, PID: 4410
java.lang.IllegalStateException: Couldn't read row 1, col 7 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:438)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at com.motus.AppMonitoreo.Controladores.SQLiteHandler.getNodes(SQLiteHandler.java:186)</code>
我覺得主要的問題是在使用遊標
請你在光標光標= db.rawQuery(selectQuery,NULL)獲得;光標?光標索引從0開始,並從1開始。請檢查 –
[單擊此處。我希望這會幫助你](http://stackoverflow.com/a/41615670/7399521) –
是的,是coursor索引從0開始,但現在我只得到1行 –