这是Mp_DB类,方法getMPName()
和delete()
方法已发布。 getMP_Name()
方法返回字符串包含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;
}
我收到以下错误:
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
答案 0 :(得分:0)
编写普通的SQL语句会不会更容易?
String string_id = Long.toString(id);
String sql = "DELETE FROM " + [table_name] + " WHERE " + [primary_key_column] + " = " + string_id;
将[table_name]和[primary_key_column]替换为您的案例中的任何内容。 不确定你正在使用什么平台但是应该有一些查询方法可以将字符串传递给。
答案 1 :(得分:0)
您的查询会在此处返回任何结果:
SQLiteCursor c = (SQLiteCursor) db.rawQuery("SELECT name FROM MPData WHERE "+
BaseColumns._ID+" = "+
Long.toString(id), null);
c.moveToFirst();
String r = c.getString(0);
因此最后一行抛出错误。请确保查询应返回预期值。
此外,我从不使用原始查询查询我的数据库。尝试这样做:
String tableName = "MPData";
String [] columns = { "name" };
String where = BaseColumns._ID + "=?";
String [] whereArgs = { Long.toString(id) };
SQLiteCursor c = (SQLiteCursor) db.query(tableName, columns, where, whereArgs, null, null, null);
后记录进行以下检查以查看查询是否返回任何结果:
if (c.getCount() > 0) {
c.moveToFirst();
r = c.getString(0);
} else {
// the query returned nothing. Do appropriate action.
}
也永远不要忘记关闭数据库和游标(否则会出错)。所以该方法的最终版本:
public String getMP_Name(long id) {
SQLiteDatabase db = this.getReadableDatabase();
String tableName = "MPData";
String [] columns = { "name" };
String where = BaseColumns._ID + "=?";
String [] whereArgs = { Long.toString(id) };
SQLiteCursor c = (SQLiteCursor) db.query(tableName, columns, where, whereArgs, null, null, null);
String r = "";
if (c.getCount() > 0) {
c.moveToFirst();
r = c.getString(0);
} else {
// the query returned nothing. Do appropriate action.
}
cursor.close();
db.close();
return r;
}
PS:记住我对你的问题的编辑。以这种方式格式化使用户更容易阅读。