• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Day   4  android database tutorial - 15092012
 

Day 4 android database tutorial - 15092012

on

  • 425 views

 

Statistics

Views

Total Views
425
Views on SlideShare
425
Embed Views
0

Actions

Likes
0
Downloads
14
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Day   4  android database tutorial - 15092012 Day 4 android database tutorial - 15092012 Presentation Transcript

    • ANDROID DATABASE TUTORIAL Mr. Pritesh N. Patel Assistant Professor MCA ISTAR,V.V. Nagar Mr. Pritesh N. Patel 1
    • Storage Options  Android provides several options for you to save persistent application data. Your data storage options are the following: 1. Shared Preferences : Store private primitive data in key-value pairs. 2. Internal Storage : Store private data on the device memory. 3. External Storage : Store public data on the shared external storage. 4. SQLite Databases : Store structured data in a private database. 5. Network Connection : Store data on the web with your own network server. Mr. Pritesh N. Patel 2
    • Using SharedPreferencs  Preferences are typically name value pairs. They can be stored as “Shared Preferences” across various activities in an application (note currently it cannot be shared across processes).  The context object lets you retrieve SharedPreferences through the method Context.getSharedPreferences(). Mr. Pritesh N. Patel 3 Creation SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE); SharedPreferences.Editor prefsEditor = myPrefs.edit(); prefsEditor.putString(“Name”, “Pritesh"); prefsEditor.putString(“Desig”, “Professor"); prefsEditor.commit(); Retrival SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE); String prefName = myPrefs.getString(“Name”, "nothing"); String wallPaper = myPrefs.getString(“Desig”, null);
    • Introduction  Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very light weight database which comes with Android OS.  Here we will see how to create and manage database using SQLite Database. Things to consider when dealing with SQLite: 1. Data type integrity is not maintained in SQLite, you can put a value of a certain data type in a column of another datatype (put string in an integer and vice versa). 2. Referential integrity is not maintained in SQLite, there is no FOREIGN KEY constraints or JOIN statements. 3. SQLite Full Unicode support is optional and not installed by default. Mr. Pritesh N. Patel 4
    • Database Structure Field Name Type empid Integer ( Primary Key ) empname Text Database Name: demoDB Table Name: tblemployees Mr. Pritesh N. Patel 5
    • Creating Database and Tables  By default, SQLite on Android does not have a management interface or an application to create and manage databases from, so we're going to create the database ourselves by code. static final String dbName="demoDB"; static final String employeeTable="Employees"; static final String colID="EmpId"; static final String colName="EmpName"; Mr. Pritesh N. Patel 6
    • Demo – Database and Table private SQliteDatabase db; db = openOrCreateDatabase(“demodb”, SQLiteDatabase.CREATE_IF_NECESSARY, null); db.setLocale(Locale.getDefault()); db.setLockingEnabled(true); db.setVersion(1); db.execSQL(“CREATE TABLE employee( empid INTEGER PRIMARY KEY, empnameTEXT)”); Mr. Pritesh N. Patel 7
    • Insert Operation Syntax insert(String tableName, String nullColumnHack, ContentValues values); //GetValues from control ContentValues cv = new ContentValues(); cv.put(“empid”,txteno.getText().toString()); cv.put(“empname”,txtenm.getText().toString()); long count = db.insert(“employee”, null, cv); Mr. Pritesh N. Patel 8
    • Update Operation Syntax update(String tableName, ContentValues values, String whereClause, String[]whereArgs); //GetValues from control ContentValues cv = new ContentValues(); cv.put(“empname”,txtenm.getText().toString()); long count = db.update(“employee”, cv,”empid=?”, new String[] {txteno.getText().toString()} ); Mr. Pritesh N. Patel 9
    • Delete Operation Syntax delete(String tableName, String whereClause, String[] whereArgs); //GetValues from control long count = db.delete(“employee”,”empid=?”, new String[] {txteno.getText().toString()} ); Mr. Pritesh N. Patel 10
    • Select Operation Syntax query(String tableName, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy); //GetValues from control Cursor c = db.query(“employee”,null, null, null, null, null, null); c.moveToFirst(); while(c.isAfterLast()) {Toast.makeText(getApplicationContext(), "*" + c.getString(0) + "|" + c.getString(1),Toast.LENGTH_SHORT).show(); c.moveToNext();} Mr. Pritesh N. Patel 11
    • Thank You Mr. Pritesh N. Patel 12 pritam.world@gmail.com +91 972-634-4355 http://priteshpatel.co.in