SQLite删除不起作用

时间:2014-12-02 11:34:20

标签: android sqlite delete-row

当我尝试删除数据库中的记录时,它显示空指针异常错误。这是我的代码:

ourDatabase.delete(DATABASE_TABLE[1], KEY_ID + " = " + id, null);

不要担心id有价值,我已经检查过了!

public class DiaryDB {
// User info
public static final String KEY_NAME = "my_name";
public static final String KEY_PASSWORD = "my_password";
public static final String KEY_QUESTION_ID = "my_question";
public static final String KEY_ANSWER = "my_answer";

// Entry details
public static final String KEY_ID = "_id";
public static final String KEY_DATE = "entry_date";
public static final String KEY_TIME = "entry_time";
public static final String KEY_TITLE = "entry_title";
public static final String KEY_CONTENT = "_entry";
public static final String KEY_RATE = "entry_rating";
public static final String KEY_IMAGE = "entry_image";

private static final String DATABASE_NAME = "MyDiary";
private static final String[] DATABASE_TABLE = { "My_Details", "My_Entries" };
private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

public DiaryDB(Context c) {
    ourContext = c;
}

public DiaryDB open() throws SQLException {
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() {
    ourHelper.close();
}

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) {
        db.execSQL("CREATE TABLE " + DATABASE_TABLE[0] + " (" + KEY_NAME
                + " TEXT PRIMARY KEY NOT NULL, " + KEY_PASSWORD
                + " TEXT NOT NULL, " + KEY_QUESTION_ID + " TEXT NOT NULL, "
                + KEY_ANSWER + " TEXT NOT NULL);");

        db.execSQL("CREATE TABLE " + DATABASE_TABLE[1] + " (" + KEY_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DATE
                + " TEXT NOT NULL, " + KEY_TIME + " TEXT NOT NULL, "
                + KEY_TITLE + " TEXT NOT NULL, " + KEY_CONTENT
                + " TEXT NOT NULL, " + KEY_RATE + " REAL, " + KEY_IMAGE
                + " BLOB);");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE[0]);
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE[1]);
        onCreate(db);
    }

}


public void deleteEntry(long id){
    Log.e("Tester", "" + id);
    ourDatabase.delete(DATABASE_TABLE[1], KEY_ID + " = " + id, null);
}
}

在以下位置记录聊天错误:

  

android.database.sqlite.SQLiteStatement.releaseAndUnlock(SQLiteStatement.java:290)

可用的源代码here

2 个答案:

答案 0 :(得分:2)

当您尝试删除行时,似乎可以关闭您的数据库。尝试在delete之前打开数据库并在结束时关闭它。

另外,如果你的ID是一个字符串:

,那么你应该这样做
 ourDatabase.delete(DATABASE_TABLE[1], KEY_ID + "=?" , new String[]{id});

答案 1 :(得分:-1)

String where = KEY_ID+ "=?";
String[] selectionArgs = {String.valueOf(id)};
yourDatabase.delete(DATABASE_TABLE[1], where, selectionArgs);