0

I am using SQLite database in my application.I want to remove/delete the database but unable to do so.I have checked the location of the database and it is present at /data/data/org.secure.sms/databases but when i tried to delete it,it was not successful.Please help me. This is the code:

public static void DeleteDatabase(Context context)
{
    try
    {

    String filePath = context.getFilesDir()+ File.separator + "contactsManager";
    String filePath1 = context.getFilesDir()+ File.separator + "database";
    String filePath2 = context.getFilesDir()+ File.separator + "databases";

    context.deleteDatabase(filePath);
    context.deleteDatabase(filePath1);
    context.deleteDatabase(filePath2);

    File file = new File(filePath);
    File file1 = new File( filePath1 );

context.deleteFile(file.getName());
context.deleteFile(file1.getName());
    }
    catch(Exception e)
    {
        Toast.makeText(context,"Exception in DeleteDatabase",Toast.LENGTH_SHORT).show();
    }

}

Please help me.Thanks in advance.

1
  • can you please provide error stack? Commented Oct 16, 2012 at 9:59

3 Answers 3

2

I use this to delete my database, which is from a custom class which extends SQLiteOpenHelper.

private final static String DATABASE_NAME = "pain.db";

private static final String DB_PATH = "/data/data/YOUR.PACKAGE.NAME/databases/" + DATABASE_NAME;


@Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

        Log.d("DATABASE UPDATED", "DATABASE UPDATED");

        try {
            File file = new File(DB_PATH);
            file.delete();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

For deleting the database, this one is the code:

context.deleteDatabase(DATABASE_NAME);

In your code you are providing complete path.

4 Comments

sir i have provided complete path but i am getting this message in logcat:
10-16 15:15:44.578: D/(9319): unable to unlink '/data/data/org.secure.sms/files/contactsManager': No such file or directory (errno=2) 10-16 15:15:44.607: D/(9319): unable to unlink '/data/data/org.secure.sms/files/database': No such file or directory (errno=2)
@user1726619 : as error itself explains the database you are trying to delete is not present. Either path is wrong or try to add extension .db to your database name.
0

You are missing the file Extension !! Try below:

String filePath = context.getFilesDir()+ File.separator + "yourDataBaseName.db"; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.