Flex移动开发装载预定义sqlite数据库方法
发布时间:2020-12-15 01:00:16 所属栏目:百科 来源:网络整理
导读:Flex4.5中的数据库驱动包中可以实现对sqlite数据库的创建及一系列的操作。在很多的项目需求中,已经有现成的数据库,在程序运行初始化的时候需要把这个现成的数据库导入到数据存储目录中,而不需求在程序运行的时候去动态创建一个数据库。需求实现的思路是将
Flex4.5中的数据库驱动包中可以实现对sqlite数据库的创建及一系列的操作。在很多的项目需求中,已经有现成的数据库,在程序运行初始化的时候需要把这个现成的数据库导入到数据存储目录中,而不需求在程序运行的时候去动态创建一个数据库。 需求实现的思路是将现成的数据库预先复制到系统的document文件夹下,文件路径可以用File.documentsDirectory.resolvePath(**.db)方法来获得这个目录下的源数据库。这里以Android系统为例,得到的文件路径为/mnt/sdcard。然后在系统init初始化后,执行复制,将这个源数据库复制到程序存储目录之下。获取路径为:File.applicationStorageDirectory.resolvePath(*.db);得到路径后,执行file的copyto()方法即可。 代码如下,版本FB4.6 <?xml version="1.0" encoding="utf-8"?> <s:View xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" title="主页视图" creationComplete="init()"> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <fx:Script> <![CDATA[ import flash.data.SQLConnection; import mx.events.CloseEvent; import spark.components.ViewNavigatorApplication; import spark.components.supportClasses.ViewNavigatorAction; import spark.events.ViewNavigatorEvent; private function init():void{ var dbFile:File=File.documentsDirectory.resolvePath("neilDB.db");//源数据库路径 var workFile:File=File.applicationStorageDirectory.resolvePath("database/neilDB.db");//目录位置 if(dbFile.exists){ if(!workFile.exists){ dbFile.copyTo(workFile); message2.text=dbFile.nativePath;//处理没有复制源及目录位置已经存在同名数据库的情况 } } else if(!workFile.exists){ message2.text="不存在复制源!"; return; } conDB(); } /* 连接gc */ private function conDB():void{ var file:File = File.applicationStorageDirectory.resolvePath("database/neilDB.db"); var conn:SQLConnection=new SQLConnection(); conn.addEventListener(SQLEvent.OPEN,openHandler); conn.addEventListener(SQLErrorEvent.ERROR,errorHaneler); /* var folder:File=File.applicationStorageDirectory; var dbFile:File=folder.resolvePath("DBSample.db"); */ var stmt:SQLStatement=new SQLStatement(); stmt.sqlConnection=conn; conn.open(file); //conn.openAsync(file); message.text=file.nativePath; } private function openHandler(event:SQLEvent):void{ trace("the database was create sussessfully"); } private function errorHaneler(event:SQLErrorEvent):void{ trace("error message:",event.error.message); trace("Detials:",event.error.details); message.text="error"; } private function goout():void{ var exitingEvent:Event = new Event(Event.EXITING,false,true); NativeApplication.nativeApplication.dispatchEvent(exitingEvent); if (!exitingEvent.isDefaultPrevented()) { NativeApplication.nativeApplication.exit(); } } ]]> </fx:Script> <s:Label id="message"/> <s:Label id="message2" y="50"/> <s:Button label="结束应用程序" y="80" click="goout()"/> </s:View> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |