Android无法升级我的数据库时,首先查询使用只读数据库我的、数据库、Android

2023-09-07 00:09:44 作者:∝秀甜蜜,死得快。

所以我一直到目前为止的自动化(以及几乎)的onCreate / onUpgrade 的方法严重依赖我的的SQLite 数据库。这是所有工作的罚款,我不得不改变数据库的版本号有它运行 onUpgrade 自动。

So I've been so far relying heavily on the automated (well almost) onCreate / onUpgrade method of my SQLite database. It was all working fine, and I just had to change the version number of the database to have it run onUpgrade automatically.

但现在,我运行到该异常时试图添加一列到我的数据库,并更改版本号:

But now, I'm running into that exception when trying to add a column to my database, and changing the version number:

SQLiteException: Can't upgrade read-only database from version X to Y

看着我的code,果然我的应用程序的第一个查询是 SELECT 查询和我使用 getReadableDatabase 来打开它的连接,因为请求不需要写访问。

Looking at my code, sure enough the first query of my application is a SELECT query, and I'm using getReadableDatabase to open a connection to it, because the request doesn't need write access.

所以我想,我应该如何解决这样的问题?我应该使用 getWritableDatabase 所有的时间? (作为一种变通方法,即工作让我的更新通过)当然,如果这两种方法都存在,这是因为他们是为了使用,该连接为只读访问需要时优化?我不想失去的利益。

So I was wondering, how should I solve such a problem? Should I use getWritableDatabase all the time? (as a workaround, that works to get my update to pass) Surely, if both methods exist that's because they are meant to be used and the connection is optimized for read-only access when needed? I wouldn't want to lose the benefit.

推荐答案

解决方法我用:在开始我的应用程序中有一个方法,不只是 getWritableDatabase()并关闭它​​,这样它会触发所有需要创建/更新,如果有必要的。

Workaround I used: have a method at the "start" of my application that does just getWritableDatabase() and closes it, so that it triggers all the needed create / update if necessary.