Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


Share this Page URL
Help

Section 4: Building Air Applications > Create a Database - Pg. 212

212 Building Air ApplicAtions in case we want them to be accessed only by the application or a particular user who has been provided with credentials to access it. There are a few classes that we should know in order to work with local databases: · flash.data.SQLConnection--to create/open/interact with the database · flash.data.SQLStatement--to represent an SQL query/com- mand and parameters · flash.data.SQLResult--torepresenttheoutputofacommand · flash.event.SQLEvent--to carry data about an SQL operation (in asynchronous mode) There are two ways to interact with local databases: synchro- nous and asynchronous. In the first case the operation is executed in the same sequence of the action code. For example, let's look at the following code. obj.method1(); // synchronous database operation obj.method2(); In this case method2() is called when the operation on the database is finished. It is not wrong or right, it just depends on the scenario. If the code in method2() can be performed without waiting for the result of the database, then it is better to adopt an asynchronous approach to a database operation, which is in general a more flexible approach, but requires a bit more code to be ritten--for example, to set up event listeners and callback w handling. create a database To create a database we follow a process very similar to the creation of a file. We create an instance of SQLConnection. We can optionally pass a file and open the connection, as in the fol- lowing code. var connection:SQLConnection = new SQLConnection(); var f:File = File.applicationStorageDirectory.resolvePath( "DBSample.db" ); connection.openAsync(f); //asynchronous connection.open(f); //synchronous If we do not provide a file, the database is created in the mem- ory of the computer. Let's assume we follow the asynchronous way. We need to set up some callback to handle events. This has to be done before calling the openAsynch() method.