代码之家  ›  专栏  ›  技术社区  ›  Joel

adobe air/flex+sqlite数据库问题

  •  4
  • Joel  · 技术社区  · 16 年前

    我还是Adobe Air/Flex的新手,而且还是SQL的新手。

    我下载了 this ( http://coenraets.org/blog/2008/11/using-the-sqlite-database-access-api-in-air _-part-1/)代码,并且一直在研究它,我正在尝试实现相同的想法。

    我觉得这是愚蠢的事情。我用的是flex builder。我做了一个新的桌面应用程序项目,没有导入任何内容。

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

    我正在尝试这样做,当程序初始化时,它将从数据库加载数据(如果存在的话),否则它将创建一个新的数据库。

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

    <?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>
    
    2 回复  |  直到 16 年前
        1
  •  3
  •   Feet    16 年前

    我刚用你的代码试过。我做了一个更改,删除了条件,因为我得到了关于表不存在的错误。

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

    这使数据报显示了正确的信息。我认为您初始化数据库文件的方式有问题。我现在正在调查这件事。

    试用使用

    sqlConnection.open(file, SQLMode.CREATE);
    

    而是打开数据库。

        2
  •  3
  •   Joel    16 年前

    谢谢你的双脚。有了你的建议,我相信我已经明白了。我将if语句改为:

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

    而且效果很好。谢谢你的帮助。