我正在使用Flash Builder 4.6和Flex 4.6处理移动Flex应用程序,我在bin-debug文件夹中有一个SQlite db文件,我在应用程序启动时将其复制到ActionScript中的applicationStorageDirectory。
这是设置在app initialize event上调用的数据库的脚本:
/* Variables*/
private var file:File = File.applicationDirectory.resolvePath("db/myHealthBuddy.db");
private var local:File = File.applicationStorageDirectory.resolvePath("db/myHealthBuddy.db");
private var conn:SQLConnection;
private var model:Model = new Model();
/* db */
protected function dbInit():void
{
if(!local.exists)
{
local.createDirectory();
file.copyTo(local,true);
}
conn = new SQLConnection();
conn.addEventListener(SQLEvent.OPEN, openHandler);
conn.addEventListener(SQLErrorEvent.ERROR, errorHandler);
conn.openAsync(local);
}
private function openHandler(event:SQLEvent):void
{
trace("\ndb open");
conn.removeEventListener(SQLEvent.OPEN, openHandler);
// saving connection to Model valueObject
model.connection = conn;
// create tables if not already exists
var createTablesFile:File = File.applicationDirectory.resolvePath("db" + File.separator + "createtables.xml");
var stream:FileStream = new FileStream();
stream.open(createTablesFile, FileMode.READ);
var xml:XML = XML(stream.readUTFBytes(stream.bytesAvailable));
stream.close();
conn.begin(SQLTransactionLockType.IMMEDIATE);
for each (var statement:XML in xml.statement)
{
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = conn;
stmt.text = statement;
stmt.execute();
}
conn.commit();
navigator.firstViewData = model;
}
此代码是否正常。
我需要对数据库进行更改,例如(添加新表并向现有表添加列)。当我对sqlite数据库进行这些更改时,它们会恢复,但是在包含表创建SQL的xml文件中预先存在。