0

私はflex4.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列からデータを取得する方法を教えてください-よろしくお願いします

4

1 に答える 1

1

sqliteデータベースの非同期モードを開いたため、非同期実行モデルを理解する必要があるという最初の問題。列名'nameofcity'の2番目の問題は、personテーブルに宣言したような列がないため、saveName()で'nameofperson'としてここで変更します。

saveName()を呼び出す場合は、「saveName()」を呼び出していることを確認してください。

dataInsertedSuccess()の場合、sqlqueryでは、再度実行した場合にのみ影響を受ける行を返しません。tINSERT / UPDATE sql query、つまり整数値のみを返します。したがって、常にinsertData.getResult()。dataはnull / undefinedです。SELECTsqlクエリを実行すると、データが含まれます。

            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);
        }
于 2012-10-18T06:22:13.527 に答える