Building Adobe AIR application for BlackBerry Playbook with SQLite Database


Developing Adobe AIR application for BlackBerry Tablet, i-OS, Android Devices with SQLite database is quite easy as like building desktop Adobe AIR application with SQLite database.

In this article, we will create a SQLite database in blackBerry Tablet Playbook device & named it as “Users.db” & create SQLConnection with the SQLite database.

Within the applicationComplete event handler, the SQLConnection is initiated, two event listeners are added to listen for SQLEvent.OPEN and    SQLErrorEvent.ERROR, and finally, the  openAsync method is called and the db file is passed in.  

After the database is opened, the   openHandler function is called. Within this function,  the  SQLEvent.OPEN event listener is removed. Next, the  createTableStmt is created, configured,  and executed. This statement will create a new table called Users if it doesn’t yet exist. If this statement is successful, then the createResult method is called.

Code: 

<?xml version=”1.0″ encoding=”utf-8″?>

<s:Application

xmlns:fx=”http://ns.adobe.com/mxml/2009

   xmlns:s=”

library://ns.adobe.com/flex/spark

   applicationComplete=”application1_applicationCompleteHandler(event)”

>


<fx:Script>


<![CDATA[

import mx.collections.ArrayCollection;

import mx.events.FlexEvent;

privatevar db:File = File.applicationStorageDirectory.resolvePath(“users.db”);

privatevar conn:SQLConnection;

privatevar createTableStmt:SQLStatement;

privatevar createTableSQL:String = “CREATE TABLE IF NOT EXISTS User(“ +

“userId INTEGER PRIMARY KEY AUTOINCREMENT,”+

“firstName TEXT,”+“lastName TEXT)”;

privatevar selectStmt:SQLStatement;

privatevar selectSQL:String = “SELECT* FROM User”;

privatevar insertStmt:SQLStatement;

privatevar insertSQL:String = “INSERT INTO User(firstName,lastName)” +

“Values (:firstName,:lastName)”;

protectedfunction application1_applicationCompleteHandler(event:FlexEvent):void{

conn =

new SQLConnection();

conn.addEventListener(SQLEvent.OPEN,openHandler);

conn.addEventListener(SQLErrorEvent.ERROR,errorHandler);

conn.openAsync(db);

}

privatefunction openHandler(event:SQLEvent):void {

log.text +=

“Database opened sucessfully”;

conn.removeEventListener(SQLEvent.OPEN,openHandler);

createTableStmt =

new SQLStatement();

createTableStmt.sqlConnection = conn;

createTableStmt.text = createTableSQL;

createTableStmt.addEventListener(SQLEvent.RESULT,createResult);

createTableStmt.addEventListener(SQLErrorEvent.ERROR,errorHandler);

createTableStmt.execute();

}

privatefunction createResult(event:SQLEvent):void {

log.text +=

“\nTable created”;

conn.removeEventListener(SQLEvent.RESULT,createResult);

selectUsers();

}

privatefunction errorHandler(event:SQLErrorEvent):void {

log.text +=

“\nError message:” + event.error.message;

log.text +=

“\nDetails:” + event.error.details;

}

privatefunction selectUsers():void {

selectStmt =

new SQLStatement();

selectStmt.sqlConnection = conn;

selectStmt.text = selectSQL;

selectStmt.addEventListener(SQLEvent.RESULT,selectResult);

selectStmt.addEventListener(SQLErrorEvent.ERROR,errorHandler);

selectStmt.execute();

}

privatefunction selectResult(event:SQLEvent):void {

log.text +=

“\nSelect completed”;

var result:SQLResult = selectStmt.getResult();

users.dataProvider =

new ArrayCollection(result.data);

}

protectedfunction button1_clickHandler(event:MouseEvent):void {

insertStmt =

new SQLStatement();

insertStmt.sqlConnection = conn;

insertStmt.text = insertSQL;

insertStmt.parameters[

“:firstName”] = firstName.text;

insertStmt.parameters[

“:lastName”] = lastName.text;

insertStmt.addEventListener(SQLEvent.RESULT,insertResult);

insertStmt.addEventListener(SQLErrorEvent.ERROR,errorHandler);

insertStmt.execute();

}

privatefunction insertResult(event:SQLEvent):void {

log.text +=

“\nInsert completed”;

selectUsers();

}

]]>

</fx:Script>


<fx:Declarations>


<!– Place non-visual elements (e.g., services, value objects) here –>


</fx:Declarations>

<s:Label text=”First name” top=”35” left=”10/>


<s:TextInput id=”firstName” left=”150” top=”10” width=”300/>


<s:Label text=”Last Name” top=”95” left=”10/>


<s:TextInput id=”lastName” left=”150” top=”70” width=”300/>


<s:Button label=”Save” click=”button1_clickHandler(event)” top=”130” left=”150/>

<s:Scroller height=”200” width=”100%” left=”10” right=”10” top=”200>


<s:DataGroup id=”users” height=”100%” width=”95%” itemRenderer=”UserRenderer>


<s:layout>


<s:VerticalLayout/>


</s:layout>


</s:DataGroup>


</s:Scroller>


<s:TextArea id=”log” width=”100%” bottom=”0” height=”250/>

</s:Application>

Code for User Renderer:

<?xml version=”1.0″ encoding=”utf-8″?>

<s:ItemRenderer

xmlns:fx=”http://ns.adobe.com/mxml/2009

xmlns:s=”

library://ns.adobe.com/flex/spark>


<s:Label text=”{data.lastName},{data.firstName}/>

</s:ItemRenderer>

Lets enjoy a SQLite database application in BlackBerry Tablet 7 Playbook simulator & Device .

Before launching to device , mention to change the default version of the application from 0.0.0 to 1.0.0. in app.xml.

This slideshow requires JavaScript.

Advertisements

About Anindita
Anindita Basak is working as Big Data Cloud Consultant in Microsoft. Worked in multiple MNCs as Developer & Senior Developer on Microsoft Azure, Data Platform, IoT & BI , Data Visualization, Data warehousing & ETL & of course in Hadoop platform.She played both as FTE & v- employee in Azure platform teams of Microsoft.Passionate about .NET , Java, Python & Data Science. She is also an active Big Data & Cloud Trainer & would love share her experience in IT Training Industry. She is an author, forum contributor, blogger & technical reviewer of various books on Big Data Hadoop, HDInsight, IoT & Data Science, SQL Server PDW & PowerBI.

2 Responses to Building Adobe AIR application for BlackBerry Playbook with SQLite Database

  1. Pingback: Blackberry Playbook, Outsmarting Its Rivals - World of Technologies

  2. Pingback: Your Questions About Beat Machine Beats | Beat Maker Software SonicProducer

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: