向现有 sqlite 数据库添加字段
我已经发布了一个现有的应用程序,我想将位置坐标字段添加到 sqlite 数据库中。
我想知道是否可以在不在数据库中创建新表的情况下执行此操作。我不想覆盖用户现有的数据库条目,我只想为现有数据库条目添加这个新字段并为其指定默认值。
这可能吗?
I have an existing app published and I want to add location coords field to the sqlite database.
I want to know if it is possible to do this without creating a new table in the database. I don't want to overwrite the users existing database entries, I just want to add this new field for existing database entries and give it a default value.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,
您需要在更新表时编写
onUpgrade()
方法。目前,我使用以下命令创建一个包含新列的新表,并复制所有当前数据。希望您可以将其适应您的代码。其中包含
onUpgrade()
和两个辅助方法。DATABASE_UPGRADE
是一个包含升级数据库的字符串:关于其工作原理的快速说明:
onUpgrade()
应该存在的那样在这种情况下不会被叫到。GetColumns()
)。我尝试编写足够通用的代码,所以我所要做的就是使用附加列更新 DATABASE_UPGRADE,这将处理所有其余的事情。到目前为止,它已经对我进行了 3 次升级。
Yes it is,
You need to write your
onUpgrade()
method when you update your table. Currently, I use the following to create a new table with the new column and to copy all my current data over. Hopefully you can adapt this to your code.This contains
onUpgrade()
and two helper methods.DATABASE_UPGRADE
is a string that contains the upgrade database:Quick note about how this works:
onUpgrade()
shouldn't get called in this case.GetColumns()
).I tried to write this generic enough so all i have to do is update DATABASE_UPGRADE with the additional columns and this handles all the rest. It has worked for me through 3 upgrade so far.
您可以使用 ALTER TABLE 添加列。
更改表 my_table 添加列位置 ...;
You can add columns using ALTER TABLE.
ALTER TABLE my_table ADD COLUMN location ...;
使用 onUpgrade 方法来运行“alter table”语句。
Use the onUpgrade method of the SQLiteOpenHelper to run the "alter table" statements.