0

我正在使用 flash Builder 4.6 和 Flex 4.6 开发移动 flex 应用程序,我在 bin-debug 文件夹中有一个 SQlite db 文件,我在应用程序启动时将其复制到 ActionScript 中的 applicationStorageDirectory。

这是设置它在应用程序初始化事件中调用的数据库的脚本:

        /* 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 db 进行这些更改时,它们会恢复,但会保留在包含表创建 SQL 的 xml 文件中。

4

0 回答 0