如何在 Android 中重置 Sqlite 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21457432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:36:35  来源:igfitidea点击:

How to reset SqLite database in Android?

androidsqliteandroid-sqlite

提问by Roland

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.

我希望我的用户能够重置应用程序,然后我需要重置我创建的 SQLite 数据库。我怎样才能做到这一点?我想重置数据库或删除并重新创建数据库。

回答by Lingasamy Sakthivel

Just delete your database by

只需删除您的数据库

context.deleteDatabase(DATABASE_NAME);

Please make sure to close your database before deleting.

请确保在删除之前关闭您的数据库。

回答by RamIndani

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!

这应该从表中删除所有行,请参阅此处的 SQLite 数据库文档!

回答by user3307005

Working for me.

为我工作。

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

回答by Prateek

Just drop tables from database

只需从数据库中删除表

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

and again create the table in same database.

并再次在同一个数据库中创建表。

回答by Giacomoni

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

您可以使用 删除所有表的内容delete from table where 1=1,也可以onCreate再次调用数据库方法

回答by Muhammad Yaseen

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

我希望这会帮助你