Dragon Warrior
Dragon Warrior

Reputation: 327

SQLite delete isn't working

When I try delete a record in my database it is showing a null pointer exception error. This is my code:

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

Don't worry id has a value, I've already checked that!

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);
}
}

Log chat error at:

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

Source code available here.

Upvotes: 0

Views: 386

Answers (2)

Umer Waqas
Umer Waqas

Reputation: 1794

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

Upvotes: -1

DavidGSola
DavidGSola

Reputation: 727

It seems that your database could be closed when you are trying to delete the row. Try opening the database before the delete and closing it at the end.

Also, as a good advice you should do this if your ID is a String:

 ourDatabase.delete(DATABASE_TABLE[1], KEY_ID + "=?" , new String[]{id});

Upvotes: 2

Related Questions