Mobile Computing With Android ACST Android Database Storage Part 2

Slides:



Advertisements
Similar presentations
Programming with Android: Data management
Advertisements

SQLite in Mobile Apps by Dan Youberg. Overview Many well known applications and Internet browsers use SQLite due to its very small size (~250 Kb). Also.
Cosc 5/4730 Android and Blackberry SQLite. For the sql language syntax, please see SQlite documentation –
SQLLite and Java CS-328 Dick Steflik. SQLLite Embedded RDBMS ACID Compliant Size – about 257 Kbytes Not a client/server architecture –Accessed via function.
SQLite 1 CS440. What is SQLite?  Open Source Database embedded in Android  SQL syntax  Requires small memory at runtime (250 Kbytes)  Lightweight.
CS378 - Mobile Computing Persistence - SQLite. Databases RDBMS – relational data base management system Relational databases introduced by E. F. Codd.
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.
Database Rung-Hung Gau Department of Computer Science and Engineering
Data Storage: Part 3 (SQLite)
LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.
 Mysql – popular open-source database management system  PHP usually works with Mysql for web- based database applications  LAMP applications—Web-based.
Data Storage: Part 4 (Content Providers). Content Providers Content providers allow the sharing of data between applications. Inter-process communication.
COMP 365 Android Development.  Manages access from a central database  Allows multiple applications to access the same data.
SQLite Android Club SQLite About onCreate Insert Select Update Delete onUpdate.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Address Book App 1. Define styles   Specify a background for a TextView – res/drawable/textview_border.xml.
9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
PHP and Mysql Database. PHP and Database Mysql – popular open-source database management system PHP usually works with Mysql for web-based database applications.
Persistence Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
SQLite (part deux) 1 CS440. Traditional Model View Controller (MVC) CS440 2.
Mobile Software Development ISCG 7424 Department of Computing UNITEC John Casey and Richard Rabeder SQLite and Permissions.
SQLite DB Storing Data in Android RAVI GAURAV PANDEY 1.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
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.
Address Book App 1 Fall 2014 CS7020: Game Design and Development.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
By: Eliav Menachi.  On Android, all application data (including files) are private to that application  Android provides a standard way for an application.
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
Data Storage in Android Димитър Н. Димитров. Why talk about data? Why not 3D graphics or network connectivity? Data as fundamental term in computer science.
 MySQL is a database system used on the web  MySQL is a database system that runs on a server  MySQL is ideal for both small and large applications.
CS499 – Mobile Application Development
Making content providers
Data Storage: Part 3 (SQLite)
Cosc 5/4730 Sqlite primer.
Android Content Providers & SQLite
Mobile Applications (Android Programming)
SQLite in Android Landon Cox March 2, 2017.
Mobile Software Development for Android - I397
Mobile Application Development BSCS-7 Lecture # 18, 19
Mobile Applications (Android Programming)
Reactive Android Development
Android Database using SQLite
Introduction to Web programming
Introduction To Codeigniter
ISC440: Web Programming 2 Server-side Scripting PHP 3
SQL Tutorial.
CMPE419 Mobile Application Development
Introduction To Structured Query Language (SQL)
CMPE419 Mobile Application Development
HAVING,INDEX,COMMIT & ROLLBACK
Mobile Computing With Android ACST 4550 Android Database Storage
Introduction To Structured Query Language (SQL)
Android Developer Fundamentals V2
Updating Databases With Open SQL
Department of School of Computing and Engineering
SQLLite and Android.
MySQL Database System Installation Overview SQL summary
Database SQL.
Lecture 8: Database Topics: Basic SQLite Operations.
Mobile Programming Dr. Mohsin Ali Memon.
Introduction to Web programming
Trainer: Bach Ngoc Toan– TEDU Website:
Updating Databases With Open SQL
Presentation transcript:

Mobile Computing With Android ACST 4550 Android Database Storage Part 2

Creating a Database You use the SQLiteOpenHelper class to create or open a database. You do this by creating a Java class that extends the SQLiteOpenHelper class. Then in that classes constructor you pass the name to the parent constructor: super(context, “DbName”, null, DB_VERSION); This creates the database if it does not exist already. Then in the onCreate() method, you create the database table like so: db.execSQL("CREATE TABLE " + TableName + " (“ + columNames + “)”); The above text is a standard SQL statement for creating the table and identifying the features of each of the column/field.

Creating a Database Next, you call getWritableDatabase() for a database you can modify, or getReadableDatabase() for a read-only database. Both of these calls returns an SQLiteDatabase object. SQLiteDatabase db = getWritableDatabase(); This opens the database, and to close it you just call the close() method.

Working with Databases The methods for modifying your SQLiteDatabase table are as follow: db.insert() <= inserts a new record into a table db.update() <= updates an existing record in a table db.delete() <= deletes an existing record in a table db.query() <= queries the database and returns a list of records The first argument to each of these methods is a string which is the name of the table, like “AddressBook” If you need to provide one or more column name/value pairs, you use the ContentValues class, giving a column name and value using put(): ContentValues values = new ContentValues(); values.put(“ColumnName1”, val1); values.put(“ColumnName2”, val2);

Working with Databases db.insert db.insert(“TableName”,nullableColumn,ContentValues); The first argument is the name of the table. The second argument is a parameter that provides the name of a nullable column name to explicitly insert a NULL into if a null ContentValue name/value pairs is given. The last argument is the set of name/value pairs in a ContentValues object. For example the following inserts a row into the Addressbook table where ID is 456 and the LastName is “Smith”: ContentValues values = new ContentValues(); values.put(“ID”, 456); values.put(“LastName”, “Smith”); db.insert(“AddressBook”, null, values);

Working with Databases db.update db.update(“TableName”,ContentValues,whereClause,whereargs); The first argument is the name of the table. The second argument is the set of name/value pairs in a ContentValues object. The third argument is a Where clause. The fourth argument is an arguments string array for the Where clause. For example the following finds records where ID is 455 or 457 and sets the LastNames of those records to “Smith”: ContentValues values = new ContentValues(); values.put(“LastName”, “Smith”); String wherestr = “ID=? OR ID=?”; String[] argstrs = { “455”, “457” }; db.update(“AddressBook”, values, wherestr, argstrs);

Working with Databases db.delete: db.delete(“TableName”,whereClause,whereArgs); The first argument is the name of the table. The second argument is a Where clause. The third argument is an arguments string array for the Where clause. For example the following deletes records where ID is 455 or 457: String wherestr = “ID=? OR ID=?”; String[] argstrs = { “455”, “457” }; db.delete(“AddressBook”, wherestr, argstrs);

Working with Databases db.query: Cursor c = db.query(“TableName”, ColumnNames, whereClause, whereArgs, groupBy, having, orderBy); The first argument is the table name. The second argument is a string array of the Column Name of the values to return. The third is a Where clause. The fourth is an arguments string array for the Where clause. The fifth is a GroupBy clause. The sixth is a Having clause. The seventh is a OrderBy clause. For example the following gets all IDs and LastNames where ID is 455 or 457, grouped by LastName, as long as no more than 5 LastNames in a group, and ordered by ID: String [] colnames = { “ID”, “LastName” }; String wherestr = “ID=? OR ID=?”; String [] argstrs = { “455”, “457” }; String havingstr = “count(ID) <= 5”; Cursor c = db.query(“AddressBook”, colnames, wherestr, argstrs, “LastName”, havingstr, “ID”);

Working with Databases db.query: Cursor c = db.query(“TableName”, ColumnNames, whereClause, whereArgs, groupBy, having, orderBy); The Cursor is the set of results returned by the query. You can get the amount of results with c.getCount(). Each row returned may have multiple columns of values. You get each by first getting a column index using he column name like so: c.getColumnIndex(String columnName). Then you use the index to get a value of a certain type like c.getLong(int columnIndex) if the value for that column is a long, or if it’s a string you’d use c.getString(int columnIndex). If you don’t have a Where clause, a GroupBy clause, a Having clause, or a OrderBy clause, you can pass nulls for any or all of those values. The following would just get all the IDs and LastNames for all the records: String [] colnames = { “ID”, “LastName” }; Cursor c = db.query(“AddressBook”, colnames, null, null, null, null, null);