我正在 flex 4.5 中制作一个移动应用程序,我想获取人的姓名和年龄,然后保存到 sqlite 数据库完成我的代码如下所示:
public var personNamesDB:File;
public var dbConnection:SQLConnection;
protected function createDatabase(event:FlexEvent):void
{
// TODO Auto-generated method stub
personNamesDB = File.applicationStorageDirectory.resolvePath("person.db");
dbConnection = new SQLConnection();
dbConnection.openAsync(personNamesDB, SQLMode.CREATE);
dbConnection.addEventListener(SQLEvent.OPEN, onDatabaseOpened);
dbConnection.addEventListener(SQLEvent.CLOSE, onDatabaseClosed);
}// end createDatabase method
protected function onDatabaseOpened(arshayEvent:SQLEvent):void
{
trace("DB Opened");
var statement:SQLStatement = new SQLStatement();
statement.sqlConnection = dbConnection;
statement.text = "CREATE TABLE IF NOT EXISTS personinfo(id INTEGER PRIMARY KEY AUTOINCREMENT, nameofperson TEXT, ageofperson TEXT)";
statement.execute();
// for showing saved city names in list on App start up
showSavedNames();
trace("table created");
}
现在插入数据代码是:
///////////////////////////////////
public var insertData:SQLStatement
protected function saveName():void
{
// SQLite Usage
insertData = new SQLStatement();
insertData.sqlConnection = dbConnection;
insertData.text = "INSERT INTO personinfo(nameofcity, ageofperson) VALUES(:nameofcity, :ageofperson)";
insertData.parameters[":nameofcity"] =nameInput.text;
insertData.parameters[":ageofperson"] = ageInput.text;
insertData.addEventListener(SQLEvent.RESULT, dataInsertedSuccess);
trace("Name " + nameInput.text);
insertData.execute();
showSavedNames();
}
protected function dataInsertedSuccess(event:SQLEvent):void
{
trace(insertData.getResult().data);
}
//////////////////////////////////////////////
public var selectQuery:SQLStatement;
protected function showSavedNames():void
{
selectQuery = new SQLStatement();
selectQuery.sqlConnection = dbConnection;
selectQuery.text = "SELECT * FROM personinfo ORDER BY nameofperson ASC";
selectQuery.addEventListener(SQLEvent.RESULT, showNamesInList);
selectQuery.execute();
}
protected function showNamesInList(event:SQLEvent):void
{
listOfAddedNames.dataProvider = new ArrayCollection(selectQuery.getResult().data);
}
列表控件的 mxml 代码是:
<s:List id="listOfAddedNames" width="345" height="100%" labelField="nameofperson"
click="get_names_data(event)"/>
现在 get_names_data 方法是这样的:
public var selectNameQuery:SQLStatement;
protected function get_names_data(event:MouseEvent):void
{
// TODO Auto-generated method stub
var currentName:String = listOfAddedNames.selectedItem.nameofperson;
selectNameQuery = new SQLStatement();
selectNameQuery.sqlConnection =dbConnection;
selectNameQuery.text = "SELECT ageofperson FROM personinfo WHERE (nameofperson = '" + currentName + "')";
selectNameQuery.addEventListener(SQLEvent.RESULT, nowGotAge);
selectNameQuery.execute();
trace("current selected >> "+currentName);
}
protected function nowGotAge(event:SQLEvent):void
{
trace("Age of selected Name is >> "+selectNameQuery.getResult().data.ageofperson);
}
在这条线上:
trace("Age of selected Name is >> "+selectNameQuery.getResult().data.ageofperson);
没有从数据库中获取数据并且 trce 显示“未定义”请为我解决这个问题并告诉我如何根据人名列表中的选定名称从 ageofperson 列中获取数据——提前致谢