DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Jonnie has posted 12 posts at DZone. View Full User Profile

Connecting To SQLite

09.21.2008
| 9270 views |
  • submit to reddit
        For connecting to and creating a db, use the code as follows:

//DB Connection
private var dbconn:SQLConnection;
//Query Statement
private var sqlQuery:SQLStatement;		
//Create Table Statement
private var sqlCreateTable:SQLStatement;
//Insert Statement
private var sqlInsert:SQLStatement;
//Import Statement
private var sqlImport:SQLStatement;

//Returned Data
[Bindable] private var sqlData:ArrayCollection;

/**
 * Creates a database connection, registers event listeners, specifies the database filename
 * checks to see if the database exsists, if it does then we connection to it.
 * If it doesnt, then we create a new database file, and create our tables.
 * 
 * @constructor
 */		
public function DatabaseManager()
{
	//Connect to the db
	dbconn = new SQLConnection();
		
	//Add event listener
	dbconn.addEventListener( SQLErrorEvent.ERROR, onSQLError );
	
	//Set the location of the db file				
	var dbFile:File = File.applicationStorageDirectory.resolvePath( "MyDatabase.db" );
	
	//Check if the db file exsists
	if ( dbFile.exists )
	{
		//Connect to the db then
		dbconn.addEventListener( SQLEvent.OPEN, onSQLOpen );
	} else {
		//Create the db
		dbconn.addEventListener( SQLEvent.OPEN, onSQLCreate );	
	}
	//Execute
	dbconn.openAsync( dbFile );			
}