2012-10-17 70 views
0

我正在製作一個移動應用程序在flex 4.5中,我想獲取人的姓名和年齡,然後保存到sqlite數據庫做我的代碼如下所示:無法從flexite數據庫中獲取數據

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_數據的方法是這樣的:

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數據列根據人名列表中選定的名稱 - 在此先感謝

回答

1

由於您打開sqlite數據庫異步模式需要了解異步執行模型的第一個問題。 列名'nameofcity'的第二個問題是人表沒有像你聲明的任何列,所以我在這裏修改爲'nameofperson'在saveName()中。

如果你調用saveName(),請確保你已經調用了'saveName()'。

in dataInsertedSuccess()在sqlquery中,只有當再次運行時才返回受影響的行no INSERT/UPDATE sql查詢即只返回整數值。因此,總是insertData.getResult()。如果您運行SELECT sql查詢,則數據爲null/undefined.it將包含數據。

  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); 
     } 
+0

非常感謝。 –