【问题标题】:Adobe Air/Flex + SQLite database problemAdobe Air/Flex + SQLite 数据库问题
【发布时间】:2008-12-28 05:39:04
【问题描述】:

我还是 Adob​​e Air/Flex 的新手,对 SQL 还是很陌生。

我已经下载了this (http://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>

【问题讨论】:

    标签: apache-flex sqlite datagrid air adobe


    【解决方案1】:

    我刚刚使用您的代码进行了尝试。我进行了更改并删除了条件,因为我收到有关表不存在的错误。

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

    这使数据网格显示了正确的信息。我认为您初始化数据库文件的方式有问题。我现在正在研究它。

    尝试使用

    sqlConnection.open(file, SQLMode.CREATE);
    

    相反,用于打开数据库。

    【讨论】:

      【解决方案2】:

      谢谢脚。有了你的建议,我相信我已经想通了。我将 if 语句更改为:

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

      而且效果很好。感谢您的帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-16
        • 2010-12-02
        • 1970-01-01
        • 2012-12-16
        • 2010-11-23
        • 1970-01-01
        • 2017-06-16
        • 1970-01-01
        相关资源
        最近更新 更多