Saturday, August 6, 2011

Android database example

Android database example
...............................................



// android db example
--------------------------------



package com.sqlite;


public class SQLiteDataBase extends ListActivity {
public static int Online=1;
ArrayList LISTITEMS=new ArrayList();
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo().isConnectedOrConnecting();

}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();


}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
if(isOnline())
{

}else
{
showDialog(Online);
}
// if(isOnline()==false)
// {
// showDialog(Online);
// }



DBAdapter db = new DBAdapter(this);


setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, LISTITEMS));


//Inserting emp details in to databse.


db.open();

long id;

id = db.insertTitle(1,
"programer1",
"Android Application Developer",
"programer1@gmail.com",1111111);

id = db.insertTitle(2,
"programer2",
"iPhone Developer",
"programer2@gmail.com",2222222);
id = db.insertTitle(3,
"programer3",
"iPhone Developer",
"programer3@gmail.com",333333);

db.close();

// Retriving all the records


db.open();
Cursor c = db.getAllTitles();
if (c.moveToFirst())
{
do {
LISTITEMS.add( c.getString(0));
LISTITEMS.add( c.getString(1));
LISTITEMS.add( c.getString(2));
LISTITEMS.add( c.getString(3));
LISTITEMS.add( c.getString(4));
} while (c.moveToNext());
}

// if(isOnline()==false)
// {
// showDialog(Online);
// }
//
/*
// Retriving a single Record

db.open();
Cursor r = db.getTitle(1);
if (r.moveToFirst())
DisplayTitle(r);
else
Toast.makeText(this, "No title found",
Toast.LENGTH_LONG).show();


*/
/*
//---update title---
db.open();
try{

if (db.updateTitle(3 ,"xyz","C# 2008 Programmer","xyz.abc@gmail.com",1234))

Toast.makeText(this, "Update successful.",Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "Update failed.",Toast.LENGTH_LONG).show();

//---retrieve the same title to verify---
Cursor c = db.getTitle(3);
if (c.moveToFirst())
DisplayTitle(c);
else
Toast.makeText(this, "No title found",

Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
e.printStackTrace();
}

*/

/*
//---delete a title---
db.open();
if (db.deleteTitle(2))
Toast.makeText(this, "Delete successful-1.",
Toast.LENGTH_LONG).show();
if (db.deleteTitle(3))
Toast.makeText(this, "Delete successful-2.",
Toast.LENGTH_LONG).show();


*/
}
public Dialog onCreatDialiog(int id){

switch(id){
//This is a simple Alert Dialog
case 1:

AlertDialog.Builder ab = new AlertDialog.Builder(this);

ab.setTitle("ALERT_DIALOG IN3");
ab.setMessage(" Please enable the internet ");
ab.setIcon(R.drawable.icon);

ab.setPositiveButton("OK", new DialogInterface.OnClickListener(){

public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "OK Button CLicked", Toast.LENGTH_LONG).show();
}

});
ab.setNegativeButton("No I dont agree", new DialogInterface.OnClickListener(){

public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "No Button CLicked", Toast.LENGTH_LONG).show();

}

});

Dialog ad = ab.create();
return ad;
}
return null;
}
/*
//// public Dialog onCreateDialog(int id) {
switch(id){
//This is a simple Alert Dialog
case ALERT_DIALOG:
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setTitle("ALERT_DIALOG IN3");
ab.setMessage(" Example for Alert Dialog PRES BACK TO Dismiss Dialog");
ab.setIcon(R.drawable.icon);

ab.setPositiveButton("OK", new DialogInterface.OnClickListener(){

public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "OK Button CLicked", Toast.LENGTH_LONG).show();
}

});
ab.setNegativeButton("No I dont agree", new DialogInterface.OnClickListener(){

public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "No Button CLicked", Toast.LENGTH_LONG).show();

}

});

Dialog ad = ab.create();
return ad;



//This is a Custom Dialog
//New Layout CustomLayout is ceated check res\layout
//check that SetcontentView function is used
case CUSTOM_DIALOG:

Dialog dialog = new Dialog(AllSortsOfDialogs.this);
///////
public void DisplayTitle(Cursor c)
{
Toast.makeText(this,
"eno: " + c.getString(0) + "\n" +
" ename: " + c.getString(1) + "\n" +
"desg: " + c.getString(2) + "\n" +
"email: " + c.getString(3)+"\n"+
"phno: "+c.getString(4),
Toast.LENGTH_LONG).show();
}
*/


}

---------------------------


db file
-------

package com.sqlite;

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_NO = "no";
public static final String KEY_NAME = "name";
public static final String KEY_DESG = "desg";
public static final String KEY_EMAIL = "email";
public static final String KEY_PHNO="phno";


// private static final String TAG = "DBAdapter";

private static final String DATABASE_NAME = "employedetails";
private static final String DATABASE_TABLE = "tableone";
private static final int DATABASE_VERSION = 4;

private static final String DATABASE_CREATE =
"create table tableone(no integer primary key , "+ "name text not null, desg text not null, "+ "email text not null," +"phno integer);";

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)
{
db.execSQL(DATABASE_CREATE);
}

@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 tableone");
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 title into the database---
public long insertTitle(long no,String name, String desg, String email ,long phno)
{
ContentValues initialValues = new ContentValues();

initialValues.put(KEY_NO, no);
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_DESG, desg);
initialValues.put(KEY_EMAIL, email);
initialValues.put(KEY_PHNO, phno);

return db.insert(DATABASE_TABLE, null, initialValues);
}
//---deletes a particular title---
public boolean deleteTitle(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_NO +"=" + rowId, null) > 0;
}

//---retrieves all the titles---
public Cursor getAllTitles()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_NO,
KEY_NAME,
KEY_DESG,
KEY_EMAIL,KEY_PHNO},
null,
null,
null,
null,
null);
}

//---retrieves a particular title---
public Cursor getTitle(long rowId) throws SQLException
{
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_NO,
KEY_NAME,
KEY_DESG,
KEY_EMAIL,KEY_PHNO
},
KEY_NO + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}

//---updates a title---
public boolean updateTitle(long no, String name,String desg, String email,long phno)
{
ContentValues args = new ContentValues();
args.put(KEY_NO, no);
args.put(KEY_NAME, name);
args.put(KEY_DESG, desg);
args.put(KEY_EMAIL, email);
args.put(KEY_PHNO, phno);
return db.update(DATABASE_TABLE, args,KEY_NO , null) > 0;
}
}




--
krishnachary

No comments:

Post a Comment