关于我:
我刚开始。我知道必须有一个简单的方法来做到这一点,但一周后我仍然难倒
它是什么:
我已经制作了一个应用来取代一个很长的书面程序。我希望文本输入框填充前面给出的答案,用于他们正在运行的测试
问题:
我可以从DB获得正确的信息;但是我不能把它放在文本输入字段中。
该框只显示:[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>
答案 0 :(得分: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"];