SQLite Supported by BlackBerry OS 5.0 Using SQLite.

Slides:



Advertisements
Similar presentations
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
Java Card Technology Ch07: Applet Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Computer Science & Engineering.
Stored Procedure Language Stored Procedure Overview Stored Procedure is a function in a shared library accessible to the database server can also write.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Managing Concurrency in Web Applications. DBI 2007 HUJI-CS 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses.
Cosc 5/4730 Android and Blackberry SQLite. For the sql language syntax, please see SQlite documentation –
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
Cursors in Pl/SQL Database 1. Practice. Sample Database The schema of the sample database is the following: Drinkers (name, occupation, birthday, salary)
Data Persistence in Android
SQLite Database. SQLite Public domain database – Advantages Small (about 150 KB) – Used on devices with limited resources Each database contained within.
Agenda Journalling More Embedded SQL. Journalling.
Javax.sql and java.sql. java.sql Interface Connection public interface Connection extends WrapperWrapper A connection (session) with a specific database.
ADO.NET A2 Teacher Up skilling LECTURE 3. What’s to come today? ADO.NET What is ADO.NET? ADO.NET Objects SqlConnection SqlCommand SqlDataReader DataSet.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Dinamic SQL & Cursor. Why Dinamic SQL ? Sometimes there is a need to dynamically create a SQL statement on the fly and then run that command. This can.
Programming using C# Joins SQL Injection Stored Procedures
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Advanced SQL: Cursors & Stored Procedures
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Stored Procedure James Wang.
BlackBerry Persistent Storage Models Persistent Storage APIs and Record Management System.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
Object Oriented Software Development 10. Persistent Storage.
Constraints, Triggers and Views COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Manipulating Data in PL/SQL. 2 home back first prev next last What Will I Learn? Construct and execute PL/SQL statements that manipulate data with DML.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
Microsoft Access 2013 ®® Tutorial 12 Managing and Securing a Database.
JDBC CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Android - SQLite Database 12/10/2015. Introduction SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with.
SQlite. SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
JDBC - Resultset The java.sql.ResultSet interface represents the result set of a database query. A ResultSet object maintains a cursor that points to the.
DBI: The Neophyte's Guide1 What is DBI? DBI = DataBase Interface DBI is database-independent DBI allows you to write code that interacts with databases.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
App Package Folder App data Folders Local Roaming Temp Removable Storage (SD Card) Cloud Credential Locker B/ground Transfer Publishers Shared Folder.
1 Stored Procedures in MySQL Part I. 2 Objectives SQL Vs. MySQL SP MySQL SP Parameters MySQL SP Control Structures.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
JDBC.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
By: Eliav Menachi.  On Android, all application data (including files) are private to that application  Android provides a standard way for an application.
Introduction to Database Programming with Python Gary Stewart
Data Storage in Android Димитър Н. Димитров. Why talk about data? Why not 3D graphics or network connectivity? Data as fundamental term in computer science.
Making content providers
ASP.NET Programming with C# and SQL Server First Edition
ATS Application Programming: Java Programming
Views, Stored Procedures, Functions, and Triggers
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Mobile Computing With Android ACST 4550 Android Database Storage
Chapter 10 ADO.
Chapter 8 Advanced SQL.
Information Management
Data.
plus content providers, loaders, recyclerview
Manipulating Data Lesson 3.
Stored Procedure Language
Presentation transcript:

SQLite Supported by BlackBerry OS 5.0 Using SQLite

Overview SQLite Library SQLite Database Database Security Options A Statement Life Cycle Transactions Remove Databases Vacuum Command Best Parctice

SQLite Library It is a relational database library. Has a small footprint. Hence, it is good for small devices. BlackBerry devices that run BlackBerry Device Software version 5.0 or later supports SQLite library. SQLite library has been introduced in BlackBerry Java Development Environment Version 5 and later.

SQLite Library (Cont.) The package that provides classes and interfaces to work with SQLite is “net.rim.device.api.database”. DB locations can be : –SD Card: /SDCard/ –Device memory: /store/home/user –System memory: /system/ –Default location: /SDCard/databases/

SQLite Database SQLite database is stored in a single file on a SD Card. The default location for a SQLite database is /SDCard/databases/ You can store the database file in the device memory by specifying the file system path It can be encrypted or plain text.

How to Create a Plain Database Import net.rim.device.api.database that include classes to work with SQLite. Create a URI representing the database file –dbURI = URI.create(“file:///SDCard/Databases/myApplication/” + “MyDatabase.db”); Invoke the Create() method of the DatabaseFactory class to create a new database –Database d = DatabaseFactory.create(dbURI);

How to Create a Plain Database -or – Invoke openorCreate() to create a new database or open an existing one. – Database db = DatabaseFactory.openOrCreate(uri); You can also create an encrypted database by specifying security options in Create() or openOrCreate() method.

How to Create a Plain Database (Cont.) DatabaseFactory: –Creates new or open existing instances of a device database –Databases can be created temporary (in-memory) or persistent depending on the URI path –URI path can be one of the DB locations presented in the previous slides. Example: create a database in device memory: –URI path: /store/home/user Create a database in system memory: –URI path: /system/

Example- Open or Create a Database public void createPlainDatabaseFile() throws Exception { // The database is created in SD card String dbLocation = "/SDCard/databases/myApplication/"; //if you want to create the database in flash memory use the following URI // dbLocation = "/store/home/user/"; //Create a URI path file for the database URI uri = URI.create(dbLocation + “myDB.db”); // Invoke the create() or openOrCreate() method Database db = DatabaseFactory.openOrCreate(uri, new DatabaseSecurityOptions(false)) }

Encrypted Databases A database can be encrypted by specifying the security options in Creat() or openOrCreate() method: –DatabaseFactory.create(URI fileURI, DatabaseSecurityOptions securityOptions); –DatabaseFactory.openOrCreate(URI fileURI, DatabaseSecurityOptions securityOptions);

Encrypted Databases (Cont.) When a database is encrypted, it is linked to the device in which it is created. So, it does not support portability between databases. In order to transfer the encrypted database to the other device, you have to decrypt it first. An encrypted database should be closed as soon as possible, otherwise, it is susceptible to “cold boot attack”.

DatabaseSecurity Options DatabaseSecurityOptions defines the security options for a database. There are three choices available: –Not encrypted, accessible from any application –Encrypted, accessible from any application –Encrypted and protected, accessible only from applications that are signed with code signed key

DatabaseSecurity Options (Cont.) If two applications are signed with the same key, then both applications have access to the database. Methods: –Public DatabaseSecurityOptions(boolean encrypted) If encrypted is true, it means that the database is encrypted and not portable –Public DatabaseSecurityOptions(CodeSigningKey key) –public DatabaseSecurityOptions(int signerId)

Example- Encrypted Database It opens or creates an encrypted database file. It is not portable between devices. public void createEncryptedDatabaseFile() throws Exception { // The database is created in SD card String dbLocation = "/SDCard/databases/myApplication/"; //Create a URI path file for the database URI uri = URI.create(dbLocation + “myDB.db”); //Specify security options to encrypt the database DatabaseSecurityOptions dso = new DatabaseSecurityOptions(true); // Invoke the create() or openOrCreate() method Database db = DatabaseFactory.openOrCreate(uri, dso); }

Database Class Database class allows to create, delete and execute SQL statements In order to create a SQL statement, invoke createStatement() method of Database class. –Statement createStatement(String sqlStatement) Creates a Statement in this database. The argument can contain multiple SQL Statements delimited by ‘;’

A statement lifecycle It represents a SQL statement. The lifecycle of a statement is: –Create a statement –Prepare statement –Bind –Query Execution or –Update Execution

Example- Create a Table Public void createTable() throws Exception{ String dbLocation = "/SDCard/databases/myApplication/"; //Create a URI path file for the database URI uri = URI.create(dbLocation + “myDB.db”); Database db = DatabaseFactory.open(uri); //Create a statement Statement st = db.createStatement( “CREATE TABLE ‘Student’ ( ” + “ ‘Name’ TEXT,” + “ ‘ID’ INTEGER )”); st.prepare(); st.execute(); }

Bind() Method If the SQL statement has parameters, you have to invoke bind() method to bind the parameter to a value. Statement.bind() has two arguments: the first argument is the parameter number and the second parameter is the value to bound to it. The occurance of ? in a statement means parameters are numbered sequentially.

Bind() Method (Cont.) The occurance ?NN in a SQL statement means that each parameter is numbered as integer NN. Example: Statement s = “SELECT * FROM Employee WHERE salary ?10”; Means that: bind(2, 80000);// means salary <80000 bind(10, 50000);// means salary >50000

Example-Bind() method Public void insertDataIntoTable() throws Exception{ String dbLocation = "/SDCard/databases/myApplication/"; URI uri = URI.create(dbLocation + “myDB.db”); Database db = DatabaseFactory.open(uri); Statement st = db.createStatement( “ INSERT INTO Employee VALUES(?, ?, ?)”); st.prepare(); st.bind(1, employeeID); st.bind(2, name); st.bind(3, salary); st.execute(); }

Query Execution Query execution is done using getCursor() method. getCursor() executes only SELECT queries and returns cursor with selected rows. The returned cursor is positioned before the first row. Hence, to get the current row, invoke Cursor.first() or Cursor.next() method. The method’s signature: Cursor getCursor()

Cursor Class Cursor provides read-only access to results returned by getCursor() method It is an iterator. It provides one-way forward- only navigation method. Methods: –getRow(): returns current row with values –next(): move the cursor to the next row –prev(): move cursor to the previous row –isEmpty(): determines whether cursor has rows –close(): closes cursor object

Example- Cursor Class Public void selectDataTable() throws Exception{ URI uri = URI.create(" /SDCard/databases/myApplication/" + “ myDB.db ”); Database db = DatabaseFactory.open(uri); Statement st = db.createStatement( “SELECT * FROM Employee ”); st.prepare(); Cursor cursor = st.getCursor(); while(cursor.next()) { row = cursor.getRow(); id = row.getInteger(0); name = row.getString(1); salary = tow.getInteger(2); } st.close(); cursor.close(); }

Update Execution To update execution, invoke execute() method Statement.execute() executes an update statement. It does not return a result set. If the query statement is NOT SELECT type, invoke this method. You can use for any other SQL statement like INSERT, DELETE, CREATE, UPDATE and so on.

Example – Execute() method Public void deleteTable() throws Exception{ String dbLocation = "/SDCard/databases/myApplication/"; URI uri = URI.create(dbLocation + “myDB.db”); Database db = DatabaseFactory.open(uri); Statement st = db.createStatement( “DELETE Employee”); st.prepare(); st.execute(); }

Reset() method Statement. reset() method resets the statement to its state after prepare() method. It also clears all bindings. After invoking reset() method, execute() must be called to het a new cursor. So, if several SQL statements are run, invoke reset() method after each execute() mthod to clear bindings and clear the statement state.

Example- reset() Method Public void insertDataTable() throws Exception{ URI uri = URI.create("/SDCard/databases/myApplication/" + “myDB.db”); Database db = DatabaseFactory.open(uri); Statement st = db.createStatement(“ INSERT INTO Employee VALUES(?, ?) ”); st.prepare(); while(count <10) { st.bind(1,count); st.bind(2, employee[count]); st.execute(); st.reset(); count++; } st.close(); }

Transactions In order to execute multiple statements in one transaction, two methods should be called: –beginTransaction(): Starts a new transaction. So, all of the following statement executions can be committed or rolled back. –commitTransaction() Commits current transaction starting with the beginTransaction(). Before committing the transaction, all the cursors started within this transaction should be closed otherwise, the transaction is failed. Nested transactions are not supported.

Example- Transaction public void transactionStatement() { URI uri = URI.create("/SDCard/databases/myApplication/" + “myDB.db”); Database db = DatabaseFactory.open(uri); db.beginTransaction(); Statement st = db.createStatement( “INSERT INTO Employee VALUES(1, ‘Bob’ ) ”); st.prepare(); st.execute(); st.reset(); Statement st = db.createStatement( “INSERT INTO Employee VALUES(2, ‘John’ ) ”); st.prepare(); st.execute(); db.commitTransaction(); }

Delete a Database Invoke DatabaseFactory.delete() to remove an exisiting database from the device. The method’s signature: public static void delete( URI fileURI) Example: URI uri = URI.create("/SDCard/databases/myApplication/" + “myDB.db”); //Remove the database from the device DatabaseFactory.delete(uri);

Vacuum Command Database is stored as a file. Some operations such as dropping a table, inserting or deleting data, cause the file to be fragmented. Vacuum command is used to defragment and reduce the size of the database file. To defragment the file, Vacuum command copies all pieces of the file into the memory and creates a new database file.

Vacuum Command (Cont.) Because Vacuum copies all information related to the database into the memory, it needs enough memory. If enough memory is not available, the Vacuum command is failed.

Best Practices In order to have a better performance when developing a database application, consider the following: –Use local variables –Use primitive types instead of classes –Use static variables instead of Strings –Use efficient algorithms –Write efficient loops –Avoid Java.util.Enumeration

Best Practices (Cont.) –Make classes final Using final keyword means that it would never be extended. –Use int instead of long long is a 64-bit integer –Avoid unnecessary field initialization Explicitly initialize local variables –Use temporary tables as much as possible It takes less recourses.

Best Practices (Cont.) –Store data as little as possible The search is faster. –Avoid subqueries Because the result is stored in a temporary file –Use static inner class Reduce the number of references

References 1.BlackBerry Java Application SQLite Version. 5.0 (Development Guide) Available online at