如何在 Android 中删除 SQLite 中的所有项目
我想制作一个应用程序,用户单击按钮,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您的 delete() 调用是正确的。你有可写数据库吗?这是使用删除方法的示例:
Your delete() call is correct. Did you get writable database? Here is example of method using delete:
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
It might help :)
使用此代码清除所有数据库内容
Use This Code for clear all database content
实际上,我的另一种方法是
DROP
表,然后调用SQLiteDatabase
的onCreate
方法。我不知道哪个是最有效的Delete
或Drop
因为我还没有真正深入研究每种方法的效率,但它对我来说非常有效,因为在我的初始数据库我设置了一些默认值,因此当我调用数据库的 onCreate 方法时,我也有一些 PUT 方法。这节省了代码复制。 (我没有先执行删除,然后执行放置,而是从onCreate
函数中获得多用途)。我称之为
重置
。因此,您只需执行db.reset()
操作,然后在创建表格后将默认值添加到onCreate
中。Actually another way I did it was to
DROP
the tables and then just call theonCreate
method for theSQLiteDatabase
. I don't know which is most a efficientDelete
orDrop
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 theonCreate
method for the database I have somePUT
methods there also. This saves code replication. ( instead of doing the delete and then the puts I get a multi purpose out of myonCreate
function).I call it
reset
. So you'd just have to dodb.reset()
and then the default values are added in theonCreate
after you make your tables.在设备上开发时可以尝试的一件事:
设置>>应用程序>>(您的应用程序名称)>>存储>>清除缓存和清除数据
通过清除应用程序数据和应用程序缓存,您还可以擦除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.
使用此代码:
您可以添加 DatabaseHandler 类,这是完整的源代码:
use this code:
you can add it DatabaseHandler class, this is full source code:
对我来说经典的 mysql 语句:
完美地工作。祝你好运:)
ps.:没有开始/结束事务或关闭数据库...
for me classical mysql statement:
worked perfectly. good luck :)
ps.: without start/endtransaction or closeDatabase…
这对我有用:
this work for me: