details.java
package com.ann;
public class details {
String name,age,place;
public void setName(String name) {
this.name=name;
// TODO Auto-generated method stub
}
public void setAge(String age) {
this.age=age;
// TODO Auto-generated method stub
}
public void setPlace(String place) {
this.place=place;
// TODO Auto-generated method stub
}
public String getName(){
return name;
}
public String getAge(){
return age;
}
public String getPlace(){
return place;
}
}
2________________
package com.ann;
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;
public class databasehandler extends SQLiteOpenHelper {
private static final String MYDB="mydb";
private static final String MYTB="USER";
public databasehandler(Context context) {
super(context,MYDB, null, 32);
// TODO Auto-generated constructor stub
}
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table if not exists "+MYTB+"(name text,age text,qualification text,place text)");
}
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) {
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("place",d.getPlace());
Log.v("place",d.getPlace());
db.insert(MYTB,null,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");
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 Place=c.getString(c.getColumnIndex("place"));
Log.v("Place",Place);
results.add("Name: " +Name+"\n Age : " +Age+"\n Place :"+Place);
//results.add("dfrty");
Log.v("Array",""+results);
} while (c.moveToNext());
}
} catch (SQLiteException se) {
Log.e(getClass().getSimpleName(),
"Could not create or Open the database");
}
return results;
}
} 3------------------------------- package com.ann;
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.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class DatabaseThreeActivity extends Activity {
EditText ename,eage,eplace;
String name,age,place;
ListView lv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ename=(EditText)findViewById(R.id.editText1);
eage=(EditText)findViewById(R.id.editText2);
eplace=(EditText)findViewById(R.id.editText3);
lv=(ListView)findViewById(R.id.listView1);
}
public void submit(View v){
name=ename.getText().toString();
age=eage.getText().toString();
place=eplace.getText().toString();
details d=new details();
d.setName(name);
d.setAge(age);
d.setPlace(place);
databasehandler db=new databasehandler(this);
db.insert(d);
Toast.makeText(getApplicationContext(),"Details Submitted Successfully", Toast.LENGTH_LONG).show();
}
public void view(View v){
databasehandler db=new databasehandler(getApplicationContext());
ArrayList<String > result=new ArrayList<String>();
result=db.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);
lv.setAdapter(adapter);
}
}
public void updatate(View v){
}
}