5 Replies Latest reply on May 26, 2009 3:43 PM by onditz

    Read database and update some labels all the time

    onditz

      Hello!

       

      I'm doing a program that can read data from a database and update some labels. I've been trying to set a timer to read this data but it frozes the screen. I've seen some examples like this one: http://www.cflex.net/showfiledetails.cfm?ChannelID=1&Object=File&objectID=560 but I'm not trying to update a datagrid.

       

      I was trying to do it in this way:

       

      ...
      <mx:HTTPService id="DBService" url="http://localhost/Test2/Test2.php" resultFormat="e4x"  method="post">
       </mx:HTTPService>
      
      <mx:Label x="29" y="18" text="Name:" fontSize="16"/>
      <mx:Label  id="userName" x="96" y="23" text="{DBService.lastResult.row.getItemAt(1).@user_name}" width="244"/>
      <mx:Label x="29" y="50" text="Pass:" fontSize="16"/>
      <mx:Label  id="userPass" x="96" y="55" text="{DBService.lastResult.row.getItemAt(1).@user_pass}" width="232"/>
      ...
      
      

      What I don't know is how to ask the database all the time. Like "while(true)" (I know that Flex doesn't allow multithreads).

       

      Can anyone help me, please?

        • 1. Re: Read database and update some labels all the time
          Michael Borbor Level 4

          Sorry I misunderstood your question, have you considered using BlazeDS?

          • 2. Re: Read database and update some labels all the time
            onditz Level 1

            If I want to use BlazeDS I have to create a J2EE server and use it for polling the MySQL database I have. Am I right?

             

            Is any trivial way of doing this? I mean is there any "Updating values in Flex from MySQL for Dummies"?

             

            This is my first project in Flex and it's becoming a little nightmare... I can program in J2SE but I know nothing about J2EE.

             

            At the moment I have a php server that sends the data from the database to my flex application, but I cannot update the values as they change in the database.

            • 3. Re: Read database and update some labels all the time
              Michael Borbor Level 4

              Could you post your whole app, or attach it so we can see what is causing the lock up?

              • 4. Re: Read database and update some labels all the time
                onditz Level 1

                Here is the code for the mxml part:

                 

                <?xml version="1.0" encoding="utf-8"?>
                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"   creationComplete="submitQuery('SELECT id,user_name,user_pass FROM users')"  width="1148" height="1059" xmlns:Updater="Updater.*">
                       <mx:Script source="Test2Script.as"/>
                            
                       <mx:HTTPService id="DBService" url="http://localhost/Test2/Test2.php" resultFormat="e4x"  method="post">
                                 
                       </mx:HTTPService>
                       
                       <mx:DataGrid left="0" right="0" top="109" bottom="762" dataProvider="{DBService.lastResult.row}">
                              <mx:columns>
                                     <mx:DataGridColumn headerText="ID" dataField="@id"/>
                                     <mx:DataGridColumn headerText="user_name" dataField="@user_name"/>
                                     <mx:DataGridColumn headerText="user_pass" dataField="@user_pass"/>
                              </mx:columns>
                       </mx:DataGrid>
                       
                       <mx:Label  id="userName" x="96" y="23" text="{DBService.lastResult.row[1].@user_name}" width="244"/>
                       <mx:Label x="29" y="18" text="Name:" fontSize="16"/>
                       <mx:Label x="29" y="50" text="Pass:" fontSize="16"/>
                       <mx:Label x="96" y="55" text="{DBService.lastResult.row[1].@user_pass}" width="232"/>
                       <mx:Binding 
                        source="DBService.lastResult.users.user.getItemAt(1).@user_name" 
                        destination="userName.text"/>
                       
                       <mx:CheckBox id="testCheckBox" x="213" y="330" label="Timer" visible="true" enabled="true" selected="true"/>
                       
                       <mx:Button id="userPass" x="62" y="330" label="UPDATE" enabled="true">
                              <mx:click>check()</mx:click>
                       </mx:Button>
                       
                </mx:Application>
                

                 

                and here the ActionScript:

                 

                public function submitQuery(queryString:String):void {
                    DBService.send( { query: queryString } );
                }
                  
                public function check():void {
                       submitQuery("SELECT id,user_name,user_pass FROM users");
                }
                

                As you can see I can update the label when I click the button. But I would like it to happen automatically every second.

                 

                I tried to implement a timer and use a green thread library but they didn't work. (I don't have this code).

                • 5. Re: Read database and update some labels all the time
                  onditz Level 1

                  I finxed the problem seting the timer right. I had errors on my code that I couldn't see.

                   

                  Thank you for your help. n_n