在java中,如何删除一个sqlite表?

时间:2022-10-04 09:21:52

I am developing android app. I have to develop a xml button in my activity, and construct my sqlite database and tables. How can I just let user press a button to delete a table? Thanks.

我正在开发android应用,我需要在我的活动中开发一个xml按钮,并构建我的sqlite数据库和表格。如何让用户按下按钮删除表?谢谢。

4 个解决方案

#1


33  

Hard to answer without more context, but the ultimate sqlite query would be:

没有更多的上下文很难回答,但是sqlite查询的最终结果是:

db.execSQL("DROP TABLE IF EXISTS table_name");

Where db is a reference to a SqliteDatabase object.

其中db是对SqliteDatabase对象的引用。

#2


13  

There is some ambiguity with your question. Note that there is a difference between DELETING a table and DROPPING a table. Deleting the table simply erases all data from its rows:

你的问题有些含糊不清。注意,删除表和删除表是有区别的。删除该表只会删除其行中的所有数据:

database.delete(TABLE_NAME, null, null);

After this, you can still reference the table because it still exists, but creating a new one with the same name may be problematic without using the CREATE TABLE IF NOT EXISTS expression in sql.

在此之后,您仍然可以引用该表,因为该表仍然存在,但是如果不使用sql中的CREATE table(如果不存在表达式),则创建具有相同名称的新表可能会有问题。

Using DROP TABLE completely removes the table and it cannot be referenced again unless it is re-created.

使用DROP TABLE将完全删除该表,除非重新创建该表,否则不能再次引用该表。

As noted by others, this should work if you want it completely removed from the database:

正如其他人所指出的,如果您希望将其从数据库中完全删除,那么这应该是可行的:

db.execSQL("DROP TABLE IF EXISTS table_Name");

#3


0  

SQLiteDatabase sdb;
sdb=openOrCreateDatabase("dbname.db", Context.MODE_WORLD_WRITEABLE, null);
sdb.execSQL("DROP TABLE IF EXISTS tablename");

#4


0  

As a Singleton method

作为一个单例方法

 // todo DELETE a special field(s)
public boolean deleteFromTable(int yurtId) {
    SQLiteDatabase database = dbHelper.getReadableDatabase();
    final String whereClause = DBHelper.COLUMN_Y_YURT_ID + " =?";
    final String whereArgs[] = {String.valueOf(yurtId)};
    int affectedRows = database.delete(DBHelper.TABLE_NAME_YEMEKCI, whereClause, whereArgs);
    return affectedRows > 0;
}

  // todo DELETE all table 
public boolean deleteTable() {
    SQLiteDatabase database = dbHelper.getReadableDatabase();
    int affectedRows = database.delete(DBHelper.TABLE_NAME_YEMEKCI, null, null);
    return affectedRows > 0;
}

#1


33  

Hard to answer without more context, but the ultimate sqlite query would be:

没有更多的上下文很难回答,但是sqlite查询的最终结果是:

db.execSQL("DROP TABLE IF EXISTS table_name");

Where db is a reference to a SqliteDatabase object.

其中db是对SqliteDatabase对象的引用。

#2


13  

There is some ambiguity with your question. Note that there is a difference between DELETING a table and DROPPING a table. Deleting the table simply erases all data from its rows:

你的问题有些含糊不清。注意,删除表和删除表是有区别的。删除该表只会删除其行中的所有数据:

database.delete(TABLE_NAME, null, null);

After this, you can still reference the table because it still exists, but creating a new one with the same name may be problematic without using the CREATE TABLE IF NOT EXISTS expression in sql.

在此之后,您仍然可以引用该表,因为该表仍然存在,但是如果不使用sql中的CREATE table(如果不存在表达式),则创建具有相同名称的新表可能会有问题。

Using DROP TABLE completely removes the table and it cannot be referenced again unless it is re-created.

使用DROP TABLE将完全删除该表,除非重新创建该表,否则不能再次引用该表。

As noted by others, this should work if you want it completely removed from the database:

正如其他人所指出的,如果您希望将其从数据库中完全删除,那么这应该是可行的:

db.execSQL("DROP TABLE IF EXISTS table_Name");

#3


0  

SQLiteDatabase sdb;
sdb=openOrCreateDatabase("dbname.db", Context.MODE_WORLD_WRITEABLE, null);
sdb.execSQL("DROP TABLE IF EXISTS tablename");

#4


0  

As a Singleton method

作为一个单例方法

 // todo DELETE a special field(s)
public boolean deleteFromTable(int yurtId) {
    SQLiteDatabase database = dbHelper.getReadableDatabase();
    final String whereClause = DBHelper.COLUMN_Y_YURT_ID + " =?";
    final String whereArgs[] = {String.valueOf(yurtId)};
    int affectedRows = database.delete(DBHelper.TABLE_NAME_YEMEKCI, whereClause, whereArgs);
    return affectedRows > 0;
}

  // todo DELETE all table 
public boolean deleteTable() {
    SQLiteDatabase database = dbHelper.getReadableDatabase();
    int affectedRows = database.delete(DBHelper.TABLE_NAME_YEMEKCI, null, null);
    return affectedRows > 0;
}