2 Replies Latest reply: Sep 20, 2013 2:01 PM by pao1011 RSS

    sqlite db query to  text fields question

    pao1011

      I have a search query which I can send the data to a list but how do I send the data to text fields?
      for example I search in a myTable with 4 columns: id, item1, item2, item3, this is the code:I

       

      private function cSearch():void {
          stmt.sqlConnection = this.isDbConnected(sqlConn);
          var sql:String  = "SELECT * FROM myTable WHERE (myTable.item1) LIKE '%' ||@item1|| '%' ";
          stmt.parameters["@item1"] = searchInput.text;
          stmt.text = sql;
          stmt.addEventListener(SQLErrorEvent.ERROR, sqlError);
          stmt.addEventListener(SQLEvent.RESULT, selectResult);
          stmt.execute();
          }

       

      if I want to display the data to a list I will use this code:

       

      private function selectResult(res:SQLEvent):void {
          var data:Array = stmt.getResult().data;
          cList = new ArrayCollection();
          if (data){
              for (var d:int=0;d<=data.length-1;d++) {
                 
                  cList.addItem({id:data[d].id,item1:data[d].item1,item2:data[d].item2t,item3:data[d].item3 });
              }
             
          }else {
              trace("no available records");
          }
         
      }
      private function displayLData(item:Object):String {
          return item.id + ".  \t" + item.item1+ "   \t" + item.item2 + "   \t" + item.item3;
      }

       

      and the list will be:

       

      <s:List id="myList"  width="100%" height="100%" dataProvider="{cList}" labelFunction="displayLData" > </s:list>

       

      so it works, data is display

       

      But what about if I want to send the data to a 4 diferent text fields?

       

      I tried this but it doesn't work

       

      private function selectResult(event:SQLEvent):void {
          var searchRes:SQLResult = stmt.getResult();
          cList = new ArrayCollection(searchRes.data);
              if (searchRes.data != null) {
                 idSt = searchRes.data.id;
                  item1St = searchRes.data.item1;
                  item2St = searchRes.data.item2;
                  item3St = searchRes.data.item3;
              }
          }

       

      any help ?

        • 1. Re: sqlite db query to  text fields question
          archemedia Community Member

          you can use the same code as your first example (list) but instead of pushing the data to the arrayCollection, put the results in the appropriate text fields.

          For this to work, you have to access the textInput objects

           

          - Suppose your textInput are grouped in a group with id 'tfGroup's

          - suppose your textInput id's are tf0, tf1, etc...s

           

          for (var d:int=0;d<=data.length-1;d++) {

                     

                      var tf:TextInput = tfGroup["tf" + d];

                    tf.text = whateverYouWantToPutThere;

                      //cList.addItem({id:data[d].id,item1:data[d].item1,item2:data[d].item2t ,item3:data[d].item3});

                  }

           

          I haven't test this but it should work.

           

          Another solution might be to bind your textInput text properties to an item in your cList collection.

           

          <TextInput id="tf0" text = {cList.getItemAt(0)}/>

          this way you can still populate the cList in your sql function.

          Be sure to declare cList as [Bindable] thought

           

          hope this helps

           

          Dany

          • 2. Re: sqlite db query to  text fields question
            pao1011

            I try it this way and it works fine:

             

            private function selectResult(event:SQLEvent):void {

                            var searchRes:SQLResult = stmt.getResult();

                            cList = new ArrayCollection(searchRes.data);

                            if (searchRes.data != null) {

                                cidSt = cSList.getItemAt(0).id;

                                item1St = cSList.getItemAt(0).item1;

                                item2St =  cSList.getItemAt(0).item2;

                                item3St =  cSList.getItemAt(0).item3;

            }

            }

            the cList, cidSt, item1St, item2St, item3St are declared [bindable]

            and I use labels so I bind them <s:label id="myLbl1" text="{cidSt}" /> etc. also can be done  directly to the label:

            if (searchRes.data != null) {

                                myLbl1.text  = cSList.getItemAt(0).id;

                                myLbl2.text = cSList.getItemAt(0).item1;

                                myLbl3.text =  cSList.getItemAt(0).item2;

                                myLbl4.text =  cSList.getItemAt(0).item3;

            }

             

            anyawys thanks for you response it help a lot.