[英]Adobe Air/Flex + SQLite database problem
I'm still a newbie to Adobe Air/Flex, and still fairly new with SQL. 我仍然是Adobe Air / Flex的新手,而且还是SQL的新手。
I've downloaded this ( http://coenraets.org/blog/2008/11/using-the-sqlite-database-access-api-in-air …-part-1/) code and have been looking over it and I'm trying to implement the same idea. 我已经下载了这个 ( http://coenraets.org/blog/2008/11/using-the-sqlite-database-access-api-in-air ... -part-1 /)代码并且一直在查看它我正在尝试实现相同的想法。
I think it's just something stupid. 我认为这只是一些愚蠢的事情。 I'm using Flex Builder. 我正在使用Flex Builder。 I made a new desktop application project, didn't import anything. 我做了一个新的桌面应用程序项目,没有导入任何东西。
I added a DataGrid object and bound it to an ArrayCollection: 我添加了一个DataGrid对象并将其绑定到ArrayCollection:
I'm trying to make it so when the program initializes it will load data from a database if it exists, otherwise it'll create a new one. 我试图这样做,当程序初始化时,它将从数据库中加载数据(如果存在),否则它将创建一个新数据。
The problem is, when the application runs, the datagrid is empty. 问题是,当应用程序运行时,datagrid为空。 No column headers, no data, nothing. 没有列标题,没有数据,没有。 I've tried changing a whole bunch of stuff, I've used the debugger to make sure all the functions are being called like they're supposed to. 我已经尝试过改变一大堆东西,我已经使用调试器来确保所有函数都被调用,就像它们应该的那样。 I don't know what I'm doing wrong. 我不知道我做错了什么。 I've compared my code to the before mentioned code, I've looked for tutorials on Google. 我已经将我的代码与前面提到的代码进行了比较,我在Google上寻找过教程。 Anyone know what I'm doing wrong? 谁知道我做错了什么?
<?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>
I just tried this out using your code. 我只是尝试使用你的代码。 I made a change and removed the condition as I was getting errors about the table not existing. 我做了一个更改并删除了条件,因为我得到的错误表不存在。
//if(!file.exists)
//{
createDatabase();
//}
This got the datagrid showing the correct info. 这使得datagrid显示正确的信息。 I think that there is something wrong with the way you are initialising the database file. 我认为初始化数据库文件的方式有问题。 I'm having a look into it at the moment. 我现在正在研究它。
Try using 尝试使用
sqlConnection.open(file, SQLMode.CREATE);
instead, for opening the database. 相反,用于打开数据库。
Thanks Feet. 谢谢脚。 With your suggestion, I believe I have figured it out. 有了你的建议,我相信我已经明白了。 I changed the if statement to this: 我将if语句更改为:
if(!file.exists)
{
sqlConnection.open(file, SQLMode.CREATE);
createDatabase();
}
else
{
sqlConnection.open(file, SQLMode.UPDATE);
}
And it works great. 而且效果很好。 Thanks for your help. 谢谢你的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.