如果不存在,则将列添加到 sqlite db - flex/air sqlite?
我有一个一直在开发的 Flex/air 应用程序,它使用在初始应用程序启动时创建的本地 sqlite 数据库。
我已经向应用程序添加了一些功能,在此过程中我必须向其中一个数据库表添加一个新字段。我的问题是如何让应用程序创建一个位于已存在的表中的新字段?
这是创建表的行
stmt.text = "CREATE TABLE IF NOT EXISTS tbl_status ("+"status_id INTEGER PRIMARY KEY AUTOINCREMENT,"+" status_status TEXT)";
现在我想添加一个 status_default 字段。
谢谢!
谢谢 - MPelletier
我已经添加了您提供的代码,并且它确实添加了该字段,但现在下次我重新启动我的应用程序时,我收到错误 - “status_default”已经存在”。
那么我该如何在您提供的行中添加某种 IF NOT EXISTS 语句呢?
I've got a flex/air app I've been working on, it uses a local sqlite database that is created on the initial application start.
I've added some features to the application and in the process I had to add a new field to one of the database tables. My questions is how to I go about getting the application to create one new field that is located in a table that already exists?
this is a the line that creates the table
stmt.text = "CREATE TABLE IF NOT EXISTS tbl_status ("+"status_id INTEGER PRIMARY KEY AUTOINCREMENT,"+" status_status TEXT)";
And now I'd like to add a status_default field.
thanks!
Thanks - MPelletier
I've add the code you provided and it does add the field, but now the next time I restart my app I get an error - 'status_default' already exists'.
So how can I go about adding some sort of a IF NOT EXISTS statement to the line you provided?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
http://www.sqlite.org/lang_altertable.html
也就是说,在 SQLite 中添加列是有限的。除了表中最后一列之后之外,您不能在任何位置添加列。
至于检查该列是否已存在,
PRAGMA table_info(tbl_status);
将返回一个列出表中各个列的表。附加:
我一直在数据库设计中使用一种策略,该策略使我能够区分需要哪些修改。为此,您需要一个新表(称为
DBInfo
),其中包含一个字段(整数,称为SchemaVersion
)。或者,SQLite 中还有一个名为user_version
的内部值,可以使用PRAGMA
命令。您的代码可以在程序启动时检查架构版本号并相应地应用更改,一次一个版本。假设有一个名为
UpdateDBSchema()
的函数。该函数将检查您的数据库模式版本,处理不存在的 DBInfo,并确定数据库处于版本 0。该函数的其余部分可能只是一个具有不同版本的大型开关,嵌套在循环中(或其他可用的结构)到您选择的平台)。因此,对于第一个版本,有一个
UpgradeDBVersion0To1()
函数,它将创建这个新表 (DBInfo
),添加您的status_default
字段,然后将SchemaVersion
设置为 1。在您的代码中,添加一个指示最新架构版本的常量,例如LATEST_DB_VERSION
,并将其设置为 1。这样,您的代码和您的数据库有一个架构版本,如果它们不相等,您知道需要同步它们。当您需要对架构进行另一次更改时,请将
LATEST_DB_VERSION
常量设置为 2,并创建一个新的UpgradeDBVersion1To2()
函数来执行所需的更改。这样,您的程序就可以轻松移植,可以连接和升级旧数据库等。
http://www.sqlite.org/lang_altertable.html
That being said, adding columns in SQLite is limited. You cannot add a column anywhere but after the last column in your table.
As for checking if the column already exists,
PRAGMA table_info(tbl_status);
will return a table listing the various columns of your table.ADD ON:
I've been using a strategy in database design that allows me to distinguish which modifications are required. For this, you will need a new table (call it
DBInfo
), with one field (Integer, call itSchemaVersion
). Alternately, there is also an internal value in SQLite calleduser_version
, which can be set with aPRAGMA
command. Your code can, on program startup, check for schema version number and apply changes accordingly, one version at a time.Suppose a function named
UpdateDBSchema()
. This function will check for your database schema version, handle DBInfo not being there, and determine that the database is in version 0. The rest of this function could be just a large switch with different versions, nested in a loop (or other structure available to your platform of choice).So for this first version, have an
UpgradeDBVersion0To1()
function, which will create this new table (DBInfo
), add yourstatus_default
field, and setSchemaVersion
to 1. In your code, add a constant that indicates the latest schema version, sayLATEST_DB_VERSION
, and set it to 1. In that way, your code and your database have a schema version, and you know you need to synch them if they are not equal.When you need to make another change to your schema, set the
LATEST_DB_VERSION
constant to 2 and make a newUpgradeDBVersion1To2()
function that will perform the required changes.That way, your program can be ported easily, can connect to and upgrade an old database, etc.
我知道这是一个老问题......但是。
我在 Adobe AIR 中的 SQLite 实现中遇到了这个精确的问题。我以为可以使用PRAGMA命令来解决,但是由于adobe air的实现不支持PRAGMA命令,所以我们需要一个替代方案。
我所做的,我认为值得在这里分享的,是:
希望它能帮助别人。
I know this is an old question... however.
I've hit this precise problem in the SQLite implementation in Adobe AIR. I thought it would be possible to use the PRAGMA command to resolve, but since adobe air's implementation does not support the PRAGMA command, we need an alternative.
What I did, that I thought would be worth while sharing here, is this:
Hope it helps someone.
在某些情况下,我执行命令并得到“重复列”的异常。只是一个快速的解决方案,而不是完美的。
In some cases I execute the command and get the exception for "duplicate column". Just a quick solution, not the perfect.
我使用这个问题的答案解决了类似的问题:
ALTER TABLE ADD COLUMN IF NOT EXISTS in SQLite
使用内置 user_version 参数来跟踪您的更新。你设置它使用:
并检索它使用
所以基本上检索 user_version (默认情况下为 0),检查它是否为 0。如果是,则执行更新并将其设置为 1。如果将来有更多更新,请检查它是否为 1 1、执行更新并将其设置为0。依此类推...
I solved a similar problem using the answer from this question:
ALTER TABLE ADD COLUMN IF NOT EXISTS in SQLite
Use built in user_version parameter to keep track of your updates. You set it using:
and you retrieve it using
So basically retrieve user_version (it's 0 by default), check if it's 0. If yes, perform your updates and set it to 1. If you have more updates in the future, check if it's 1, perform updates and set it to 0. And so on...