本文介绍了添加列到的SQLite数据库IF NOT EXISTS - 柔性/空气源码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Flex / AIR应用程序我一直在工作,它会使用在初始应用程序启动创建一个本地SQLite数据库。

我增加了一些功能的应用程序,并在这个过程中我有一个新的字段添加到数据库表中的一个。我的问题是我如何去获得应用程序创建一个位于已存在?

一个表中的一个新领域

这是一个创建表行

  stmt.text =CREATE TABLE IF NOT EXISTS tbl_status(+status_id INTEGER PRIMARY KEY AUTOINCREMENT,+status_status文本);
 

现在,我想补充一个status_default场。

谢谢!

感谢 - MPelletier

我已经加你提供的code和它添加字段,但现在我下次重新启动我的应用程序,我碰到一个错误 - status_default'已经存在

所以,我怎么能去有关添加某种一个IF NOT EXISTS的语句来你提供的行?

解决方案

  ALTER TABLE tbl_status ADD COLUMN status_default TEXT;
 

http://www.sqlite.org/lang_altertable.html

话虽这么说,SQLite中添加列是有限的。你不能在任何地方,但在表的最后一栏后添加一列。

至于检查,如果该列已经存在, PRAGMA table_info(tbl_status); 将返回一个表,其中列出表的各列

Add(添加):

我一直在使用数据库设计的策略,让我来区分哪些修改是必需的。对于这一点,你需要一个新的表(称之为 DBINFO ),有一个字段(整数,称之为 SchemaVersion )。另外,也有在SQLite的名为 user_version ,内部值可以用的命令。您的code能,在程序启动时,检查架构版本号和相应的应用更改,一个版本的时间。

假设命名函数 UpdateDBSchema()。这个功能将检查你的数据库架构版本,处理DBINFO不在那里,并确定该数据库是在0版本的函数的其他部分可能只是一个大的交换机不同的版本,嵌套在一个循环(或其它结构可用为您选择的平台)。

因此​​,对于这第一个版本中,有一个 UpgradeDBVersion0To1()的功能,这将创建这个新表( DBINFO ),添加 status_default 字段,并设置 SchemaVersion 1。在您的code,加一个常数,表示最新的架构版本,说 LATEST_DB_VERSION ,并将其设置为1。这样一来,你的code和你的数据库有一个架构版本,你知道你需要要同步他们,如果他们不相等。

当你需要再作改变你的模式,将 LATEST_DB_VERSION 常数2,并作出新的 UpgradeDBVersion1To2()函数,将执行所需的更改。

这样的话,你的程序可以很容易的移植,可以连接到升级旧的数据库,等等。

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?

解决方案
ALTER TABLE tbl_status ADD COLUMN status_default TEXT;

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 it SchemaVersion). Alternately, there is also an internal value in SQLite called user_version, which can be set with a PRAGMA 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 your status_default field, and set SchemaVersion to 1. In your code, add a constant that indicates the latest schema version, say LATEST_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 new UpgradeDBVersion1To2() function that will perform the required changes.

That way, your program can be ported easily, can connect to and upgrade an old database, etc.

这篇关于添加列到的SQLite数据库IF NOT EXISTS - 柔性/空气源码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 08:10