6 Replies Latest reply on Sep 30, 2009 1:32 PM by erthy

    How to connect to and communicate with an SQLite database in AIR/Flex

    erthy Level 1

      Hey guys,

       

      I recently decided I would try programming a vocabulary-training program in AIR, so I could use it on Linux as well. I got stuck pretty soon. I am trying to connect to a local SQLite database and I obviously fail epically. Posting the source code of the application here:

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> 
         <mx:Script> 
            <![CDATA[   
      
                 import flash.filesystem.File;
                import flash.data.*;
               import mx.controls.Alert;  
      
                        private var dbFile:File;
                     private var conn:SQLConnection;
      
      
                     // -- AUTO INIT FUNCTIONS --------------------------------------------------- /
      
                     private function init():void {
      
                          // Create a File Reference to the Included DB
                          dbFile = File.applicationDirectory.resolvePath( "../slovniky.db" );
      
                          // Create SQL Connection
                          conn = new SQLConnection();
      
                          // Event Listener that will tell us when the DB is opened
                          conn.addEventListener(SQLEvent.OPEN, openSuccess);
                          // Event Listener that will tell us if an error occurs
                          conn.addEventListener(SQLErrorEvent.ERROR, openFailure);
      
      
      
                     }
      
                     // -- EVENT HANDLERS -------------------------------------------------------- /
      
      
      
      
               private function starter() :void {
                    slovickoEn.enabled = true;
                    nazor.enabled = true;
                    odeslat.enabled = true;
                    zrusit.enabled = true;
                    start.enabled = false;
                    // Otevírám spojení s databází v asynchroním módu
                     conn.openAsync( dbFile );
               }
      
               private function openSuccess( event:SQLEvent ):void {
                          // Pokud se spojení povede!
                          Alert.show("Spojení se zdařilo!");
                     }
      
                 private function openFailure( event:SQLEvent ):void {
                          // Pokud spojení selže!
                          Alert.show("Spojení se nezdařilo!")
                     }
      
               private function kontrola() : void { 
                    if (nazor.text != "") {
      
                         if (nazor.text == "Pes") {
                         Alert.show("Správně!");     
                         }
      
                         else {
                         Alert.show("Špatně!! " + "Napsané slovíčko bylo " + nazor.text);      
                         }
                    }
               }
      
      
            ]]>    
         </mx:Script>
         <mx:VBox width="400" height="200" 
                     horizontalCenter="0" verticalCenter="0" backgroundColor="#FFFFFF"
                     paddingBottom="20" paddingLeft="20" paddingRight="20" paddingTop="20" 
         > 
            <mx:HBox width="100%" verticalAlign="middle" horizontalAlign="center"> 
               <mx:Label text="Anglicky:"/> 
               <mx:TextInput id="slovickoEn" editable="false" text="Dog" enabled="false"/> 
            </mx:HBox>
            <mx:HBox width="100%" verticalAlign="middle" horizontalAlign="center"> 
               <mx:Label text="Česky:"/> 
               <mx:TextInput id="nazor"  enter="kontrola()" enabled="false"/> 
            </mx:HBox> 
            <mx:HBox width="100%" horizontalAlign="center"> 
               <mx:Spacer width="50" height="10"/> 
               <mx:Button id="odeslat" label="Odeslat" color="#0D8401" click="kontrola()" enabled="false"/> 
               <mx:Button id="zrusit" label="Zrušit" color="#0D8401" enabled="false"/> 
            </mx:HBox>
            <mx:HBox width="100%" horizontalAlign="center"> 
               <mx:Spacer width="50" height="10"/> 
               <mx:Button id="start" label="Start" color="#0D8401" click="starter()" enabled="true"/> 
            </mx:HBox>
      
         </mx:VBox> 
      </mx:WindowedApplication>
      
      
      

      The Run of the program tells me this:

       

      TypeError: Error #1009: Cannot access a property or method of a null object reference.

           at pes/starter()[C:\air\projects\pes\src\pes.mxml:45]

           at pes/__start_click()[C:\air\projects\pes\src\pes.mxml:93]

       

      I would appreciate any help or a how-to. I am an absolute beginner with some experience from HTML, CSS, PHP and rarely JS. What am I doing wrong? File attached for easier manipulation as well.

       

      Erthy