Fundamentals
Introduction
1st Android Application

OOPS Concepts
Class
Object
Abstraction
Encapsulation

Polymorphism
Inheritance
Message Passing
Dynamic Binding

UI Controls
TextView
Button
Toggle Button
CheckBox
Radio Button
Spinner
ProgressBar
SeekBar
RadioGroup
RatingBar
Switch
EditText
AutoComplete
TextView

MultiAutoComplete
TextView

ImageView
ImageButton
Gallery
MediaPlayer
VideoView
MediaController

TimePicker
DatePicker
CalendarView
Chronometer
Analog Clock
Digital Clock

Layouts
LinearLayout
RelativeLayout
GridLayout
TableLayout
FrameLayout
Fragment

Composite
ListView
ExpandableListView
GridView
ScrollView
SearchView
Sliding Drawer
Tab Widget
WebView

Transitions
Image Switcher
StackView
TextSwitcher
ViewAnimator
View Flipper
View Switcher

Advanced
Request Focus
View
View Stub
TextureView
Gesture Overlay View

SurfaceView
NumberPicker
Zoom Button
Zoom Controls
AbsoluteLayout

Database
SharedPreference
SQLite DB
MySQL DB

Webservices
Check HttpURLConnection
Single Webservice Call
Multi Webservice Call
ListView from Webservice
SQLite from Webservice
Data from Database

Telephone Manager
IMEI
IMSI
Phone Type
Sim Type
Network Type

Content Providers
Contact Provider
BookMark Provider
Own Provider


Message & Networking
Send SMS
SMS with Reports
Group SMS

Send E-Mail
Download Binary Data


Location Based Service
Display Google Map
Events with Google Map

Android Services
Create Service
Create Longtime Service

Asynchronous Task
Repeating Services
IntentService

Activity from a Service
Bind Activity to Service


Sample Projects
HMS
CNB
Login Screen
Camera Event
Browse Option
QR Code Events
Accelerometer Sensor
EditText from Spinner

Android SQLite DB

  1. SQLite Database All Events in Android
         1. Insert
         2. select All
         3. Select
         4. Update
         5. Delete

Note                     Download ADT Plugin Here.
Here I am using,
OS : Linux (Ubuntu 12.04)
Eclipse : Juno (Version 4.2.0)
Android API Level : 3 to 'n' as per need
Emulator API Level : It will be displayed in output image

A. SQLite Database All Events in Android


Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.

Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="150dp"
        android:text="Insert" />
 
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="220dp"
        android:text="Select All" />
 
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="290dp"
        android:text="Select" />
    
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="360dp"
        android:text="Update" />
 
   <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="430dp"
        android:text="Delete" />
 
    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:ems="4"
        android:hint="Id"
        android:inputType="number">
        <requestFocus />
    </EditText>
    
    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:ems="8"
        android:hint="Name"
        android:inputType="textPersonName">
        
    </EditText>
    
    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:ems="10"
        android:hint="Email"
        android:inputType="textEmailAddress" />
 
</RelativeLayout>

Step 3 : Open src -> package -> DBAdapter.java and add following code :

package balaji.sqlite_events;
 
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
 
public class DBAdapter {
	public static final String KEY_ROWID = "_id";
	public static final String KEY_NAME = "name";
	public static final String KEY_EMAIL = "email";
	private static final String TAG = "DBAdapter";
	private static final String DATABASE_NAME = "MyDB";
	private static final String DATABASE_TABLE = "contacts";
	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_CREATE =
			"create table contacts (_id integer primary key autoincrement, "
					+ "name text not null, email text not null);";
	private final Context context;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;
	
	public DBAdapter(Context ctx)
	{
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}
	
	private static class DatabaseHelper extends SQLiteOpenHelper
	{
		DatabaseHelper(Context context)
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}
		
		@Override
		public void onCreate(SQLiteDatabase db)
		{
			try {
				db.execSQL(DATABASE_CREATE);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
		{
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS contacts");
			onCreate(db);
		}
	}
	
	
	//---opens the database---
	public DBAdapter open() throws SQLException
	{
		db = DBHelper.getWritableDatabase();
		return this;
	}
	
	
	//---closes the database---
	public void close()
	{
		DBHelper.close();
	}
	
	
	//---insert a contact into the database---
	public long insertContact(String name, String email)
	{
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_NAME, name);
		initialValues.put(KEY_EMAIL, email);
		return db.insert(DATABASE_TABLE, null, initialValues);
	}
	
	
	//---deletes a particular contact---
	public boolean deleteContact(long rowId)
	{
		return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}
	
	
	//---retrieves all the contacts---
	public Cursor getAllContacts()
	{
		return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,KEY_EMAIL}, 
				null, null, null, null, null);
	}
	
	
	//---retrieves a particular contact---
	public Cursor getContact(long rowId) throws SQLException
	{	
		Cursor mCursor =db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
				KEY_NAME, KEY_EMAIL}, KEY_ROWID + "=" + rowId, null,
				null, null, null, null);
		if (mCursor != null) {
    			mCursor.moveToFirst();
		}
		return mCursor;
	}
	
	
	//---updates a contact---
	public boolean updateContact(long rowId, String name, String email)
	{
		ContentValues args = new ContentValues();
		args.put(KEY_NAME, name);
		args.put(KEY_EMAIL, email);
		return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
	}
}

Step 4 : Open src -> package -> MainActivity.java and add following code :

package balaji.sqlite_events;
 
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
       
		final Button b1=(Button) findViewById(R.id.button1);
		final Button b2=(Button) findViewById(R.id.button2);
		final Button b3=(Button) findViewById(R.id.button3);
		final Button b4=(Button) findViewById(R.id.button4);
		final Button b5=(Button) findViewById(R.id.button5);
 
		final EditText et1=(EditText)findViewById(R.id.editText1);
		final EditText et2=(EditText)findViewById(R.id.editText2);
		final EditText et3=(EditText)findViewById(R.id.editText3);
 
		final DBAdapter db = new DBAdapter(MainActivity.this);
 
		//---Insert Contact---
		b1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				db.open();
				db.insertContact(et2.getText().toString(),
					et3.getText().toString());
				db.close();
				Toast.makeText(getBaseContext(), "Inserted",
					Toast.LENGTH_SHORT).show();
			}
		});
 
		//---Select All contacts---
		b2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				db.open();
				Cursor c = db.getAllContacts();
				if (c.moveToFirst())
				{
					do {
						DisplayContact(c);
					} while (c.moveToNext());
				}
				db.close();
			}
 
			private void DisplayContact(Cursor c)
			{
				// TODO Auto-generated method stub
				Toast.makeText(getBaseContext(),"id: " + c.getString(0) + "
					\n" +"Name: " + c.getString(1) + "\n" +
					"Email: " + c.getString(2),
					Toast.LENGTH_LONG).show();
			}
		});
 
		//---Select a contact---    
		b3.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				db.open();
				Cursor c = db.getContact(Integer.parseInt
					(et1.getText().toString()));
				if (c.moveToFirst())        
					DisplayContact(c);
				else
					Toast.makeText(getBaseContext(), "No contact found", 
						Toast.LENGTH_LONG).show();
				db.close();
			}
 
			private void DisplayContact(Cursor c) {
				// TODO Auto-generated method stub
				Toast.makeText(getBaseContext(),"id: " + c.getString(0) + 
						"\n" +"Name: " + c.getString(1) + "\n" +
						"Email: " + c.getString(2),
						Toast.LENGTH_LONG).show();
			}
		}) ;
 
		//---updates a contact---       
		b4.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				db.open();
				if (db.updateContact
					(Integer.parseInt(et1.getText().toString()), 
					et2.getText().toString(), et3.getText().toString()))

					Toast.makeText(getBaseContext(), "Update
						successful.", Toast.LENGTH_LONG).show();
				else
					Toast.makeText(getBaseContext(), "Update failed.", 
							Toast.LENGTH_LONG).show();
				db.close();
			}
		});
		//---delete a contact---  
		b5.setOnClickListener(new View.OnClickListener() {	
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub		
				db.open();
				db.deleteContact(Integer.parseInt(et1.getText().toString()));
				db.close();		
			}
		});
	}
 
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
}

Step 5 : Open AndroidManifest.xml and add following code :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="balaji.sqlite_events"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="3"
        android:targetSdkVersion="15" />
 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".DBAdapter"></activity>
    </application>
 
</manifest>

Step 6 : Open res ->values ->strings.xml and add following code :

<resources>
 
    <string name="app_name">SQLite_Events</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
 
</resources>

Step 7 : Our output will be like this :


Output


Output


Output


Output


Output


Output









SHARE THIS PAGE



product 2

product 3

product 4

Feedbacks : balaji.scz@gmail.com