Friday, July 12, 2013

SQLIteDatabase in Android

SQLiteDatabase

In this tutorial I will describe about using/creating database in Android.


See the post    Using,Creating,Updating DataBase In Android   for Running Example of Using SQLiteDatabase in Android. The example describes how to create table, Insert rows in Table, delete rows from table, Updating Table etc with Example.

SQLiteDatabase
•  is an Open Source Database which is embedded into Android.
•  supports standard relational database features like SQL syntax.
•  requires only little memory at runtime .
•   supports all data types like int, string, long etc

Using SQLite database


•  does not require any database setup or administration.
•  need define the SQL statements for creating and updating the database. Afterwards the database is automatically managed for you by the Android platform.
• database is by default saved in the directory DATA/data/APP_NAME


Creating DataBase


• Use SQLiteOpenHelper
• need to override the onCreate() and onUpgrade() methods.
• onCreate() is called by the Android framework, to create the database if the database does not exists.
• onUpgrade() is called, if the database version is increased in your application code.
• provides the methods getReadableDatabase() and getWriteableDatabase()


Sample Code
public class DataBaseHelper extends SQLiteOpenHelper
 {  
          String DATABASE_CREATE = "create table "+"LOGIN"+  "( " +"ID"+" integer primary key              autoincrement,"+ "USERNAME text,PASSWORD text); ";
        @Override
         public void        onCreate(SQLiteDatabase db)  
         {     
                    db.execSQL(DATABASE_CREATE); 
         } 
       @Override
        public void onUpgrade(SQLiteDatabase db, int _oldVersion, int _newVersion)
        {
                   db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE"); 
                   onCreate( db); 
        } 
}

Accessing DataBase/Table


•By rawQueay() method call
       Ex:  Cursor cursor = getReadableDatabase(). rawQuery("select * from tablename where _id = ?", new String[] { id });
•By query() mrthod call 
       Ex:  return database.query(DATABASE_TABLE, new String[] { “Column1”,”Column2”}, null, null, null, null, null);

Parameters of query() method

  1. String TableName: The name of Table
  2. String[] columnNames: A list of which table columns to return. Passing "null" will return all columns.
  3. String whereClause: Where-clause, i.e. filter for the selection of data, null will select all data.
  4. String[] selectionArgs: You may include ?s in the "whereClause"". These placeholders will get replaced by the values from the selectionArgs array.
  5. String[] groupBy: how to group rows, null will cause the rows to not be grouped.
  6. String[] having: Filter for the groups, null means no filter.
  7. String[] orderBy: Table columns which will be used to order the data, null means no ordering. Cursor

Cursor

•  A query returns a Cursor object.
•  A Cursor represents the result of a query and basically points to one row of the query result.
getCount() : returns  Number of rows
•  moveToFirst() : moves the cursor  to first row of returned result
•  moveToNext() : moves the cursor  to next row of returned result
•  moveToPosition(int position) : moves the cursor  to particular row of returned result



See the post    Using,Creating,Updating DataBase In Android   for Running Example of Using SQLiteDatabase in Android. The example describes how to create table, Insert rows in Table, delete rows from table, Updating Table etc with Example.




More Topics:

New Advance Topics:                   Android LiveWallpaer Tutorial
Android ImageSwitcher                    Android TextSwitcher                                Android ViewFlipper
Android Gesture Detector               Handling/Detecting Swap Events                Gradient Drawable
Detecting Missed Calls                    Hide Title Bar                                           GridView Animation
Android AlarmManager                 Android BootReceiver                       Vibrate Phone In a Desirable Pattern    
Developing for Different Screen Sizes           Showing Toast for Longer Time       Publishing your App
How to publish Android App on Google Play
Android TextWatcher

 Beginning With Android
      Android : Introduction(What is Android)                                                              Configuring Eclipse for Android Development
     Creating Your First Android Project                                           Understanding Android Manifest File of your android app

 Advance Android Topics                                                              Customizing Android Views


Working With Layouts                                                                Working With Views

Understanding Layouts in Android                                                   Using Buttons and EditText in Android
Working with Linear Layout (With Example)                                     Using CheckBoxes in Android
Nested Linear Layout (With Example)                                              Using AutoCompleteTextView in Android                                                                                          Grid View
Relative Layout In Android                                                               ListView
Table Layout                                                                                   Android ProgressBar
Frame Layout(With Example)                                                          Customizing ProgressBar
Absolute Layout                                                                             Customizing Radio Buttons
Grid Layout                                                                                    Customizing Checkboxes In Android

Android Components                                                                 Dialogs In Android

Activity In Android                                                                    Working With Alert Dialog
Activity Life Cycle                                                                    Adding Radio Buttons In Dialog
Starting Activity For Result                                                       Adding Check Boxes In Dialog
Sending Data from One Activity to Other in Android                    Creating Customized Dialogs in Android
Returning Result from Activity                                                   Creating Dialog To Collect User Input
Android : Service                                                                     DatePicker and TimePickerDialog
BroadcastReceiver                                                                   Using TimePickerDialog and DatePickerDialog In android

Menus In Android                                                                ListView:
Creating Option Menu                                                               Populating ListView With DataBase
Creating Context Menu In Android                                              Populating ListView with ArrayList
                                                                                               ListView with Custom Adapter

Toast                                                                                      Working With SMS
Customizing Toast In Android                                                       How to Send SMS in Android
Customizing the Display Time of Toast                                        How To Receive SMS
Customizing Toast At Runtime                                                  Accessing Inbox In Android
Adding Image in Toast
Showing Toast for Longer Time


TelephonyManager                                                            Storage: Storing Data In Android
Using Telephony Manager In Android                                          SharedPreferences In Android
                                                                                              Reading and Writing files to Internal Stoarage

Working With Incoming Calls                                       DataBase :  Introduction of SQLiteDataBase
How To Handle Incoming Calls in Android                                Working With Database in Android
How to Forward an Incoming Call In Android                            Creating Table In Android
CALL States In Android                                                          Inserting, Deleting and Updating Records In Table in Android


Miscellaneous
Notifications In Android
How To Vibrate The Android Phone
Sending Email In Android
Opening a webpage In Browser
How to Access PhoneBook In Android
Prompt User Input with an AlertDialog









2 comments: