Connecting To SQLite
Join the DZone community and get the full member experience.
Join For FreeFor 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 );
}
SQLite
Opinions expressed by DZone contributors are their own.
Comments