Database Rung-Hung Gau Department of Computer Science and Engineering

Slides:



Advertisements
Similar presentations
Programming with Android: Data management
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
Introduction to Structured Query Language (SQL)
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.
Introduction to Structured Query Language (SQL)
1 JDBC Java Database Connectivity. 2 c.pdf
SQL Data Definition II Stanislava Armstrong 1SQL Data Definition II.
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
SQLLite and Java CS-328 Dick Steflik. SQLLite Embedded RDBMS ACID Compliant Size – about 257 Kbytes Not a client/server architecture –Accessed via function.
CS378 - Mobile Computing Persistence - SQLite. Databases RDBMS – relational data base management system Relational databases introduced by E. F. Codd.
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
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.
Introduction to SQL Structured Query Language Martin Egerhill.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
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.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
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.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
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.
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.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
1 CS 430 Database Theory Winter 2005 Lecture 13: SQL DML - Modifying Data.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
CHAPTER 9 File Storage Shared Preferences SQLite.
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
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.
CS371m - Mobile Computing Persistence - SQLite. 2 In case you have not taken 347: Data Management or worked with databases as part of a job, internship,
Making content providers
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Data Storage: Part 3 (SQLite)
Cosc 5/4730 Sqlite primer.
More SQL: Complex Queries,
Android Content Providers & SQLite
Mobile Applications (Android Programming)
SQLite in Android Landon Cox March 2, 2017.
Relational Database Design
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Android Application SQLite 1.
Mobile Computing With Android ACST Android Database Storage Part 2
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
CMPE419 Mobile Application Development
CS371m - Mobile Computing Persistence - SQLite.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter 8 Advanced SQL.
Android Developer Fundamentals V2
ListView A view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view. ListAdapter is used to.
Department of School of Computing and Engineering
SQLLite and Android.
Mobile Programming Dr. Mohsin Ali Memon.
Presentation transcript:

Database Rung-Hung Gau Department of Computer Science and Engineering National Sun Yat-Sen University Kaohsiung, Taiwan

Outline SQLite Create a Database Create a Table Making Data Retrieving Data

SQLite Android uses SQLite, which is a very popular embedded database. SQLite combines a clean SQL interface with a very small memory footprint and decent speed. SQLite is public domain. Lots of firms (Adobe, Apple, Google, Sun, Symbian) and open source projects (Mozilla, PHP, Python) all ship products with SQLite.

SQLite The native API of SQLite is not JDBC. JDBC might be too much overhead for a memory-limited device like a phone Activities will typically access a database via a content provider or service.

SQLite SQLite uses a dialect of SQL for queries (SELECT), data manipulation (INSERT, et. al.), and data definition (CREATE TABLE, et. al.). SQLite has a few places where it deviates from the SQL- 92 standard, no different than most SQL databases. SQLite is so space-efficient that the Android runtime can include all of SQLite.

SQLite The biggest difference from other SQL databases you will encounter is probably the data typing. You can put whatever data you want in whatever column you want. You can put a string in an INTEGER column and vice versa. SQLite refers to this as "manifest typing“. There are some standard SQL features not supported in SQLite: FOREIGN KEY constraints, nested transactions, RIGHT OUTER JOIN and FULL OUTER JOIN, and some flavors of ALTER TABLE.

SQLite The biggest difference from other SQL databases you will encounter is probably the data typing. You can put whatever data you want in whatever column you want. You can put a string in an INTEGER column and vice versa. SQLite refers to this as "manifest typing“. There are some standard SQL features not supported in SQLite: FOREIGN KEY constraints, nested transactions, RIGHT OUTER JOIN and FULL OUTER JOIN, and some flavors of ALTER TABLE.

SQLite No databases are automatically supplied to you by Android. If you want to use SQLite, you have to create your own database, then populate it with your own tables, indexes, and data. To create and open a database, your best option is to craft a subclass of SQLiteOpenHelper. This class wraps up the logic to create and upgrade a database for your application.

SQLite Your subclass of SQLiteOpenHelper will need three methods: The constructor, which chains upward to the SQLiteOpenHelper constructor. onCreate(), which passes you a SQLiteDatabase object that you need to populate with tables and initial data. onUpgrade(), which passes you a SQLiteDatabase object and the old and new version numbers, so you can figure out how best to convert the database from the old schema to the new one.

SQLiteOpenHelper Constructor The constructor takes the Context (e.g., an Activity), the name of the database, an optional cursor factory (typically, just pass null), and an integer representing the version of the database schema you are using.

SQLiteOpenHelper onUpgrade() It passes you a SQLiteDatabase object and the old and new version numbers, so you can figure out how best to convert the database from the old schema to the new one. The simplest, albeit least friendly, approach is to simply drop the old tables and create new ones.

Creating a Database To use your SQLiteOpenHelper subclass, create an instance and ask it to getReadableDatabase() or getWriteableDatabase(), depending upon whether or not you will be changing its contents: db=(new DatabaseHelper(getContext())).getWritableDat abase(); return (db == null) ? false : true; This will return a SQLiteDatabase instance, which you can then use to query the database, modify its data, or close the database.

Setting the Table For creating your tables and indexes, you will need to call execSQL() on your SQLiteDatabase, providing the DDL statement you wish to apply against the database. db.execSQL("CREATE TABLE constants (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, value REAL);"); This will create a table, named constants, with a primary key column named _id that is an auto-incremented integer (i.e., SQLite will assign the value for you when you insert rows), plus two data columns: title (text) and value (a float, or "real" in SQLite terms).

Setting the Table SQLite will automatically create an index for you on your primary key column – you could add other indices here via some CREATE INDEX statements. If you want to drop your tables or indexes, just use execSQL() to invoke DROP INDEX and DROP TABLE statements.

Making Data Given that you have a database and one or more tables, you have two major approaches for adding data into the database. Use execSQL(). Use the insert(), update(), and delete() methods on the SQLiteDatabase object. These methods are called builder methods.

Making Data The execSQL() method works for any SQL that does not return results, so it can handle INSERT, UPDATE, DELETE, etc. db.execSQL("INSERT INTO widgets (name, inventory)"+ "VALUES ('Sprocket', 5)");

Making Data The builder methods make use of ContentValues objects, which implement a Mapesque interface, albeit one that has additional methods for working with SQLite types. For example, in addition to get() to retrieve a value by its key, you have getAsInteger(), getAsString(), and so forth.

Making Data The insert() method takes the name of the table, the name of one column as the "null column hack", and a ContentValues with the initial values you want put into this row.

/* prepare the ContentValues object, similar to a HashTable (key, value) pairs */ ContentValues cv=new ContentValues(); cv.put(Constants.TITLE, "Gravity, Death Star I"); cv.put(Constants.VALUE, SensorManager.GRAVITY_DEATH_STAR_I); /* In the “constants” table, insert a row/record specified by the ContentValues object (denoted by “cv”) */ db.insert("constants", getNullColumnHack(), cv);

Making Data The "null column hack" is for the case where the ContentValues instance is empty – the column named as the "null column hack" will be explicitly assigned the value NULL in the SQL INSERT statement generated by insert().

Making Data The update() method takes the name of the table, a ContentValues representing the columns and replacement values to use, an optional WHERE clause, and an optional list of parameters to fill into the WHERE clause, to replace any embedded question marks (?).

String[] parms=new String[] {"snicklefritz"}; /* replacements is a ContentValues instance */ /* In the “widget” table, for each row with “name” equals “parms”, replace the values of the columns specified by “replacements” */ db.update("widgets", replacements, "name=?", parms);

Retrieving Data There are two major ways for retrieving data from a SQLite database using SELECT: You can use rawQuery() to invoke a SELECT statement directly, or You can use query() to build up a query from its component parts

Raw Queries Cursor c=db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='constants'", null); In the above statement, we query a SQLite system table (sqlite_master) to see if our constants table already exists. The return value is a Cursor, which contains methods for iterating over results.

Dynamic Queries Query could be dynamic!! For example, the set of columns you need to retrieve might not be known at compile time. rawQuery() does not work for dynamic queries.

Regular Queries The query() method takes the discrete pieces of a SELECT statement and builds the query from them. The pieces, in order that they appear as parameters to query(), are: The name of the table to query against The list of columns to retrieve The WHERE clause, optionally including positional parameters The list of values to substitute in for those positional parameters The GROUP BY clause, if any The ORDER BY clause, if any The HAVING clause, if any

Regular Queries String[] columns={"ID", "inventory"}; String[] parms={"snicklefritz"}; Cursor result=db.query("widgets", columns, "name=?", parms, null, null, null);

Retrieving Data SQLiteQueryBuilder offers much richer query-building options, particularly for queries involving things like the union of multiple sub-query results. A common pattern for your content provider's query() implementation is based on SQLiteQueryBuilder: create a SQLiteQueryBuilder, fill in some defaults, then allow it to build up (and optionally execute) the full query combining the defaults with what is provided to the content provider on the query request.

public Cursor query(Uri url, String[] projection, String selection, String[] selectionArgs, String sort) { SQLiteQueryBuilder qb=new SQLiteQueryBuilder(); qb.setTables(getTableName()); if (isCollectionUri(url)) { qb.setProjectionMap(getDefaultProjection()); } else { qb.appendWhere(getIdColumnName()+"="+url.getPat hSegments().get(1)); String orderBy; if (TextUtils.isEmpty(sort)) { orderBy=getDefaultSortOrder();

else { orderBy=sort; } Cursor c=qb.query(db, projection, selection, selectionArgs, null, null, orderBy); c.setNotificationUri(getContext().getContentResolver(), url); return c;

Dissecting the Program A SQLiteQueryBuilder is constructed It is told the table to use for the query (setTables(getTableName())) It is either told the default set of columns to return (setProjectionMap()), or is given a piece of a WHERE clause to identify a particular row in the table by an identifier extracted from the Uri supplied to the query() call (appendWhere()) Finally, it is told to execute the query. (qb.query(db, projection, selection, selectionArgs, null, null, orderBy))

Retrieving Data Instead of having the SQLiteQueryBuilder execute the query directly, we could have called buildQuery() to have it generate and return the SQL SELECT statement we needed, which we could then execute ourselves.

Using Cursors After you execute the query, you get back a Cursor, which is the Android/SQLite edition of the database cursor. With Cursor, you can Find out how many rows are in the result set via getCount() Iterate over the rows via moveToFirst(), moveToNext(), and isAfterLast() Find out the names of the columns via getColumnNames(), convert those into column numbers via getColumnIndex(), and get values for the current row for a given column via methods like getString(), getInt(), etc.

Using Cursors Re-execute the query that created the cursor via requery() Release the cursor's resources via close()

Cursor result= db.rawQuery("SELECT ID, name, inventory FROM widgets"); result.moveToFirst(); while (!result.isAfterLast()) { int id=result.getInt(0); String name=result.getString(1); int inventory=result.getInt(2); // do something useful with these result.moveToNext(); } result.close();

Making Your Own Cursors In Android, you can use your own Cursor subclass, rather than the stock implementation provided by Android. You can use queryWithFactory() and rawQueryWithFactory() that take a SQLiteDatabase.CursorFactory instance as a parameter. The factory is responsible for creating new cursors via its newCursor() implementation.

Tools to inspect and manipulate the contents of the database With the Android emulator, you can inspect and manipulate the contents of the database, beyond merely the database's API, by The sqlite3 program SQLite clients (e.g., the SQLite Manager extension for Firefox)