CS499 – Mobile Application Development

Slides:



Advertisements
Similar presentations
Programming with Android: Data management
Advertisements

CE881: Mobile and Social Application Programming Flexible Data Handling with Integrity: SQLite Simon M. Lucas.
SQLite is a software library. It is: self-contained + Serverless + zero-configuration transactional = SQL database engine. Most widely deployed. The source.
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.
Single Application Persistent Data Storage.  Files  SharedPreferences  SQLite database.
ContentProviders.  Databases for reading & writing data  Support typical database operations  e.g., query, insert, update & delete.
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)
CSE 486/586, Spring 2013 CSE 486/586 Distributed Systems Content Providers & Services.
Content providers Accessing shared data in a uniform way 1Content providers.
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.
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal.
SQLite Android Club SQLite About onCreate Insert Select Update Delete onUpdate.
Android Storage. There are several options for storage of data with Android We can put data into a preferences file. We can put data into a ‘normal’ file.
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.
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.
Address Book App 1 Fall 2014 CS7020: Game Design and Development.
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,
Content Providers.
Making content providers
Data Storage: Part 3 (SQLite)
Content provider.
Cosc 5/4730 Sqlite primer.
Android Content Providers & SQLite
Mobile Applications (Android Programming)
Data Storage: Part 4 (Content Providers)
Content Providers And Content Resolvers
SQL – Python and Databases
Data Storage: Part 2 (File System)
Android Application Data Storage 1.
SQLite in Android Landon Cox March 2, 2017.
Mobile Software Development for Android - I397
Mobile Application Development BSCS-7 Lecture # 18, 19
Android Application SQLite 1.
Reactive Android Development
CS499 – Mobile Application Development
Android Database using SQLite
Mobile Application Development Chapter 5 [Persistent Data in Android]
Android Storage.
Mobile Computing With Android ACST Android Database Storage Part 2
CMPE419 Mobile Application Development
CS371m - Mobile Computing Persistence - SQLite.
CMPE419 Mobile Application Development
Mobile Computing With Android ACST 4550 Android Database Storage
Android Developer Fundamentals V2
Web Design & Development Lecture 8
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.
Lecture 8: Database Topics: Basic SQLite Operations.
Mobile Programming Dr. Mohsin Ali Memon.
Presentation transcript:

CS499 – Mobile Application Development Fall 2013 Programming the Android Platform Data Management

Data Management Files (internal/external) SQLite database SharedPreferences (not discussed but a couple of examples included online)

File Represents a file system entity identified by a pathname Classified as internal or external Internal memory (on the device) usually used for application private files External memory (removable media) used for public files Cache files – temporary files Examples: DataManagementFileInternalMemory DataManagementFileExternalMemory

File API boolean isDIrectory() String getAbsolutePath() return true if this file represents a directory String getAbsolutePath() returns the absolute path to this file boolean setReadable(boolean readable) sets read permission on this file MANY others – see documentation

Writing an Internal Memory File // Open file with ContextWrapper.openFileOutput() FileOutputStream fos = openFileOutput(filename,MODE_PRIVATE); PrintWriter pw = new PrintWriter( new BufferedWriter( new OutputStreamWriter(fos))); // Write to file pw.println(…); //Close file pw.close();

Reading an Internal Memory File // Open file with ContextWrapper.openFileOutput() FileInputStream fis = openFileOutput(filename); PrintWriter fr = new PrintWriter( new BufferedReader( new InputStreamReader(fis))); // Read from file while (null != (line=fr.readLine())) { // process data } //Close file fr.close();

External Memory Files Removable media may appear/disappear without warning String Environment.getExternalStorageState() MEDIA_MOUNTED – present & mounted with read/write access MEDIA_MOUNTED_READ_ONLY MEDIA_REMOVED – not present Need permission to write external files in AndroidManifest.xml: <uses-permission android:name= “android.permission.WRITE_EXTERNAL_STORAGE” />

Writing an External Memory File public class FileWriteAndReadActivity extends Activity { public void onCreate(Bundle savedState) { … if (Environment.MEDIA_MOUNTED.equals( Environment.getExternalStorageState())) { File outFile = new File(getExternalFilesDir( Environment.DIRECTORY_PICTURES),fileName); try { BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(outFile)); BufferedInputStream is = new BufferedInputStream(getResources() .openRawResource(R.drawable.icon)); copy(is,os); } catch (FileNotFoundException e) {} }

Writing an External Memory File private void copy(InputStream is, OutputStream os) { final byte[] buf = new byte[1024]; int numBytes; try { while (-1 != (numBytes = is.read(buf))) { os.write(buf,0,numBytes); } } catch (IOException e) { … } } finally { is.close(); os.close(); } catch (IOException e) {} …

SQLite SQLite provides in-memory database available to the app that created it Designed to operate within a very small footprint Implements most of SQL92 Supports ACID transactions ACID: atomic, consistent, isolated & durable Will need to use a content provider to make data available to rest of the system.

Databases This is not a class in databases (CS450) and you aren’t required to know about databases to use SQLite Data organized in tables where each row will hold a single element that we are interested in. Each element has a unique identifier called the key each column is a field of the given elements in the table

Example: GradesDB GNumber LastName FirstName Exam1grade Exam2grade Smith Bob 80 G00000001 Jones Davey 70 90 G01010101 White Betty 85 G87654321 Doe Jane 20 100 G99999999 John 50 G88888888

SQL – Structured Query Language Standard way to make database requests (creation, queries, insertion, deletion) Based on relational algebra and tuple relational calculus Fairly standardized SELECT LastName, FirstName FROM GradesDB WHERE Gnumber = G99999999 In Android, requests formulated as method calls with multiple parameters, but has the notation at the core.

Opening a Database Recommended method relies on a helper class called SQLiteOpenHelper Create a subclass SQLiteOpenHelper Override onCreate() Execute CREATE TABLE command Use Constructor to instantiate subclass Use SQLiteOpenHelper methods to open & return underlying database

Opening a Database public class DatabaseOpenHelper extends SQLiteOpenHelper { final private static String CREATE_CMD = “CREATE TABLE artists(“ + “_id “+”INTEGER PRIMARY KEY AUTOINCREMENT, “ + “name “+”TEXT NOT NULL)”; public DatabaseOpenHelper(Context context) { super(context,”artist_db”,null,1); } public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_CMD); …

Using a Database public class DatabaseExampleActivity extends ListActivity { final static String[] columns={“_id”, “name”}; static SQLiteDatabase db = null; public void onCreate(Bundle savedState) { … DatabaseOpenHelper dbHelper = new DatabaseOpenHelper(); db = dbHelper.getWritableDatabase(); insertArtists(); Cursor c = readArtists(); deleteLadyGaga(); setListAdapter(new SimpleCursorAdapter( this, R.layout.list_layout,c,columns, new int[]{R.id._id,R.id.name})); }

Insertion private void insertArtists() { ContentValues values = new ContentValues(); values.put(“name”,”Lady Gaga”); db.insert(“artists”,null,values); values.clear(); values.put(“name”,”Johnny Cash”); values.put(“name”,”Ludwig von Beethoven”); }

Deletion & Querying private int deleteLadyGaga() { return db.delete(“artists”,”name =?”, new String[]{“Lady Gaga”}); } private Cursor readArtists() { // SELECT * from artists // i.e. return all rows that match return db.query(“artists”, new String[]{“_id”,”name”}, null, new String[]{},null,null,null);

Querying public Cursor query(String table, String columns[], String selection, String selectionArgs, String groupBy, String having, String orderBy, String limit) The parameters are designed to allow queries with the full power of SQL

Query Examples Return all rows where there value matches a given parameter String[] COLUMNS = new STRING{“FirstName”,”LastName”}; String selection = “Gnumber = ?”; String[] args = {“G99999999”}; Cursor result = db.query(“GradesDB”,COLUMNS, selection, args,null,null,null,null); Return rows where some field has a particular range String selection = “Exam1Grade < ? AND Exam2Grade < ?”; String[] args = {“70”,”70”};

Cursor class  Provides random read-write access to the result set returned by a database query http://developer.android.com/reference/android/database/Cursor.html moveToFirst(), moveToNext(), moveToPosition(int) – iterate through getString(int index), getInt(int index) – column value associated with a particular row CursorAdapter lets you put info into a ListView

Upgrading

Content Providers

Examining Databases Databases stored in /data/data/<package name>/databases Can examine database with sqlite3