DatabaseapplicationActivity.java
package com.databaseapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class DatabaseapplicationActivity extends Activity {
/** Called when the activity is first created. */
EditText name,age,qual,place;
String name1,age1,qual1,place1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name=(EditText)findViewById(R.id.editText1);
age=(EditText)findViewById(R.id.editText2);
qual=(EditText)findViewById(R.id.editText3);
place=(EditText)findViewById(R.id.editText4);
}
public void submit(View v)
{
name1=name.getText().toString();
age1=age.getText().toString();
qual1=qual.getText().toString();
place1=place.getText().toString();
details d=new details();
d.setName(name1);
d.setAge(age1);
d.setQual(qual1);
d.setPlace(place1);
databasehandler dbh=new databasehandler(this);
dbh.insert(d);
Toast.makeText(getApplicationContext(),"Details Submitted Successfully", Toast.LENGTH_LONG).show();
}
public void viewDetails(View v)
{
Intent i=new Intent(this,second.class);
startActivity(i);
}
public void editDetails(View v)
{
Intent k=new Intent(this,third.class);
startActivity(k);
}
public void deletedata(View v)
{
Intent l=new Intent(this,fourth.class);
startActivity(l);
}
}
Database Handler
package com.databaseapp;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
public class databasehandler extends SQLiteOpenHelper{
private static final String MYDB="mydb";
private static final String MYTB="USER";
public String deleted;
SQLiteDatabase db;
Context context;
public databasehandler(Context context) {
super(context,MYDB, null, 32);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table if not exists "+MYTB+" (id integer primary key autoincrement,name text,age text,qualification text,place text)");
//db.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("drop table if exists "+MYTB);
onCreate(db);
}
public void insert(details d) {
Log.v("Errror","here");
SQLiteDatabase db=getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("name",d.getName());
Log.v("name",d.getName());
cv.put("age",d.getAge());
Log.v("age",d.getAge());
cv.put("qualification",d.getQual());
Log.v("qualification",d.getQual());
cv.put("place",d.getPlace());
Log.v("place",d.getPlace());
db.insert(MYTB,"id",cv);
Log.v("data","inserted");
}
public ArrayList<String> resultdata() {
ArrayList<String> results=new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
try {
Cursor c=null;
c = db.rawQuery("select * from "+MYTB, null);
if (c!= null) {
Log.v("cursor","notnull");
if (c.moveToFirst());
{
do {
int cc=c.getCount();
Log.v("cursor count",""+cc);
String Name = c.getString(c.getColumnIndex("name"));
Log.v("Name",Name);
String Age = c.getString(c.getColumnIndex("age"));
Log.v("Age",Age);
String Qualification=c.getString(c.getColumnIndex("qualification"));
Log.v("Qualification",Qualification);
String Place=c.getString(c.getColumnIndex("place"));
Log.v("Place",Place);
String id=c.getString(c.getColumnIndex("id"));
Log.v("id",id);
results.add( "\n Id: "+id+"\n Name: " +Name+"\n Age : " +Age+"\n Qualification : "+Qualification+"\n Place :"+Place);
//results.add("dfrty");
Log.v("Array",""+results);
}
while (c.moveToNext());
}}
else
{
Toast.makeText(context, "results empty",Toast.LENGTH_LONG).show();
results.equals(null);
}
} catch (SQLiteException se) {
Log.e(getClass().getSimpleName(),
"Could not create or Open the database");
}
return results;
}
public Cursor update(String i){
Cursor c;
SQLiteDatabase db=this.getWritableDatabase();
String[] columns={"name","age","qualification","place"};
String selection="id=?";
String[] selectionArgs={i};
Log.v("selection",selection);
Log.v("selectionargs",i);
c=db.query(MYTB, columns, selection, selectionArgs, null, null, null);
//db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy)
return c;
}
public void updateDetails(details d,String id1) {
//db.close();
Log.v("db","notclosed");
db=getWritableDatabase();
ContentValues cv=new ContentValues();
Log.v("update","query");
cv.put("name",d.getName());
Log.v("name",d.getName());
cv.put("age",d.getAge());
Log.v("age",d.getAge());
cv.put("qualification",d.getQual());
Log.v("qualification",d.getQual());
cv.put("place",d.getPlace());
Log.v("place",d.getPlace());
String where="id=?";
String [] whereArgs={id1};
db.update(MYTB, cv, where,whereArgs);
}
public void deletedetails(String idd)
{
Cursor c=null;
Log.v("ivalue", ""+idd);
SQLiteDatabase db=getWritableDatabase();
deleted="false";
String[] columns={"name","age","qualification","place"};
String selection="id=?";
String[] selectionArgs={idd};
Log.v("selection",selection);
Log.v("selectionargs",idd);
c=db.query(MYTB, columns, selection, selectionArgs, null, null, null);
//db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy)
if(c!=null)
{
if( c.moveToFirst())
{
String where="id=?";
String[] args={idd};
Log.v("value",""+args);
db.delete(MYTB, where, args);
deleted="true";
}
else
{
deleted="false";
}
}
else
{
deleted="false";
}
}
}
get and set
package com.databaseapp;
public class details {
String name,age,qual,place;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getQual() {
return qual;
}
public void setQual(String qual) {
this.qual = qual;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
}
Listview
package com.databaseapp;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class second extends Activity {
ListView list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
list=(ListView)findViewById(R.id.listView1);
databasehandler dbh=new databasehandler(getApplicationContext());
ArrayList<String > result=new ArrayList<String>();
result=dbh.resultdata();
Log.v("reached",""+result);
if (result.isEmpty()==true)
//Log.v("after clear",""+result);
{
Toast.makeText(getApplicationContext(),"Database empty",Toast.LENGTH_LONG).show();
}
else
{
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, result);
list.setAdapter(adapter);
}
}
}
update
package com.databaseapp;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class third extends Activity{
EditText t1,t2,t3,t4,t5;
String name1,age1,qual1,place1,id1;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
t1=(EditText)findViewById(R.id.id1);
t2=(EditText)findViewById(R.id.txt1);
t3=(EditText)findViewById(R.id.txt2);
t4=(EditText)findViewById(R.id.txt3);
t5=(EditText)findViewById(R.id.txt4);
}
public void editdetails(View v)
{
String id1=t1.getText().toString();
databasehandler dbh=new databasehandler(this);
Cursor c=null;
c=dbh.update(id1);
if(c!=null)
{
Log.v("db","null");
Log.v("inside","cursor");
if( c.moveToFirst())
{
String s=c.getString(c.getColumnIndex("name"));
Log.v("name",s);
String p=c.getString(c.getColumnIndex("age"));
String q=c.getString(c.getColumnIndex("qualification"));
String r=c.getString(c.getColumnIndex("place"));
t2.setText(s);
t3.setText(p);
t4.setText(q);
t5.setText(r);
c.close();
// dbh.db.close();
}
else
{
Toast.makeText(getApplicationContext(), "no data found", Toast.LENGTH_LONG).show();
}
}
else
{
Toast.makeText(getApplicationContext(), "No data found", Toast.LENGTH_LONG).show();
}
//db.close();
}
public void updatedata(View v)
{
Log.v("ivide","ethi");
databasehandler dbh=new databasehandler(this);
name1=t2.getText().toString();
Log.v("updatedname",name1);
age1=t3.getText().toString();
Log.v("updateage",age1);
qual1=t4.getText().toString();
Log.v("updatedqual",qual1);
place1=t5.getText().toString();
Log.v("updatedplace",place1);
id1=t1.getText().toString();
details d=new details();
d.setName(name1);
d.setAge(age1);
d.setQual(qual1);
d.setPlace(place1);
Log.v("going to","update query");
dbh.updateDetails(d,id1);
Toast.makeText(getApplicationContext(),"Updated",Toast.LENGTH_LONG).show();
}
public void viewdata1(View v)
{
Intent i =new Intent(third.this,second.class);
startActivity(i);
}
}
delete
package com.databaseapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class fourth extends Activity{
EditText t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fourth);
t1=(EditText)findViewById(R.id.etxt1);
}
public void deleteDetails(View v)
{
String idd=t1.getText().toString();
databasehandler dbh=new databasehandler(this);
dbh.deletedetails(idd);
String s=dbh.deleted;
if( s=="true")
{
Toast.makeText(getApplicationContext(), "deleted", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "no data found", Toast.LENGTH_LONG).show();
}
}
}
No comments:
Post a Comment