Android:应用程序终止时刷新 sqlite 数据库

发布于 2024-12-04 17:54:46 字数 112 浏览 1 评论 0原文

我正在开发一个在 SQLite 数据库中存储一些信息的应用程序。

我的问题是,当应用程序被终止或手机重新启动时,我想清除数据库中的所有数据。 有谁知道我可以这样做吗?

谢谢 马可

I'm working on an app that stores some information inside an SQLite DB.

My problem is that I'd like to clear all data from the database when the app gets killed or the phone restarted.
Does anybody know I can I do this?

Thanks
Marco

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

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

发布评论

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

评论(3

再可℃爱ぅ一点好了 2024-12-11 17:54:46

可能没有一种完全万无一失的方法可以做到这一点。您可以清除活动的 onDestroy() 中的数据,但如果有人使用任务杀手之类的东西(对此非常确定),它可能不会被调用。
更好的选择是下次启动应用程序时清除数据。

There probably isn't a totally foolproof way of doing this. You could clear the data in onDestroy() of your activity, though it might not get called if someone uses something like a task killer (pretty sure about this).
A better option would be to clear the data the next time you start the app.

羅雙樹 2024-12-11 17:54:46

您应该看看 此方法子类化应用程序并在那里执行表删除/创建,而不是在 onStop 和 onPause 中。

You should take a look at this method of subclassing Application and performing your table dropping/creating there rather than in onStop and onPause.

失去的东西太少 2024-12-11 17:54:46

我认为您应该通过传递 null 作为数据库名称来使用内存中标志。这意味着数据库根本不会存储在 SD 卡上,因此无需在应用程序终止之前清除它。

I think you should use the in-memory flag by passing null as your db name. That means the db won't be stored on sd card at all so no need to clear it before the app dies.

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