Domanda

About me:
I am just starting out. I know there has to be a simple way to do this, but after a week I am still stumped.
What it is:
I have made an app to replace a very long written procedure. I would like to have text input boxes populated with the previous given answer for the test they are running.
Problem:
I can get the correct info from the DB; however I cannot put it in the text input field. The box just displays: [object Object]

Any help would be greatly appreciated.

Here is sample code for the Settings.mxml page:

<?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="Settings" >
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <s:Label id="DB_Name_Label" left="10" top="10" width="150" height="50" fontSize="28"
             text="DB Name" textAlign="right" verticalAlign="middle"/>
    <s:TextInput id="DB_Name_Input" left="170" top="10" width="300" height="50" fontSize="32"/>

    <s:Label id="Test_Title" left="10" top="70" width="150" height="50" fontSize="28"
             text="Test Title" textAlign="right" verticalAlign="middle"/>
    <s:TextInput id="Test_Title_Input" left="170" top="70" width="300" height="50" fontSize="32"/>

    <s:Button id="Equipment_Button" left="10" right="10" top="130" height="50"
              label="Save, then next Page" click="EquipmentButtonClick()"/>

    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import spark.events.ViewNavigatorEvent;     

            protected var sqlConnection:SQLConnection;

            public function add_info_to_DB(tableName:String, valueToBeSaved:String):void
            {
                sqlConnection = new SQLConnection();
                sqlConnection.open(File.userDirectory.resolvePath(DB_Name_Input.text+".db"));
                var add2db:SQLStatement = new SQLStatement();
                add2db.sqlConnection = sqlConnection;
                add2db.text = ("CREATE TABLE IF NOT EXISTS "+tableName+" (id INTEGER PRIMARY KEY AUTOINCREMENT, answer TEXT)");
                add2db.execute();

                add2db.text = "INSERT INTO "+tableName+" (answer) VALUES (?)";
                add2db.parameters[0] = valueToBeSaved;
                add2db.execute();
            }

            [bindable] public var testdata:Object= new Object();

            protected function EquipmentButtonClick():void
            {   
                add_info_to_DB("Test_Title",Test_Title_Input.text);         
                navigator.pushView(views.ReadfromDB, testdata);
            }
        ]]>
    </fx:Script>
</s:View>

Here is code for the ReadfromDB.mxml page:

<?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="Reading" >
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <s:Label id="DB_to_Read_Label" left="10" top="10" width="150" height="50" fontSize="28"
             text="DB to Read" textAlign="right" verticalAlign="middle"/>
    <s:TextInput id="DB_to_Read" left="170" top="10" width="300" height="50" fontSize="32"/>

    <s:Label id="Test_Title" left="10" top="70" width="150" height="50" fontSize="28"
             text="Test Title" textAlign="right" verticalAlign="middle"/>
    <s:TextInput id="Test_Title_Input" left="170" top="70" width="300" height="50" fontSize="32"/>

    <s:Button id="Populate_Button" left="10" right="10" top="130" height="50" label="Populate"
              click="PopulateButtonClick()"/>

    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import spark.events.ViewNavigatorEvent;     

            protected var sqlConnection:SQLConnection;
            public var valueToBeRead:String;

            public function get_DB_info(tableName:String):void
            {   
                sqlConnection = new SQLConnection();
                sqlConnection.open(File.userDirectory.resolvePath(DB_to_Read.text+".db"));
                var add2db:SQLStatement = new SQLStatement();
                add2db.sqlConnection = sqlConnection;
                add2db.text = ("SELECT answer FROM "+tableName+" ORDER BY id DESC LIMIT 1");
                add2db.execute();
                valueToBeRead = add2db.getResult().data.toString();//.valueOf();//.toString();
            }

            protected function PopulateButtonClick():void
            {   
                get_DB_info("Test_Title");  
                Test_Title_Input.text = valueToBeRead;
            }
        ]]>
    </fx:Script>
</s:View>
È stato utile?

Soluzione

When you call add2db.getResult() the return object is a SQLResult. The info there about the data property is very useful. It says that the data property is an Array of Objects containing your result.

In your case, it looks like your SQL query will return exactly 1 database row, and that row contains only one column: answer

So you should therefore be able to populate the text input like this:

var firstRow:Object = add2db.getResult().data[0];
valueToBeRead = firstRow["answer"];
// or, instead of storing it in a variable, just assign it to the text input right away
Test_Title_Input.text = firstRow["answer"];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top