无法从flex中的sqlite数据库获取数据

无法从flex中的sqlite数据库获取数据,sqlite,flex4.5,flex-mobile,Sqlite,Flex4.5,Flex Mobile,我正在flex 4.5中制作一个移动应用程序,我想获得人名和年龄,然后保存到sqlite数据库中。我的代码如下所示: public var personNamesDB:File; public var dbConnection:SQLConnection; protected function createDatabase(event:FlexEvent):void { // TODO Auto-generated met

我正在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)"/>
在这一行:

trace("Age of selected Name is >> "+selectNameQuery.getResult().data.ageofperson);

没有从数据库中提取数据,trce显示“undefined”(未定义),请帮我解决这个问题,并告诉我如何根据人名列表中选定的姓名从ageofperson列中获取数据——提前感谢您,因为您打开了sqlite数据库异步模式,所以首先需要了解异步执行模型。 列名“nameofcity”的第二个问题是person表没有您声明的任何列。因此,我在这里修改为saveName()中的“nameofperson”

在调用saveName()的位置,请确保已调用“saveName()”

在sqlquery中的dataInsertedSuccess()中,仅当重新运行INSERT/UPDATE sql查询ie时返回受影响行的编号,仅返回整数值。因此,请始终insertData.getResult()。数据为空/未定义。如果运行SELECT sql查询,它将包含(s)个数据

            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.addEventListener(SQLEvent.RESULT ,function(event:SQLEvent):void
            {
                trace("table created");
                // for showing saved city names in list on App start up
                showSavedNames(); **//Need to call after get success event**
            });
            statement.execute();
        }

        public var insertData:SQLStatement
        protected function saveName():void
        {
            // SQLite Usage
            insertData = new SQLStatement();
            insertData.sqlConnection = dbConnection;
            insertData.text = "INSERT INTO personinfo(nameofperson, ageofperson) VALUES(:nameofperson, :ageofperson)";
            insertData.parameters[":nameofperson"] = "Xyz";
            insertData.parameters[":ageofperson"] = "27"
            insertData.addEventListener(SQLEvent.RESULT, dataInsertedSuccess);
            insertData.addEventListener(SQLErrorEvent.ERROR, function(event:SQLErrorEvent):void
            {
                //details   "table 'personinfo' has no column named 'nameofcity'"   
                trace(event.error.message.toString());
                //                  showSavedNames();
            });
            insertData.execute();
        }

        protected function dataInsertedSuccess(event:SQLEvent):void
        {
            trace("Success :: " + insertData.getResult().rowsAffected);
            showSavedNames();
        }
        //////////////////////////////////////////////
        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);
        }

        public var selectNameQuery:SQLStatement;
protected function get_names_data(event:MouseEvent):void
        {
            //Need not to get ageofperson from db
            Alert.show(listOfAddedNames.selectedItem.ageofperson);

            //Any way continue your way
            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[0].ageofperson);
        }
            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.addEventListener(SQLEvent.RESULT ,function(event:SQLEvent):void
            {
                trace("table created");
                // for showing saved city names in list on App start up
                showSavedNames(); **//Need to call after get success event**
            });
            statement.execute();
        }

        public var insertData:SQLStatement
        protected function saveName():void
        {
            // SQLite Usage
            insertData = new SQLStatement();
            insertData.sqlConnection = dbConnection;
            insertData.text = "INSERT INTO personinfo(nameofperson, ageofperson) VALUES(:nameofperson, :ageofperson)";
            insertData.parameters[":nameofperson"] = "Xyz";
            insertData.parameters[":ageofperson"] = "27"
            insertData.addEventListener(SQLEvent.RESULT, dataInsertedSuccess);
            insertData.addEventListener(SQLErrorEvent.ERROR, function(event:SQLErrorEvent):void
            {
                //details   "table 'personinfo' has no column named 'nameofcity'"   
                trace(event.error.message.toString());
                //                  showSavedNames();
            });
            insertData.execute();
        }

        protected function dataInsertedSuccess(event:SQLEvent):void
        {
            trace("Success :: " + insertData.getResult().rowsAffected);
            showSavedNames();
        }
        //////////////////////////////////////////////
        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);
        }

        public var selectNameQuery:SQLStatement;
protected function get_names_data(event:MouseEvent):void
        {
            //Need not to get ageofperson from db
            Alert.show(listOfAddedNames.selectedItem.ageofperson);

            //Any way continue your way
            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[0].ageofperson);
        }