Reputation: 2790
i am trying to put strings from query to string[], but i gor an error:
FATAL EXCEPTION: main
java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
here is my code:
private void getCitiesArray() {
DonorDbHelper donorDbHelper = new DonorDbHelper(getActivity());
String[] cityColumnName = new String[]{DonorContract.CitiesEntry.COLUMN_NAME};
Cursor c = donorDbHelper.getReadableDatabase().query(DonorContract.CitiesEntry.TABLE_NAME,
cityColumnName,
null, null, null, null, null
);
c.moveToFirst();
String[] test = null;
int count = c.getCount();
for (int i = 1; i < count ; i++){
test[i-1] = c.getString(i);
Log.d("111", "city #" + i + " - " + c.getString(i));
}
c.close();
}
dateBase:
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
final String SQL_CREATE_CITIES_TABLE = "CREATE TABLE " + CitiesEntry.TABLE_NAME + " (" +
CitiesEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
CitiesEntry.COLUMN_CITY_ID + " INTEGER NOT NULL," +
CitiesEntry.COLUMN_NAME + " TEXT NOT NULL," +
CitiesEntry.COLUMN_REGION + " INTEGER NOT NULL, " +
"UNIQUE ( " + CitiesEntry.COLUMN_CITY_ID + " ) ON CONFLICT REPLACE);";
sqLiteDatabase.execSQL(SQL_CREATE_CITIES_TABLE);
}
why it happends and how to fix this?
Upvotes: 0
Views: 90
Reputation: 33
Here is an example. Try this:
Cursor cu=db.query(Helper.TABLE_NAME, column, null, null, null, null,null);
Upvotes: 0
Reputation: 44118
You're not looping the cursor rows but looping the columns. Furthermore, you forgot to initialize the String array.
c.moveToFirst();
int count = c.getCount();
String[] strings = new String[count];
for (int i = 0; i < count ; ++i){
strings[i] = c.getString(0);
Log.d("111", "city #" + i + " - " + c.getString(i));
c.moveToNext();
}
c.close();
Here I'm looping returned rows and save the first column value as a string.
Upvotes: 1