Friday, April 20, 2012

Trying to load SQLite database once and persist across views in mobile Flex app

I have created a mobile app that has 3 views.



Each view needs access to a local SQLite database and currently I load the database on each view. What I would like to do is load the database once and use the sqlConnection and sqlStatement between views.



After some research I believe I need to create a Class - This is the 1st time I created a class and I have no errors in my class. I created 2 public variables called



sqlConnection and stmt and made them public. I then call my method that loads the database.



In my views I want to refer to those public variables so I can reuse the sqlconnection and sqlstatement whenever I wish. But I get an error message saying 'access of undefined property stmt' which is my public variable in the Class that is for my sqlstatement.



My class is structered like so:



package com.myDomain.db
{
import flash.data.SQLConnection;
import flash.data.SQLResult;
import flash.data.SQLStatement;
import flash.display.Sprite;
import flash.events.SQLErrorEvent;
import flash.events.SQLEvent;
import flash.filesystem.File;

public class dbOperations
{
public var sqlConnection:SQLConnection;
public var stmt:SQLStatement = new SQLStatement();
private var isLoaded:Boolean = false;


public function OpenDB():void
{

if(!isLoaded){

isLoaded = true

sqlConnection = new SQLConnection();
sqlConnection.open(File.applicationDirectory.resolvePath("myFile.db"));
stmt.sqlConnection = sqlConnection;
stmt.text = "CREATE TABLE IF NOT EXISTS tblDefault(DefaultID integer primary key, Field1 text, Field2 numeric)";
stmt.execute();
}
}
}


}



Obviously I am doing something wrong but how do I refer to the public variables sqlConnection and stmt from within my views.



cheers,






Never mind people I figured it out.



I removed my public declarations from this class and I created a new class to hold my Global variables and set them as static and refer to my global variables through my global class.



cheers,





No comments:

Post a Comment