Download presentation
Presentation is loading. Please wait.
Published byDonna Bailey Modified over 9 years ago
1
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1
2
Storage Options Storage API 2
3
3 Implements a simple database application full-featured database tables accessed via SQL queries Based on SQLLite database https://www.sqlite.org/lang.html
4
Methods 4 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()-
5
openDatabase() method 5 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
6
6 // 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 }
7
transaction() method 7 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
8
8 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() { }
9
Transaction error() method 9 function onTxnError(sqltext, err) { if (err) { alert (“code: + err.code + “ msg: “ + err.message ) } Else alert(“Unknown error”) }
10
Transaction Error Codes 10 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;
11
Transaction success() method 11 function onTxnSuccess() { alert (“transaction succeeded”); }
12
Transaction Sql – you name the function 12 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)
13
Inside the Transaction SQL function 13 function CreateTable(txn) { var sqlString = ‘CREATE TABLE IF NOT EXISTS STUDENTS (StudentID TEXT, FirstName TEXT, LastName TEXT, Email TEXT)’; txn.executeSql(sqlString, [], onSqlSuccess(), onSqlError(); }
14
Syntax for Creating Tables 14 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 )WITHOUT ROWID
15
Data Types 15 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.
16
Affinities 16 common datatype names from more traditional SQL implementations are converted into affinities INT INTEGER TINYINT SMALLINT MEDIUMINT BIGINT UNSIGNED BIG INT INT2 INT8 INTEGER CHARACTER(20) VARCHAR(255) VARYING CHARACTER(255) NCHAR(55) NATIVE CHARACTER(70) NVARCHAR(100) TEXT CLOB TEXT BLOB REAL DOUBLE DOUBLE PRECISION FLOAT REAL NUMERIC DECIMAL(10,5) BOOLEAN DATE DATETIME NUMERIC
17
Example 17 CREATE TABLE COMPANY( ID INT PRIMARY KEY ASC NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL );
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.