Adobe Air/Flex + SQLite 数据库问题

4
我对Adobe Air/Flex还是个新手,对SQL也相对陌生。
我已经下载了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>
2个回答

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

它非常好用。感谢您的帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接