无法在deleteDatabase后创建同名数据库

本文探讨了在Android开发中遇到的一个奇怪问题:使用deleteDatabase删除SQLite数据库后,登录时出现无法预料的行为和错误。文章提供了详细的解决方案,包括在删除数据库前关闭所有相关连接的方法。

Android开发,遇到一个奇怪的问题,在应用退出的时候,使用deleteDatabase删除sqlite数据库。

然后登录的时候,出现类似os_unix.c stat(/data/data/org.ibuo.demo/databases/demo.db) 

 android.database.sqlite.SQLiteDiskIOException: disk I/O error (code 1802)的错误。


后来在google code发现已经有人提出了同样的问题:

https://code.google.com/p/android/issues/detail?id=13727

Reported by  jschn...@gmail.comJan 5, 2011
This happened with multiple android versions and devicesI was developing on...

I attempted to clear the database when logging out of our app in two different ways:
1.  Deleting the file under "data/data/MY_APP/databases" directly
2.  Using context.deleteDatabase();

Both of these resulted in the file seeming to be deleted from the file system (it did not show up when listing files from an adb shell).

When logging back in however, the old data was still available, and there was no db file created on the file system.  SQLiteDatabase.getPath showed we were still looking at the same location however.  Using lsof, I determined that the file handle still existed.

This is unusual behavior which I would not have expected from the built in Context.deleteDatabase() method, and at least requires some explanation in the docs.

Workaround: 
I solved my problem by dropping all of the tables in the db and setting the version back to 0 instead of using Context.deleteDatabase().
=======================

是说被删除的文件的句柄还没有释放。后来用了其他的方法绕过去了这个问题。


在调试的过程中发现,SDK 2.2,2.3会出现数据库已删除,但是却能读出文件的问题。

而4.1,4.2,会直接报错。提示找不到文件的错误。

然后应用会重启,重启后,却可以了。


解决办法:

在deleteDatabase前,把同一句柄的db close掉。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值