How can I clear an SQLite database each time I start my application?

前端 未结 4 1684
不知归路
不知归路 2020-12-06 03:16

I want my SQLite database instance to be wiped away when the program starts.

What I tried was make a method on my class MyDBAdapter.java like this:

p         


        
4条回答
  •  醉酒成梦
    2020-12-06 04:08

    The quick and easy solution that I used was to delete the database file in the OnCreate() method by calling another method named doDBCheck(). The doDBCheck() looks for the file on the emulator/phone's file system and if it exists, remove it.

     private static final String DB_PATH = "data/data//databases/";
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mainView = (TextView) findViewById(R.id.mainView);
        doDBCheck();
    
    }
    
    private void doDBCheck()
    {
        try{
                File file = new File(DB_PATH);
                file.delete();
        }catch(Exception ex)
        {}
    }
    

提交回复
热议问题