2

I want to delete the SQLite database from device. I am trying to delete it but its not getting delete, because I can see the old records of database.

Trying to delete like this:

        deleteDatabase(Constants.DATABASE_NAME);

            DatabaseHelper db = new DatabaseHelper(this);
            db.createDatabase();

Constants :

    public class Constants {

    public static final int DATABASE_VERSION = 12;
    public static final String DATABASE_NAME = "contactsApp";
}

DatabaseHelper :

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String USER_TABLE = "userTable";
    private static final String KEY_USER_NAME = "userName";
    private static final String KEY_EMAIL_ID = "emailId";
    private static final String KEY_MOBILE_NO = "mobileNo";
    private static final String KEY_PROFILE_IMAGE = "profileImage";
    private static final String KEY_FULL_NAME = "fullName";
    private static final String KEY_DEVICE_ID = "deviceId";
    private static final String KEY_PASSWORD ="password";
    private static final String KEY_USER_ID = "userId";
    private static final String KEY_JOB_TITLE = "jobTitle";
    private static final String KEY_WORK_ADDRESS = "workAddress";
    private static final String KEY_WORK_PHONE = "workPhone";
    private static final String KEY_HOME_ADDRESS = "homeAddress";


    private static final String CONTACT_TABLE = "contactTable";
    private static final String KEY_CONTACT_NAME = "contactName";
    private static final String KEY_CONTACT_EMAIL_ID = "contactEmailId";
    private static final String KEY_CONTACT_MOBILE_NO = "contactMobileNo";
    private static final String KEY_CONTACT_PROFILE_IMAGE = "contactProfileImage";
    private static final String KEY_CONTACT_ID = "contactId";
    private static final String KEY_CONTACT_FULL_NAME = "fullName";
    private static final String KEY_CONTACT_JOB_TITLE = "jobTitle";
    private static final String KEY_CONTACT_WORK_ADDRESS = "workAddress";
    private static final String KEY_CONTACT_WORK_PHONE = "workPhone";
    private static final String KEY_CONTACT_HOME_ADDRESS = "homeAddress";

    private Context context;

    public DatabaseHelper(Context context) {
        super(context, Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION);
        //3rd argument to be passed is CursorFactory instance
        this.context = context;
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {

        createTable(db);

    }

    public void createDatabase(){
        context.deleteDatabase(Constants.DATABASE_NAME + ".db");
        SQLiteDatabase db = this.getReadableDatabase();
    }


    public void createTable(SQLiteDatabase db){
        String CREATE_USER_TABLE = "CREATE TABLE " + USER_TABLE + "("
                + KEY_USER_ID + " TEXT,"
                + KEY_USER_NAME + " TEXT,"
                + KEY_PASSWORD + " TEXT,"
                + KEY_MOBILE_NO + " TEXT,"
                + KEY_EMAIL_ID + " TEXT,"
                + KEY_PROFILE_IMAGE + " TEXT,"
                + KEY_FULL_NAME + " TEXT,"
                + KEY_DEVICE_ID + " TEXT,"
                + KEY_JOB_TITLE + " TEXT,"
                + KEY_HOME_ADDRESS + " TEXT,"
                + KEY_WORK_ADDRESS + " TEXT,"
                + KEY_WORK_PHONE + " TEXT " + ")";

        db.execSQL(CREATE_USER_TABLE);

        String CREATE_CONTACT_TABLE = "CREATE TABLE " + CONTACT_TABLE + "("
                + KEY_CONTACT_ID + " TEXT,"
                + KEY_CONTACT_NAME + " TEXT,"
                + KEY_CONTACT_MOBILE_NO + " TEXT,"
                + KEY_CONTACT_EMAIL_ID + " TEXT,"
                + KEY_CONTACT_PROFILE_IMAGE + " TEXT,"
                + KEY_CONTACT_FULL_NAME + " TEXT,"
                + KEY_CONTACT_JOB_TITLE+ " TEXT,"
                + KEY_CONTACT_WORK_ADDRESS + " TEXT,"
                + KEY_CONTACT_WORK_PHONE + " TEXT,"
                + KEY_CONTACT_HOME_ADDRESS + " TEXT " + ")";

        db.execSQL(CREATE_CONTACT_TABLE);

    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + USER_TABLE);
        db.execSQL("DROP TABLE IF EXISTS " + CONTACT_TABLE);

        context.deleteDatabase(Constants.DATABASE_NAME + ".db");

        createTable(db);

    }

}

I also tried to delete only tables, but its not getting delete.

What can be the issue? Can anyone help please? Thank you..

2

3 Answers 3

3
String myPath = DB_PATH + DB_NAME;
SQLiteDatabase.deleteDatabase(new File(myPath));

OR

 context.deleteDatabase(DATABASE_NAME);

for more detail.. How to delete SQLite database from Android programmatically

Sign up to request clarification or add additional context in comments.

6 Comments

I tried with context.deleteDatabase(DATABASE_NAME); but still its not getting delete. @Nikhil Boras
onUpgrade method swap last two line up to down.. after deleting you create new one.
how will it create new database if I swap? @Nikhil Borad
dosen't help..@Nikhil Borad
how to get path for your this answer : String myPath = DB_PATH + DB_NAME; @ Nikhil Borad
|
0

Make sure you close all Database connections before deleting. Else you'll have to restart the application to see the change

2 Comments

If I am restarting the application still I cant see the change. @Anto
Did you make sure all connection to DB were closed before deleting ?
0
context.deleteDatabase(DATABASE_NAME);

When this line gets run, the database should be deleted. This is a proper way to delete database file after database was created by getDatabasePath()//

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.