2008-12-28 120 views
4

我仍然是Adobe Air/Flex的新手,并且对SQL仍然很新颖。Adob​​e Air/Flex + SQLite数据库问题

我已经下载thishttp://coenraets.org/blog/2008/11/using-the-sqlite-database-access-api-in-air ... -part-1 /)代码,并且一直在查看它,并且我试图实现相同的想法。

我认为这只是一些愚蠢的东西。我正在使用Flex Builder。我做了一个新的桌面应用程序项目,没有导入任何东西。

我添加了一个DataGrid对象,并将其绑定到一个ArrayCollection:

我试图使它所以当程序初始化它会从数据库中如果存在加载数据,否则将创建一个新的一。

问题是,当应用程序运行时,数据网格为空。没有列标题,没有数据,什么都没有。我试过改变一大堆东西,我用调试器来确保所有的函数都被调用,就像它们应该的那样。我不知道我做错了什么。我将我的代码与前面提到的代码进行了比较,我在Google上查找了教程。任何人都知道我在做什么错了?

<?xml version="1.0" encoding="utf-8"?> 
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="672" height="446" 
    applicationComplete="onFormLoaded()" 
    title="iRecipes"> 

    <mx:Script> 
     <![CDATA[ 
      import mx.collections.ArrayCollection; 

      private var sqlConnection:SQLConnection; 
      [Bindable] private var recipeList:ArrayCollection; 

      private function onFormLoaded():void 
      { 
       sqlConnection = new SQLConnection(); 
       openDataBase(); 
      } 

      private function openDataBase():void 
      { 
       var file:File = File.userDirectory.resolvePath("recipes.db"); 

       sqlConnection.open(file, SQLMode.CREATE); 

       if(!file.exists) 
       { 
        createDatabase(); 
       }   

       populateRecipeList() 
      } 

      private function createDatabase():void 
      { 
       var statement:SQLStatement = new SQLStatement(); 
       statement.sqlConnection = sqlConnection; 
       statement.text = "CREATE TABLE Recipes (recipeId INTEGER PRIMARY KEY AUTOINCREMENT, recipeName TEXT, authorName TEXT)"; 
       statement.execute(); 

       statement.text = "INSERT INTO Recipes (recipeName, authorName) VALUES (:recipeName, :authorName)"; 

       statement.parameters[":recipeName"] = "Soup"; 
       statement.parameters[":authorName"] = "Joel Johnson"; 
       statement.execute(); 

       statement.parameters[":recipeName"] = "Garbage"; 
       statement.parameters[":authorName"] = "Bob Vila"; 
       statement.execute(); 
      } 

      private function populateRecipeList():void 
      { 
       var statement:SQLStatement = new SQLStatement(); 
       statement.sqlConnection = sqlConnection; 

       statement.text = "SELECT * FROM Recipes"; 
       statement.execute(); 
       recipeList = new ArrayCollection(statement.getResult().data); 
      } 
     ]]> 
    </mx:Script> 

    <mx:DataGrid dataProvider="{recipeList}"> 

    </mx:DataGrid> 
</mx:WindowedApplication> 

回答

3

我刚刚使用你的代码试了一下。我做了一个改变,并删除了条件,因为我得到的表不存在错误。

//if(!file.exists) 
//{ 
    createDatabase(); 
//} 

这得到了datagrid显示正确的信息。我认为你初始化数据库文件的方式有些问题。我现在正在研究它。

使用

sqlConnection.open(file, SQLMode.CREATE); 

相反,用于打开数据库的尝试。

3

谢谢。有了你的建议,我相信我已经弄明白了。我改变了这个if语句:

  if(!file.exists) 
      { 
       sqlConnection.open(file, SQLMode.CREATE); 
       createDatabase(); 
      } 
      else    
      { 
       sqlConnection.open(file, SQLMode.UPDATE); 
      } 

它很好用。谢谢你的帮助。