将Flex连接到SQLite [英] Connecting Flex to SQLite

查看:239
本文介绍了将Flex连接到SQLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Flash Builder中,我正在努力从本地数据库中检索数据。使用Lita,我创建了一个SQLite数据库,其中一个基本(项目)表位于DAO文件夹中。它意味着填充一个List。我有两个问题:
$ b $ ol
  • 如何嵌入数据库(及其所有预先填充的数据),而不用重新创建它很多教程?
  • 为了原型的目的,如何将数据直接链接到列表中,而不用创建许多其他类(在这种情况下,需要的类的数量将被限制)如:





     < xml version =1.0encoding =utf-8?> 
    < s:查看xmlns:fx =http://ns.adobe.com/mxml/2009
    xmlns:s =library://ns.adobe.com/flex/spark
    title =HomeView>

    < fx:Script>
    $ b<![CDATA [

    import flash.data.SQLConnection
    import flash.data.SQLStatement;
    导入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 =选择ID,名称从项目;

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

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

    dataProvider ={myData}> ;
    < s:itemRenderer>
    < fx:Component>
    < s:IconItemRenderer label ={myData.name}>
    < / s:IconItemRenderer>
    < / fx:Component>
    < / s:itemRenderer>
    < / s:List>
    < / s:视图>


    解决方案

    感谢Marcx和 Marcos Placona's Blog 关于复制本地数据库的问题我来了与此:



     <?xml version =1.0encoding =utf-8 >?; 
    < s:查看xmlns:fx =http://ns.adobe.com/mxml/2009
    xmlns:s =library://ns.adobe.com/flex/spark
    title =HomeView>

    < fx:Script>
    $ b<![CDATA [

    import flash.data.SQLConnection
    import flash.data.SQLStatement;
    导入flash.filesystem.File;
    import flash.filesystem.FileMode;
    导入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(!writeSessionDB.exists)
    {
    embededSessionDB.copyTo writeSessionDB);
    }

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

    stmt.sqlConnection.open(File.applicationStorageDirectory.resolvePath(myData));
    stmt.text =选择ID,名称从项目;

    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>

    dataProvider ={myData}标签= 名称 >
    < / s:List>
    < / s:视图>


    In Flash builder, I'm struggling with basics of data retrieval from local database. 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. 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 :

    <?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>
    

    解决方案

    Thanks to Marcx and Marcos Placona's Blog entry on copying database locally I came up with this:

    <?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>
    

    这篇关于将Flex连接到SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆