4 Replies Latest reply on Aug 13, 2011 5:36 PM by Kristin95762

    sqlite, moving sqlstatements to a custom class

    Kristin95762 Level 1

      I'm working on a FB AIR app.

       

      I have a main.mxml file with a source to a main.as file.

       

      My main.as file is getting very bloated with sqlstatements. I want to move these to a custom class, but I don't know how to proceed.

       

      I found an example of a package on this page: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/data/SQLConnectio n.html#begin()

      about a 3rd of the way down.

       

      The example above is exactly what I want to do, but I don't know how to adapt this for use in my main.as file beyond importing the class and instantiating it. How do I assign my values to the class file? How do I use the class to load up the dataProvider, etc...

       

      Also, in the example above the class extends Sprite. This sounds like it's meant for a Flash application. Would I still extend Sprite in Flex 4? I have done a couple of simple events, passing data back to the main app with the Flash.Events.Event, so the use of Sprite causes some confusion.

       

      In a previous post, Amy suggested using Robotlegs. I think I need to get a handle on using Flex before I explore other architectures. I'm just not that sophisticated yet.

       

      Thanks

       

      Kristin

        • 1. Re: sqlite, moving sqlstatements to a custom class
          kokorito Level 4

          no, no need to extend sprite

           

          I would use a singleton class (extended from eventdispatcher if you need to dispatch events)

          • 2. Re: sqlite, moving sqlstatements to a custom class
            Kristin95762 Level 1

            hmmmm....Thank you for your reply.

             

            I think I need a little more hand holding though.

             

            I was looking for a simple class and a method for using it in the main.mxml. Singletons and other design patterns are quite foreign to me at the moment.

             

            Of course, I'm looking at the example Adobe had in their Actionscript 3 reference material and I'm assuming I could apply that method as it stands. However, it's missing the information on how to use it in a practical application. Following the link about how to run class files doesn't help because the main.mxml file is not available and I don't know how to hook it up myself.

             

            Thanks again.

             

            Kristin

             

            PS: I'm just a hobbyist. Programming is something I've dabbled with on and off. So whatever I've come up with is pretty well mish-mashed together from stuff I've read on the net. I'm surprised it works at all honestly.

             

            Adobe's example: flash.data.SQLConnection - ActionScript® 3.0 Reference for the Adobe® Flash® Platform

            • 3. Re: sqlite, moving sqlstatements to a custom class
              kokorito Level 4

              ok, a quick example

              make a new package called utilities and then a new actionscript class called DatabaseManager

               

              package utilities
              {
                   import flash.data.SQLConnection;
                   import flash.data.SQLResult;
                   import flash.data.SQLStatement;
                   import flash.filesystem.File;
                   
                   import mx.collections.ArrayCollection;
                   
                   public class DatabaseManager
                   {
                        private var sqlConnection:SQLConnection;
                        private var stm:SQLStatement;
                        
                        public function DatabaseManager()
                        {
                             
                             //connect to database
                             sqlConnection = new SQLConnection();
                             sqlConnection.open(File.applicationStorageDirectory.resolvePath("whatever"));
                             
                             //create tables if not exist
                             stm = new SQLStatement();
                             stm.sqlConnection = sqlConnection;
                             stm.text = "create your table here";
                             stm.execute();
                             
                             //create more tables
                             //...
                        }
                        
                        public function getThings():ArrayCollection
                        {
                             stm = new SQLStatement();
                             stm.sqlConnection = sqlConnection;
                             stm.text = "your query";
                             //...
                        }
                        
                        public function addStuff(stuff:Object):int
                        {
                             //...
                        }
                   }
              }

               

               

              then in your main file you need to import

               

              import utilities.DatabaseManager;

               

              instantiate the class, this connects and creates tables

               

              private var myDB:DatabaseManager = new DatabaseManager(); 

               

               

              then to use it

               

              var listFromDatabase:ArrayCollection = myDB.getThings();

               

              insertid = myDB.addStuff(thingToAdd); 

               

               

              hope that gives you an idea 

              • 4. Re: sqlite, moving sqlstatements to a custom class
                Kristin95762 Level 1

                Hey, that's the ticket.

                 

                Great post. Thank you very much. This should help me clean up my main.as quite a bit. It's a jungle in there.

                 

                Course I think I bit off more than I can chew sometimes. I've got more going than just a simple CRUD db. I keep thinking of things I want it to do. But this should get me going in the right direction.