Here is the Mp_DB class and the method getMPName() and delete() method are posted. getMP_Name() method return string contains the name of the MP.
public class MP_DB extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "MP.db";
private static final String MP_TABLE_NAME = "MPData";
MP_DB (Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}// end of
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(" CREATE TABLE " + MP_TABLE_NAME + " ( " +
BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
" name TEXT, " +
" lat REAL, " +
" lng REAL, " +
" date TEXT, " +
" time TEXT " +
");" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public String getMP_Name(long id) {
SQLiteDatabase db = this.getReadableDatabase();
SQLiteCursor c = (SQLiteCursor) db.rawQuery("SELECT name FROM MPData WHERE "+
BaseColumns._ID+" = "+
Long.toString(id), null);
c.moveToFirst();
String r = c.getString(0);
return r;
}
I get the following error:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidbook.MP/com.androidbook.MP.MyLocations}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3691)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
at com.androidbook.MP.MP_DB.getMP_Name(MP_DB.java:44)
at com.androidbook.MP.MyLocations.get_MPNames(MyLocations.java:179)
at com.androidbook.MP.MyLocations.onCreate(MyLocations.java:59)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more
Your query doe snot return any results here:
Thus the last line throws the error. Please make sure the query should return the expected value.
Also I never query my database with raw query. Try doing it that way:
Afterwords make the following check to see if the query returned any results:
Also never forget to close your databases and cursors (or you will get errors). So the final version of the method:
PS: Mind my edits of your question. Formatting it that way makes it quite easier for users to read.