Over a million developers have joined DZone.

Connecting To SQLite

DZone's Guide to

Connecting To SQLite

· ·
Free Resource
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 );	
	dbconn.openAsync( dbFile );			


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}