Problem with Adobe Air / Flex + SQLite

I'm still new to Adobe Air / Flex and still pretty new to SQL.

I downloaded this ( http://coenraets.org/blog/2008/11/using-the-sqlite-database-access-api-in-air ... -part-1 /) and looked at it and I'm trying to implement the same idea.

I think this is just something stupid. I am using Flex Builder. I created a new project for desktop applications, I did not import anything.

I added a DataGrid object and attached it to an ArrayCollection:

I try to do this when the program is initialized, it will load data from the database, if it exists, otherwise it will create a new one.

The problem is that the datagrid is empty when the application starts. No column headers, no data, nothing. I tried to change a whole bunch of things, I used a debugger to make sure that all functions are called the way they should. I do not know what I am doing wrong. I compared my code with the previous code, I was looking for tutorials on Google. Does 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> 
+4
source share
2 answers

I just tried this using your code. I made the changes and removed the condition, as I was getting errors that the table does not exist.

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

This caused the datagrid to display the correct information. I think that something is wrong with how you initialize the database file. I am studying it now.

Try using

 sqlConnection.open(file, SQLMode.CREATE); 

instead, to open the database.

+3
source

Thanks. With your suggestion, I believe I understood this. I changed the if statement:

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

And it works great. Thank you for your help.

+3
source

All Articles