如何在 Android 中删除 SQLite 中的所有项目

发布于 2024-11-01 13:18:12 字数 132 浏览 4 评论 0原文

我想制作一个应用程序,用户单击按钮,SQLite 数据库就会被清除。到目前为止,这是我尝试过的:

db.delete(TABLE_NAME, null, null);

我做错了什么?

I would like to make an app where the user clicks a button, and the SQLite database is cleared. Here's what I've tried so far:

db.delete(TABLE_NAME, null, null);

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

○闲身 2024-11-08 13:18:12

您的 delete() 调用是正确的。你有可写数据库吗?这是使用删除方法的示例:

/**
 * Remove all users and groups from database.
 */
public void removeAll()
{
    // db.delete(String tableName, String whereClause, String[] whereArgs);
    // If whereClause is null, it will delete all rows.
    SQLiteDatabase db = helper.getWritableDatabase(); // helper is object extends SQLiteOpenHelper
    db.delete(DatabaseHelper.TAB_USERS, null, null);
    db.delete(DatabaseHelper.TAB_USERS_GROUP, null, null);
}

Your delete() call is correct. Did you get writable database? Here is example of method using delete:

/**
 * Remove all users and groups from database.
 */
public void removeAll()
{
    // db.delete(String tableName, String whereClause, String[] whereArgs);
    // If whereClause is null, it will delete all rows.
    SQLiteDatabase db = helper.getWritableDatabase(); // helper is object extends SQLiteOpenHelper
    db.delete(DatabaseHelper.TAB_USERS, null, null);
    db.delete(DatabaseHelper.TAB_USERS_GROUP, null, null);
}
清风挽心 2024-11-08 13:18:12

db.delete(TABLE_NAME, null, null);是删除表中所有行的正确语法。但我认为您会直接给出表名,而不用双引号将其括起来。尝试这样

db.delete("TABLE_NAME", null, null);

它可能会有所帮助:)

db.delete(TABLE_NAME, null, null); is the correct syntax to delete all rows in a table. But I think you would have given table name directly without enclosing it in double-quotes. Try like this

db.delete("TABLE_NAME", null, null);

It might help :)

你穿错了嫁妆 2024-11-08 13:18:12

使用此代码清除所有数据库内容

Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
String tableName="";
  if (c.moveToFirst()) {

      while ( !c.isAfterLast() ) {

          tableName = c.getString( c.getColumnIndex("name"));
          if(!tableName.equals('android_metadata')){
            db.execSQL("DROP TABLE '"+tableName+"'");
          }
          c.moveToNext();
      }
  }

c.close();

Use This Code for clear all database content

Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
String tableName="";
  if (c.moveToFirst()) {

      while ( !c.isAfterLast() ) {

          tableName = c.getString( c.getColumnIndex("name"));
          if(!tableName.equals('android_metadata')){
            db.execSQL("DROP TABLE '"+tableName+"'");
          }
          c.moveToNext();
      }
  }

c.close();
蘑菇王子 2024-11-08 13:18:12

实际上,我的另一种方法是DROP 表,然后调用SQLiteDatabaseonCreate 方法。我不知道哪个是最有效的 DeleteDrop 因为我还没有真正深入研究每种方法的效率,但它对我来说非常有效,因为在我的初始数据库我设置了一些默认值,因此当我调用数据库的 onCreate 方法时,我也有一些 PUT 方法。这节省了代码复制。 (我没有先执行删除,然后执行放置,而是从 onCreate 函数中获得多用途)。

我称之为重置。因此,您只需执行 db.reset() 操作,然后在创建表格后将默认值添加到 onCreate 中。

public void reset () throws SQLException {
    db = DBHelper.getWritableDatabase ();
    db.execSQL ("drop table "+TABLE_NAME);
    db.close ();
    this.DBHelper.onCreate (this.db);
}

Actually another way I did it was to DROP the tables and then just call the onCreate method for the SQLiteDatabase. I don't know which is most a efficient Delete or Drop because I haven't really dug deep into how efficient each method is but it works great for me because in my initial database I have some default values set and so when I call the onCreate method for the database I have some PUT methods there also. This saves code replication. ( instead of doing the delete and then the puts I get a multi purpose out of my onCreate function).

I call it reset. So you'd just have to do db.reset() and then the default values are added in the onCreate after you make your tables.

public void reset () throws SQLException {
    db = DBHelper.getWritableDatabase ();
    db.execSQL ("drop table "+TABLE_NAME);
    db.close ();
    this.DBHelper.onCreate (this.db);
}
浊酒尽余欢 2024-11-08 13:18:12
SQLiteDatabase db = this.getWritableDatabase(); //get database
        db.execSQL("DELETE FROM tablename"); //delete all rows in a table
db.close();
SQLiteDatabase db = this.getWritableDatabase(); //get database
        db.execSQL("DELETE FROM tablename"); //delete all rows in a table
db.close();
雨夜星沙 2024-11-08 13:18:12

在设备上开发时可以尝试的一件事:

设置>>应用程序>>(您的应用程序名称)>>存储>>清除缓存和清除数据

通过清除应用程序数据和应用程序缓存,您还可以擦除sql数据库。

One thing you could try when developing on a device:

settings>>applications>>(your application name)>>Storage>> clear cache and clear data

by clearing the application data and the application cache, you also wipe the sql database.

分开我的手 2024-11-08 13:18:12

使用此代码:

public void deleteAll()
{
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("delete from "+ TABLE_NAME);
    db.close();
}

您可以添加 DatabaseHandler 类,这是完整的源代码:

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
public static final String DATABASE_NAME = "OffLineMessagesClient";

// Contacts table name
public static final String TABLE_NAME = "messages_client";

// Contacts Table Columns names
private static final String KEY_ENTITY_ID = "entity_id";
private static final String KEY_MESSAGE = "message";

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "("
            + KEY_ENTITY_ID + " INTEGER,"
            + KEY_MESSAGE + " TEXT"
            +  ")";

    db.execSQL(CREATE_CONTACTS_TABLE);
}

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

    // Create tables again
    onCreate(db);
}

public void deleteAll()
{
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("delete from "+ TABLE_NAME);
    db.close();
}
}

use this code:

public void deleteAll()
{
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("delete from "+ TABLE_NAME);
    db.close();
}

you can add it DatabaseHandler class, this is full source code:

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
public static final String DATABASE_NAME = "OffLineMessagesClient";

// Contacts table name
public static final String TABLE_NAME = "messages_client";

// Contacts Table Columns names
private static final String KEY_ENTITY_ID = "entity_id";
private static final String KEY_MESSAGE = "message";

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "("
            + KEY_ENTITY_ID + " INTEGER,"
            + KEY_MESSAGE + " TEXT"
            +  ")";

    db.execSQL(CREATE_CONTACTS_TABLE);
}

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

    // Create tables again
    onCreate(db);
}

public void deleteAll()
{
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("delete from "+ TABLE_NAME);
    db.close();
}
}
淡笑忘祈一世凡恋 2024-11-08 13:18:12

对我来说经典的 mysql 语句:

SQLiteDatabase sqLiteDatabase = context.openOrCreateDatabase(Database.DATABASE_NAME, Context.MODE_PRIVATE, null, null);
sqLiteDatabase.execSQL("DELETE FROM " + tableName1);
sqLiteDatabase.execSQL("DELETE FROM " + tableName2);
sqLiteDatabase.execSQL("DELETE FROM " + tableName3);
sqLiteDatabase.execSQL("DELETE FROM " + tableName4);

完美地工作。祝你好运:)
ps.:没有开始/结束事务或关闭数据库...

for me classical mysql statement:

SQLiteDatabase sqLiteDatabase = context.openOrCreateDatabase(Database.DATABASE_NAME, Context.MODE_PRIVATE, null, null);
sqLiteDatabase.execSQL("DELETE FROM " + tableName1);
sqLiteDatabase.execSQL("DELETE FROM " + tableName2);
sqLiteDatabase.execSQL("DELETE FROM " + tableName3);
sqLiteDatabase.execSQL("DELETE FROM " + tableName4);

worked perfectly. good luck :)
ps.: without start/endtransaction or closeDatabase…

述情 2024-11-08 13:18:12

这对我有用:

 public void removeAll()
{

    SQLiteDatabase db = this.getWritableDatabase(); 
    db.delete(TABLE_NAME, null, null);
    db.close();



}

this work for me:

 public void removeAll()
{

    SQLiteDatabase db = this.getWritableDatabase(); 
    db.delete(TABLE_NAME, null, null);
    db.close();



}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文