Calendar

June 2007
S M T W T F S
« May   Jul »
 12
3456789
10111213141516
17181920212223
24252627282930

Tag Cloud

Categories

Archives

Recent Posts

Recent Comments


« AIR Embedded Database Sample Application | Main | AIR Embedded Database Code Exerpts 2 »

AIR Embedded Database Code Exerpts 1

By Rich Tretola | June 12, 2007
2,406 views

Here are some excerpts for the AIR Embedded Database post.

The class below on init will attempt to open the database file and if it is not found will then create the database file and then call the functions to create the tables.

Creating the database:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.everythingflex.employeeDirectory.utils
{
    import com.adobe.cairngorm.control.CairngormEventDispatcher;
    import com.everythingflex.employeeDirectory.control.GetUserIdEvent;
    import com.everythingflex.employeeDirectory.dao.EmployeeDAO;
    import com.everythingflex.employeeDirectory.dao.PreferencesDAO;
    import com.everythingflex.employeeDirectory.model.ModelLocator;

    import flash.data.SQLResult;
    import flash.data.SQLStatement;
    import flash.events.SQLErrorEvent;
    import flash.events.SQLEvent;
    import flash.filesystem.File;

    public class CreateTables
    {

        private var model:ModelLocator = ModelLocator.getInstance();

        private var employeeDAO:EmployeeDAO = new EmployeeDAO();
        private var preferencesDAO:PreferencesDAO = new PreferencesDAO();

        private var createEmployeesSQL:SQLStatement;
        private var createPreferencesSQL:SQLStatement;

        // check for existance of local database and either open existing or create new tables
        public function init():void{
            var dbFile:File = File.applicationStorageDirectory.resolve("EmployeeDirectory.db");
            if(dbFile.exists) {
                model.db.addEventListener(SQLEvent.OPEN, statusHandler);
                model.db.open(dbFile);
            } else {
                model.db.addEventListener(SQLEvent.OPEN, newDatabaseHandler);
                model.db.open(dbFile);
            }
        }

        // get local data
        private function statusHandler(event:SQLEvent):void {
            employeeDAO.getEmployees();
            preferencesDAO.getUserId();
        }

        //  create new tables
        private function newDatabaseHandler(event:SQLEvent):void {
            createEmployeesTable();
            createPreferencesTable();
        }

        // create Employees table
        private function createEmployeesTable():void{
            var sqlText:String = "CREATE TABLE Employees(EMAIL TEXT, EMPLOYEE_ID INTEGER PRIMARY KEY, FIRST_NAME TEXT, JOB_TITLE TEXT, LAST_NAME TEXT, USER_ID INTEGER)";
            createEmployeesSQL = new SQLStatement();
            createEmployeesSQL.sqlConnection = model.db;
            createEmployeesSQL.addEventListener(SQLEvent.RESULT, createEmployeesTableResult);
            createEmployeesSQL.addEventListener(SQLErrorEvent.ERROR, statusHandler);
            createEmployeesSQL.text = sqlText;
            createEmployeesSQL.execute();
            model.log +=  sqlText + " \n";
        }

        private function createEmployeesTableResult(event:SQLEvent):void{
            model.log += "Employees table created \n";
        }

        // Create Preferences table
        private function createPreferencesTable():void{
            var sqlText:String = "CREATE TABLE Preferences(USER_ID INTEGER)";
            createPreferencesSQL = new SQLStatement();
            createPreferencesSQL.sqlConnection = model.db;
            createPreferencesSQL.addEventListener(SQLEvent.RESULT, createPreferencesTableResult);
            createPreferencesSQL.addEventListener(SQLErrorEvent.ERROR, statusHandler);
            createPreferencesSQL.text = sqlText;
            createPreferencesSQL.execute();
            model.log +=  sqlText + " \n";
        }

        // get a new unique userId from remote server
        private function createPreferencesTableResult(event:SQLEvent):void{
            model.log += "Preferences table created \n";
            var getUserIdEvent : GetUserIdEvent = new GetUserIdEvent();
            CairngormEventDispatcher.getInstance().dispatchEvent( getUserIdEvent );
        }

    }
}

I will post some additional excerpts when I get the time.

Topics: Adobe AIR, Tutorials | 1 Comment »

One Response to “AIR Embedded Database Code Exerpts 1”

  1. Hosting Says:
    September 7th, 2009 at 10:57 am

    nice blog nice web thanks you admin

    Reply to this comment

Comments