I make application and compiler show error. I don’t know what happened. I want to retriev and display random record from database after click.
I used tutorial from Android SQLite Database Tutorial and from stackoverflov
Error:
10-30 19:19:59.533: E/AndroidRuntime(359): FATAL EXCEPTION: main
10-30 19:19:59.533: E/AndroidRuntime(359): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
10-30 19:19:59.533: E/AndroidRuntime(359): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
My code is:
public class PrzyslowiaArabskie {
private static final String TABLE_NAME = "tabela_przyslowia";
private static final String DATABASE_NAME = "przyslowiadb";
private static final int DATABASE_VERSION = 1;
public static final String KEY_ID = "id";
public static final String KEY_PRZYSLOWIE = "przyslowie";
private DbHelper myHelper;
private final Context myContext;
private SQLiteDatabase myDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_PRZYSLOWIE + " TEXT NOT NULL);" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
public PrzyslowiaArabskie(Context c) {
myContext = c;
}
public PrzyslowiaArabskie open() {
myHelper = new DbHelper(myContext);
myDatabase = myHelper.getWritableDatabase();
return this;
}
public void close() {
myHelper.close();
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ID, KEY_PRZYSLOWIE };
Cursor c = myDatabase.query(TABLE_NAME, columns, KEY_ID + "=?", new String[] {String.valueOf(KEY_ID + 1)}, null, null, null);
if (c != null) {
c.moveToFirst();
String data = c.getString(1);
return data;
}
return null;
}
}
What am I doing wrong?
I solved with my problem. Instead getting random record from database, I make 3 buttons (next record, previous record and show record). I use this instructions Getting the next record into view from database
Thanks for your help.
You are attempting to index into a column on a row of a cursor that does not exist.
Instead…
Of course if you intend to iterate over multiple rows you might want to consider a while-loop and using
while(c.moveToNext()) ...Edit: extended answer to address comment.
To use a while loop to iterate over all records available through the cursor and utilize the data in consumer code it is likely you will have to change the method signature to return a collection of sorts instead of a single string.
The example code provided should get you the result you want but if you need more explanation on dealing with collections of objects and while loops I would suggest that you consult the Java documentation for some information or more abstract programming information in regard to dealing with data structures and control flow.