2012-05-01 113 views
0

在Flash构建器中,我正努力从本地数据库中检索数据。使用Lita,我创建了一个包含位于“DAO”文件夹中的单个基本(项目)表的SQLite数据库。它旨在填充List。我有两个问题:将Flex连接到SQLite

  1. 如何嵌入数据库(及其所有预填充的数据),而无需从头开始重新创建它,如许多教程中所示?
  2. 对于原型的目的,如何链接数据直接在列表中检索一个MXML文件而不产生许多其他类(确定,在此情况下,所需的类的数量将是有限的),例如:
<?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="HomeView" > 

<fx:Script> 

    <![CDATA[ 

     import flash.data.SQLConnection 
     import flash.data.SQLStatement; 
     import flash.filesystem.File; 
     import flash.filesystem.FileMode; 
     import mx.collections.ArrayCollection;` 

     private function get myData():ArrayCollection 
     { 
      var stmt:SQLStatement = new SQLStatement(); 
      stmt.sqlConnection = new SQLConnection(); 

      stmt.sqlConnection.open(File.applicationStorageDirectory.resolvePath("dao/MyDatabase.db")); 
      stmt.text = "SELECT id, name FROM Item"; 

      stmt.execute(); 
      var result:Array = stmt.getResult().data; 

      if (result) 
      { 
       var list:ArrayCollection = new ArrayCollection(); 
       list.source(result); 
       return list; 
      } else { 
       return null; 
      } 
     } 
     ]]> 
    </fx:Script> 

    <s:List id="list" top="0" bottom="0" left="0" right="0" 
      dataProvider="{myData}" > 
    <s:itemRenderer> 
    <fx:Component> 
    <s:IconItemRenderer label="{myData.name}"> 
    </s:IconItemRenderer> 
    </fx:Component> 
    </s:itemRenderer> 
    </s:List> 
    </s:View> 
+0

我不能辨别NR 2个问题 – RIAstar

回答

0

感谢Marcx和复制数据库Marcos Placona's Blog进入本地,我想出了这一点:

<?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="HomeView" > 

<fx:Script> 

    <![CDATA[ 

     import flash.data.SQLConnection 
     import flash.data.SQLStatement; 
     import flash.filesystem.File; 
     import flash.filesystem.FileMode; 
     import mx.collections.ArrayCollection; 

     private function get myData():ArrayCollection 
     { 
      var myData:String = "dao/MyDatabase.db"; 
      var embededSessionDB:File = File.applicationDirectory.resolvePath(myData); 
      var writeSessionDB:File = File.applicationStorageDirectory.resolvePath(myData); 
      // If a writable DB doesn't exist, we then copy it into the app folder so it's writteable 
      if (!writeSessionDB.exists) 
      { 
      embededSessionDB.copyTo(writeSessionDB); 
      } 

      var stmt:SQLStatement = new SQLStatement(); 
      stmt.sqlConnection = new SQLConnection(); 

      stmt.sqlConnection.open(File.applicationStorageDirectory.resolvePath(myData)); 
      stmt.text = "SELECT id, name FROM Item"; 

      stmt.execute(); 
      var result:Array = stmt.getResult().data; 

      stmt.execute(); 
      var result:Array = stmt.getResult().data; 
      var r:ArrayCollection = new ArrayCollection(); 

      if (result) 
      {  
       r.source = result; 
       return r; 
      }else{ 
       return null; 
      } 
     } 

     [Bindable]private var resultArr:ArrayCollection = getData(); 

     ]]> 

    </fx:Script> 

    <s:List id="list" top="0" bottom="0" left="0" right="0" 
      dataProvider="{myData}" label="name"> 
    </s:List> 
    </s:View> 
0

对于问题1,如果你想将它放入您可以复制localstore文件夹,你可以出口释放将被内嵌到安装程序,然后在添加数据库作为项目的资产,/移动它来自代码...

对于数字2

import flash.data.SQLConnection 
    import flash.data.SQLStatement; 
    import flash.filesystem.File; 
    import flash.filesystem.FileMode; 
    import mx.collections.ArrayCollection;` 

    [Bindable]private var resultArr:ArrayCollection = new ArrayCollection(); 

    private function getData():ArrayCollection 
    { 
     var stmt:SQLStatement = new SQLStatement(); 
     stmt.sqlConnection = new SQLConnection(); 

     stmt.sqlConnection.open(File.applicationStorageDirectory.resolvePath("dao/MyDatabase.db")); 
     stmt.text = "SELECT id, name FROM Item"; 

     stmt.execute(); 
     var result:Array = stmt.getResult().data; 

     resultArr = new ArrayCollection(); 
     if (result) 
     {  
      resultArr.source = result;   
     } 
    } 
    ]]> 
</fx:Script> 

<s:List id="list" top="0" bottom="0" left="0" right="0" 
     dataProvider="{resultArr}" labelField="name" > 
</s:List>