这是我的DatabaseHelper,可以保存所有内容。如何擦除/清除/清除所有表格?
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
private static final String TAG = "DatabaseHelper";
private static final String DATABASE_NAME = "user.db";
private static final int DATABASE_VERSION = 1;
private RuntimeExceptionDao<CurrentUserModel, Long> currentUserModelDao;
private RuntimeExceptionDao<Contact, String> contactDao;
private RuntimeExceptionDao<ChatModel, Long> chatDao;
private RuntimeExceptionDao<ChatMessage, Long> chatMessageDao;
private RuntimeExceptionDao<ChatContact, Long> chatContactDao;
private RuntimeExceptionDao<BroadcastMessage, Long> broadcastMessageDao;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connection) {
try {
TableUtils.createTable(connection, CurrentUserModel.class);
TableUtils.createTable(connection, Contact.class);
TableUtils.createTable(connection, ChatModel.class);
TableUtils.createTable(connection, ChatMessage.class);
TableUtils.createTable(connection, ChatContact.class);
TableUtils.createTable(connection, BroadcastMessage.class);
} catch (SQLException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connection, int arg2, int arg3) {
try {
TableUtils.dropTable(connection, CurrentUserModel.class, true);
TableUtils.dropTable(connection, Contact.class, true);
TableUtils.dropTable(connection, ChatModel.class, true);
TableUtils.dropTable(connection, ChatMessage.class, true);
TableUtils.dropTable(connection, ChatContact.class, true);
TableUtils.dropTable(connection, BroadcastMessage.class, true);
onCreate(db, connection);
} catch (SQLException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
}
public RuntimeExceptionDao<CurrentUserModel, Long> getCurrentUserModelDao() {
if (currentUserModelDao == null) {
try {
currentUserModelDao = RuntimeExceptionDao.createDao(getConnectionSource(), CurrentUserModel.class);
currentUserModelDao.setObjectCache(true);
} catch (SQLException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
}
return currentUserModelDao;
}
public RuntimeExceptionDao<Contact, String> getContactDao() {
if (contactDao == null) {
try {
contactDao = RuntimeExceptionDao.createDao(getConnectionSource(), Contact.class);
contactDao.setObjectCache(true);
} catch (SQLException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
}
return contactDao;
}
public RuntimeExceptionDao<ChatModel, Long> getChatDao() {
if (chatDao == null) {
try {
chatDao = RuntimeExceptionDao.createDao(getConnectionSource(), ChatModel.class);
chatDao.setObjectCache(true);
} catch (SQLException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
}
return chatDao;
}
public RuntimeExceptionDao<ChatMessage, Long> getChatMessageDao() {
if (chatMessageDao == null) {
try {
chatMessageDao = RuntimeExceptionDao.createDao(getConnectionSource(), ChatMessage.class);
chatMessageDao.setObjectCache(true);
} catch (SQLException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
}
return chatMessageDao;
}
public RuntimeExceptionDao<ChatContact, Long> getChatContactDao() {
if (chatContactDao == null) {
try {
chatContactDao = RuntimeExceptionDao.createDao(getConnectionSource(), ChatContact.class);
chatContactDao.setObjectCache(true);
} catch (SQLException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
}
return chatContactDao;
}
public RuntimeExceptionDao<BroadcastMessage, Long> getBroadcastMessageDao() {
if (broadcastMessageDao == null) {
try {
broadcastMessageDao = RuntimeExceptionDao.createDao(getConnectionSource(), BroadcastMessage.class);
broadcastMessageDao.setObjectCache(true);
} catch (SQLException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
}
return broadcastMessageDao;
}
@Override
public void close() {
super.close();
if (currentUserModelDao != null) {
currentUserModelDao.clearObjectCache();
currentUserModelDao = null;
}
if (contactDao != null) {
contactDao.clearObjectCache();
contactDao = null;
}
if (chatDao != null) {
chatDao.clearObjectCache();
chatDao = null;
}
if (chatMessageDao != null) {
chatMessageDao.clearObjectCache();
chatMessageDao = null;
}
if (chatContactDao != null) {
chatContactDao.clearObjectCache();
chatContactDao = null;
}
if (broadcastMessageDao != null) {
broadcastMessageDao.clearObjectCache();
broadcastMessageDao = null;
}
}
}
我正在尝试进行某种注销,这将清除所有数据。这是我尝试过的:
DatabaseHelper dh = new DatabaseHelper(getActivity().getBaseContext());
dh.close();
clearApplicationData();
这只有在完成这两行之后才能工作,我从最近杀了应用程序并再次启动它,否则应用程序会再次尝试注册,并在尝试使用数据库时崩溃。如何正确重置安装时的所有应用程序数据?
答案 0 :(得分:4)
擦除所有表的最简单方法是实际删除整个数据库。
DatabaseHelper databaseHelper = getHelper();
databaseHelper.close();
while (databaseHelper.isOpen() == true) { // maybe you dont want to use while
Thread.sleep(500);
}
this.deleteDatabase("database.db"); // specified in DatabaseHelper class in the DATABASE_NAME field
执行此操作后,您必须创建一个新的数据库帮助程序(它重新创建数据库),否则您将获得异常,例如“无法打开已关闭的对象”:
OpenHelperManager.releaseHelper();
OpenHelperManager.setHelper(new DatabaseHelper(this));