17
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1

Phonegap Bridge – File System CIS 136 Building Mobile Apps 1

Embed Size (px)

Citation preview

Page 1: Phonegap Bridge – File System CIS 136 Building Mobile Apps 1

Phonegap Bridge – File System

CIS 136 Building Mobile Apps

1

Page 2: Phonegap Bridge – File System CIS 136 Building Mobile Apps 1

2

Storage Options

Storage API

Page 3: Phonegap Bridge – File System CIS 136 Building Mobile Apps 1

3

Storage API Implements a simple database application

full-featured database tables accessed via SQL queries Based on SQLLite database

https://www.sqlite.org/lang.html

Page 4: Phonegap Bridge – File System CIS 136 Building Mobile Apps 1

4

Methods openDatabase() –

Creates a new SQLLite database or opens one that has already been created

database is in persistent storage This method returns a database object that allows manipulation

of the data

transaction()– executes SQL statements if any statement fails, any changes that have been made are

discarded (roll back) executeSql()-

Page 5: Phonegap Bridge – File System CIS 136 Building Mobile Apps 1

5

openDatabase() method Fours parameters

db_name: name of the database – this is the filename given tio the database when its written to memory; text

db_version: version number (usually 1.0); text db_display_name: display name for the database; text db_size: amount of space allocated to the database in bytes

Page 6: Phonegap Bridge – File System CIS 136 Building Mobile Apps 1

6

<script>

// Wait for device API libraries to load document.addEventListener("deviceready", onDeviceReady, false);

// device APIs are available function onDeviceReady() { var appDB = window.openDatabase(“myDB”,”1.0”,”notes”, 1024*1024); //1MB

}

</script>

Page 7: Phonegap Bridge – File System CIS 136 Building Mobile Apps 1

7

transaction() method three parameters

Transaction SQL: SQL code to execute Usually contained in a function

Receives a transaction object which is used to execute the SQL statements Executes a method called executeSql

Error: function to execute if a SQL error occurs receives two objects

First object – can be used to execute more SQL statements Second object – error object having a code and message property

Success: function to execute if SQL works (optional) Does not receive any objects

NOTE: error function precedes success function

Page 8: Phonegap Bridge – File System CIS 136 Building Mobile Apps 1

8

<script> document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { var appDB = window.openDatabase(“myDB”,”1.0”,”notes”, 1024*1024); //1MB

appDB.transaction(txnSQLn,onTxnError,onTxnSuccess); }function txnSQL(){

}function onTxnError(){}function onTxnSuccess(){}

</script>

Page 9: Phonegap Bridge – File System CIS 136 Building Mobile Apps 1

9

Transaction error() methodfunction onTxnError(sqltext, err){if (err)

{alert (“code: + err.code + “ msg: “ + err.message )}

Elsealert(“Unknown error”)

}

Page 10: Phonegap Bridge – File System CIS 136 Building Mobile Apps 1

10

Transaction Error Codes UNKNOWN_ERR = 0; DATABASE_ERR = 1; VERSION_ERR = 2; TOO_LARGE_ERR = 3; QUOTA_ERR = 4; SYNTAX_ERR = 5; CONSTRAINT_ERR = 6; TIMEOUT_ERR = 7;

Page 11: Phonegap Bridge – File System CIS 136 Building Mobile Apps 1

11

Transaction success() methodfunction onTxnSuccess(){ alert (“transaction succeeded”); }

Page 12: Phonegap Bridge – File System CIS 136 Building Mobile Apps 1

12

Transaction Sql – you name the function Gets a transaction object

Builds a SQL statement the instructions for the database action Create table, Select, Insert, Update, Delete records

Executes the SQL statement using the executeSql() method executeSql() has four parameters

The SQL statement The values passed to the SQL statement (if any) Where to go if the SQL statement works (a Success function) Where to go if the SQL statement fails ( Error function)

Page 13: Phonegap Bridge – File System CIS 136 Building Mobile Apps 1

13

Inside the Transaction SQL functionfunction CreateTable(txn){var sqlString = ‘CREATE TABLE IF NOT EXISTS STUDENTS(StudentID TEXT,FirstName TEXT,LastName TEXT,Email TEXT)’;txn.executeSql(sqlString, [], onSqlSuccess(), onSqlError(); }

Page 14: Phonegap Bridge – File System CIS 136 Building Mobile Apps 1

14

Syntax for Creating Tables

The "CREATE TABLE" command is used to create a new table in an SQLite database

A CREATE TABLE command specifies the following attributes of the new table: The name of the new table. The database in which the new table is created

Tables may be created in the main database, the temp database, or in any attached database. The name of each column (field) in the table.

The declared data type of each column in the table A default value or expression for each column in the table A default collation sequence to use with each column

Optionally, a PRIMARY KEY for the table A set of SQL constraints for each table. SQLite supports UNIQUE, NOT NULL, CHECK

and FOREIGN KEY constraints. Whether the table is a WITHOUT ROWID table (a ROWID uniquely identifies each row

)

Page 15: Phonegap Bridge – File System CIS 136 Building Mobile Apps 1

15

Data Types NULL

The value is a NULL value. INTEGER

The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.

Boolean values are integers 0 and 1 Can store a date as an integer using date/time functions - as Unix Time, the number of seconds

since 1970-01-01 00:00:00 UTC. REAL

The value is a floating point value, stored as an 8-byte IEEE floating point number Can store a date as a real number using date/time functions

Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar

TEXT The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE) Can store a date as text using date/time functions ("YYYY-MM-DD HH:MM:SS.SSS").

BLOB. The value is a blob of data, stored exactly as it was input.

Page 16: Phonegap Bridge – File System CIS 136 Building Mobile Apps 1

16

Affinities common datatype names from more traditional SQL

implementations are converted into affinitiesINTINTEGERTINYINTSMALLINTMEDIUMINTBIGINTUNSIGNED BIG INTINT2INT8

INTEGER

CHARACTER(20)VARCHAR(255)VARYING CHARACTER(255)NCHAR(55)NATIVE CHARACTER(70)NVARCHAR(100)TEXTCLOB

TEXT

BLOB BLOB

REALDOUBLEDOUBLE PRECISIONFLOAT

REAL

NUMERICDECIMAL(10,5)BOOLEANDATEDATETIME

NUMERIC

Page 17: Phonegap Bridge – File System CIS 136 Building Mobile Apps 1

17

ExampleCREATE TABLE COMPANY( ID INT PRIMARY KEY ASC NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL);