I have to delete the sqlite data. I haven't been able to find a way to delete only the data (completely), but I did find the method to delete the database completely.
Context.deleteDatabase(“nombre_tu_base_datos_a_borrar”);
I have tested it and it really works.
I have put this code when starting the first activity. (in my case a login) so that later it recreates the correct structure of my database
The detail is that every time the user closes the app or logs out. go back to this activity and as you will understand the code to delete the database is maintained so it will delete again, which I am trying to avoid.
That's where I need to know what the best way is. I have managed so far to do it only once using preferense
String m_identificadorBorrarBD = sharedPrefs.getString("m_identificadorBorrarBD", "0");
if(m_identificadorBorrarBD.equals("0")) {
getApplicationContext().deleteDatabase(“nombre_bd”);
valoresGenerales.sharedPrefs.edit().putString("m_identificadorBorrarBD", "1").apply();
}
So far it works, but how would you do so that at another time you have to delete the database. The preference is already at 1.
Of course they will tell me to add the 1 in if in this way.
String m_identificadorBorrarBD = sharedPrefs.getString("m_identificadorBorrarBD", "0");
if(m_identificadorBorrarBD.equals("0") || m_identificadorBorrarBD.equals("1")) {
getApplicationContext().deleteDatabase(“nombre_bd”);
valoresGenerales.sharedPrefs.edit().putString("m_identificadorBorrarBD", "2").apply();
}
the idea is correct (because it works), but every time I need to delete I will have to add more values in that if and increase the value.
Is there a better way?
Alternative
Create a method before login that is executed manually (by the user)
Apparently the admin doesn't support this answer as a solution.
I understand that the logic depends on the form and visualization of each developer.
I couldn't find any solution to help me so I chose to do it manually.
create a button that if the user presses performs the function of clearing data.