我让应用程序和编译器显示错误。我不知道发生了什么。我想在点击后从数据库中检索并显示随机记录。我使用了来自Android SQLite 数据库教程和 stackoverflov 错误的教程:
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)
我的代码是:
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;
}
}
我究竟做错了什么?
我解决了我的问题。我制作了 3 个按钮(下一个记录、上一个记录和显示记录),而不是从数据库中获取随机记录。我使用此说明从数据库中获取下一条记录 感谢您的帮助。