0

关于我:
我才刚刚开始。我知道必须有一个简单的方法来做到这一点,但一周后我仍然很难过。
它是什么:
我制作了一个应用程序来替换很长的书面程序。我想为他们正在运行的测试填充先前给出的答案的文本输入框。
问题:
我可以从数据库中获取正确的信息;但是我不能把它放在文本输入字段中。该框仅显示:[object Object]

任何帮助将不胜感激。

以下是 Settings.mxml 页面的示例代码:

<?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>

这是 ReadfromDB.mxml 页面的代码:

<?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>
4

1 回答 1

1

当你调用add2db.getResult()返回对象是一个SQLResult. 那里有关数据属性的信息非常有用。它说 data 属性是Array包含Objects您的结果的。

在您的情况下,看起来您的 SQL 查询将准确返回 1 个数据库行,并且该行仅包含一列:answer

因此,您应该能够像这样填充文本输入:

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"];
于 2012-04-22T06:10:26.727 回答