1

I have a short question to the notepad tutorial on the android website. I wrote a simple function in the tutorial code to delete the whole database. It looks like this:

DataHelper.java

public void deleteDatabase() {
    this.mDb.delete(DATABASE_NAME, null, null);
}

Notepadv1.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    boolean result = super.onCreateOptionsMenu(menu);
    menu.add(0, DELETE_ID, 0, "Delete whole Database");
    return result;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {     
    case DELETE_ID:
        mDbHelper.deleteDatabase();      
        return true;
    }       
    return super.onOptionsItemSelected(item);
}

But when I run the app and try to delete the database I will get this error in LogCat:

sqlite returned: error code = 1, msg= no such table: data

Can you help how to fix this problem. It seems that the function deleteDatabase can not reach the database.

Thank you very much.

Felix


Thank you for the posts. Now I updated the code:

DataHelper.java

public boolean deleteDatabase(Context context) {
    return context.deleteDatabase(DATABASE_NAME);
}

Notepadv1.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    boolean result = super.onCreateOptionsMenu(menu);
    menu.add(0, DELETE_ID, 0, "Delete whole Database");
    return result;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {     
    case DELETE_ID:
        mDbHelper.deleteDatabase(this);      
        return true;
    }       
    return super.onOptionsItemSelected(item);
}

But now I have to restart the app that the database is deleted. I is not working while the app is running.

FelixA
  • 127
  • 5
  • 14

2 Answers2

3

The delete method you are using from a SQLiteDatabase object is to delete tables, not database.

Have a look at the delete method in the Context

Edit:

Try calling from your activity directly:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {     
    case DELETE_ID:
        deleteDatabase(DATABASENAME);      
        return true;
    }       
    return super.onOptionsItemSelected(item);
}

The way you are calling at the moment is to delete a database from a SQLliteDatabase object so I guess it won't drop the database until there is no more references to it.

ccheneson
  • 49,072
  • 8
  • 63
  • 68
  • oh thanks. I changed the delete methode: public boolean deleteDatabase(Context context) { return context.deleteDatabase(DATABASE_NAME); }. Now the database will really be deleted but not until I have not closed my app. If I close the app and start it new, the database is really deleted. What I am doing wrong? – FelixA Dec 25 '10 at 08:58
  • In the Notepadv1.jva file the code look like this. mDbHelper.deleteDatabase(this); – FelixA Dec 25 '10 at 09:05
  • I dont see any reference to 'delete' in Notepadv1.java http://www.koders.com/java/fid662C390F21E9B60283DFA00EF51B5A75D3014B88.aspx?s=NOTES#L30 – ccheneson Dec 25 '10 at 09:17
  • Thank's for the Edit. I tried it but also here the database is not deleted instantaneous. The screen is not updated. I have to restart the app to see that the database is deleted. I tried to call deleteDatabase("data"); fillData(); return true; But even this didn't help. – FelixA Dec 25 '10 at 09:37
  • I did some tests and I can't figure it out :( . I ll think about it – ccheneson Dec 25 '10 at 10:02
0

Per the android documentation, the delete method deletes rows from a table in the database. It doesn't delete the database itself.

I believe what you want instead is Context.deleteDatabase

JesusFreke
  • 19,784
  • 5
  • 65
  • 68