简体   繁体   English

将Flex连接到SQLite

[英]Connecting Flex to SQLite

In Flash builder, I'm struggling with basics of data retrieval from local database. 在Flash Builder中,我正在努力从本地数据库检索数据的基础知识。 Using Lita, I created a SQLite database with a single basic (item) table located in a "DAO" folder .It is meant to populate a List. 使用Lita,我创建了一个SQLite数据库,其中的“ DAO”文件夹中有一个基本(项目)表。该数据库用于填充列表。 and I have 2 problems: 我有两个问题:

  1. How to embed the database (with all its pre-populated data) without recreating it from scratch as shown in many tutorials ? 如何嵌入数据库(及其所有预先填充的数据),而不像许多教程中一样从头开始重新创建数据库?
  2. For the purpose of prototyping, how to link the data retrieved a single MXML file directly in the list without creating many other classes (ok, in this cases the number of required classes would be limited) such as : 为了进行原型制作,如何直接在列表中链接检索到的单个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>

For the question 1 you can add the database as an asset of the project, during the export release it will be embeded into the installer then if you want to place it into the localstore folder you can copy/move it from code... 对于问题1,您可以将数据库添加为项目的资产,在导出发行版中它将被嵌入到安装程序中,然后如果您要将其放置在localstore文件夹中,则可以从代码中复制/移动它...

For the number 2 对于数字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>

Thanks to Marcx and Marcos Placona's Blog entry on copying database locally I came up with this: 感谢Marcx和Marcos Placona在本地复制数据库上的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM