sexta-feira, 4 de novembro de 2011

Sqlite FLex Builder para IOS e Android

Exemplo simples para utilizar banco de dados.
=D


<?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="Database Access" viewActivate="view1_viewActivateHandler(event)">
    
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import spark.events.ViewNavigatorEvent;     
      
            protected var sqlConnection:SQLConnection;
            
            protected function view1_viewActivateHandler(event:ViewNavigatorEvent):void
            {
                sqlConnection = new SQLConnection();
                sqlConnection.open(File.applicationStorageDirectory.resolvePath("giberish.db"));
                var stmt:SQLStatement = new SQLStatement();
                stmt.sqlConnection = sqlConnection;
                stmt.text = "CREATE TABLE IF NOT EXISTS giberish (label TEXT)";
                stmt.execute();
                getAllGiberish();
            }
            
            protected function getAllGiberish():void
            {
                var stmt:SQLStatement = new SQLStatement();
                stmt.sqlConnection = sqlConnection;
                stmt.text = "SELECT label FROM giberish";
                stmt.execute();
                l.dataProvider = new ArrayCollection(stmt.getResult().data);
            }
            
            protected function onAdd():void
            {
                var stmt:SQLStatement = new SQLStatement();
                stmt.sqlConnection = sqlConnection;
                stmt.text = "INSERT into giberish values(:giberish)";
                stmt.parameters[":giberish"] = g.text;
                stmt.execute();
                getAllGiberish();
                g.text = "";
            }
        ]]>
        
    </fx:Script>
    
    <s:layout>
        <s:VerticalLayout paddingTop="5" paddingBottom="5" paddingLeft="5" paddingRight="5" gap="5" 
                          horizontalAlign="left" verticalAlign="top"/>
    </s:layout>
    
    <s:Form width="100%">
        <s:FormItem label="Enter text" skinClass="spark.skins.spark.StackedFormItemSkin">
            <s:TextInput id="g"/>
        </s:FormItem>
        <s:Button label="Save text" enabled="{g.text.length != 0}" click="onAdd()"/>
    </s:Form>
    
    <s:List id="l" width="100%" height="100%"/>
    
</s:View>



Vitor Yudi Hansen