How to reset SqLite database in Android?

∥☆過路亽.° 提交于 2020-03-14 05:42:05

问题


I want my users to be able to reset the application, then I need to reset the SQLite database that I have created. How can I do that? I want to reset the database or delete and recreate the database.


回答1:


Just delete your database by

context.deleteDatabase(DATABASE_NAME);

Please make sure to close your database before deleting.




回答2:


Bit late but this can help other people

public void clearDatabase(String TABLE_NAME) {  
   String clearDBQuery = "DELETE FROM "+TABLE_NAME;  
   db.execSQL(clearDBQuery);  
   }  

This should remove all the rows from the table refer documentation of SQLite database here!




回答3:


Working for me.

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS TABLE_NAME");
    onCreate(db);
}



回答4:


Just drop tables from database

db.execSQL("DROP TABLE "+TABLENAME);

and again create the table in same database.




回答5:


You can delete the content of all your tables using delete from table where 1=1, or you can call your database onCreate method again




回答6:


There are two option to clear the table from the database

Firstly

If you wan to delete the data on specific row you need to add this code in the database class

    public Boolean specification(int id, String table_name)
     {
         return db.delete(table_name, KEY_ID + "=" + id, null) > 0;
     }

and add the below code when you want to perform this action

  db.deleteSpecificOrder(id, "table_orders");

Secondly

If you want to delete all the data from th table then you just need to add below code into your database

public void clearDatabase(String TABLE_NAME) {
    db = this.getReadableDatabase();
    String clearDBQuery = "DELETE FROM " + TABLE_NAME;
    db.execSQL(clearDBQuery);
}

and then add the below line where you want to perform that action

 db.clearDatabase("table_food_items");

I Hope that will help you



来源:https://stackoverflow.com/questions/21457432/how-to-reset-sqlite-database-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!