views:

1204

answers:

1

I have a MenuItem in my Android application that deletes all the values in the database, its used more for my debugging purposes, after a while, inserting dummy values get redundant. Anyways, I have used LogCat to debug my program as it runs in the emulator and it gives me an error:

Failure 21 (out of memory) on 0x0 when preparing 'DELETE FROM <MY DBTABLE>'

I can post the code but I doubt its the code.

public void deleteAll(SQLiteDatabase db) {
   try {
        db.execSQL("DELETE FROM " + DATABASE_TABLE);} 
        Log.v(TAG, "Deleting database");
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        Log.v(TAG, "Dropping table");
        onCreate(db);
        Log.v(TAG, "Creating new database");} 
   catch (NullPointerException e) { }
   catch (SQLException e) {}
   }
 }

    @Override
public void onCreate(SQLiteDatabase db) {
 // TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + "(ID INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "LATITUDE INTEGER NOT NULL, LONGITUDE INTEGER NOT NULL, RATING INTEGER NOT NULL)");
}

This isnt my existing function, I had originally had just the:

db.execSQL("DELETE FROM " + DATABASE_TABLE);

but it didn't work, so I commented it out and then tried dropping the table first and THEN create a new table, but same error every time.

Does anyone know why this error occurred or a way for this error not to?

EDIT: This method does not run inside of a loop, its part of a Menu button, that when you select it, invokes the deleteAll() method. A point to note that I call this method through an Adapter class I had made, just to clarify here is the step-through of my code:

private TrackerDBAdapter db;

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case VIEW_DATABASE:
        Log.v(TAG, "Clicking on the view database menu option");
        Toast.makeText(this, "This should show the databases.",
                Toast.LENGTH_LONG).show();
        return true;
    case DELETE_VALUES:
        Log.v(TAG, "Clicking on the delete values menu option");
        db.deleteAll();
        Toast.makeText(this, "Database is empty",
                Toast.LENGTH_LONG).show();
        return true;

    }

Then inside my TrackAdapter class. The variable dbhelper is a DatabaseHelper object, which has the first deleteAll(SQLiteDatabase db) code in the question.

private DatabaseHelper dbhelper;
private SQLiteDatabase db;

public void deleteAll() {
        dbhelper.deleteAll(db);
}

I do not remember why I call a method, to call another method. It was out of desperation perhaps? But anyways, I am going to fiddle around with it and see if I clean up the code, maybe by doing so I can answer my own question.

A: 

I have disregarded invoking the method that invokes another method and checked up on SQLiteDatabase API and saw that it has a built-in delete function:

delete(String tableName, String[] whereClause, String whereArgs) {}

Passing in null as the whereClause deletes the whole entire row, so to add the finishing touches here is the new and improved delete function

public boolean deleteAll() {
 try {
 open();
 }
 catch (SQLException e){Log.v(DatabaseHelper.TAG, "SQLException caught");}
 return db.delete(DatabaseHelper.DATABASE_TABLE, null, null) > 0;
   }
Anthony Forloney
does that mean you still get the error? Or is it fixed?And about the logging of exceptions, consider appending at least "e.getMessage()". Without that, it's pretty much the same as writing "Unknown error!", or "oops!". Which won't help you wile debugging.
exhuma
Its completely fixed and yeah that's a good point to add about the exception, I am planning on rewriting things and currently I had maybe 13 rows at the time I posted. Good luck and thank you for the advice.
Anthony Forloney