hello friends
i am new to android. i am trying to create a simple database application.but i getting run time problem when i try to retrive more then 1 data item from database.
my code is here---
public class DataHelper extends SQLiteOpenHelper{
public DataHelper(Context c){
super(c,"mydb",null,1);
}
public void onCreate(SQLiteDatabase db){
db.execSQL("create table records(id integer primary key autoincrement,name text not null,address text,age integer)");
}
public void onUpgrade(SQLiteDatabase db,int oldv,int newv){
db.execSQL("drop table if exist records");
this.onCreate(db);
}
public void insertdata(String name,String address,int age){
SQLiteDatabase sd=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("name", name);
cv.put("address",address);
cv.put("age",age);
sd.insert("records", null, cv);
}
public String getdata(){
SQLiteDatabase sd=this.getWritableDatabase();
String result=null;
String fname=null;
String city=null;
int age;
Cursor c=sd.query("records", null, null, null, null, null, null);
if(c.moveToFirst()){
do{
fname=c.getString(c.getColumnIndex("name"));
city=c.getString(c.getColumnIndex("address"));
age=c.getInt(c.getColumnIndex("age"));
result=fname+"/n"+city+"/n"+age;
}while(c.moveToNext());
}
return result;
}
}
Activity class code is here----
public class Act5 extends Activity {
DataHelper help;
SQLiteDatabase sd;
EditText e1,e2,e3;
TextView t;
Button b1,b2;
public void onCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.main);
help=new DataHelper(this);
e1=(EditText)findViewById(R.id.et1);
e2=(EditText)findViewById(R.id.et2);
e3=(EditText)findViewById(R.id.et3);
b1=(Button)findViewById(R.id.bt1);
b2=(Button)findViewById(R.id.bt2);
t=(TextView)findViewById(R.id.tv);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String name=e1.getText().toString();
String city=e2.getText().toString();
String ages=e3.getText().toString();
int age=Integer.parseInt(ages);
sd=help.getWritableDatabase();
help.insertdata(name,city,age);
e1.setText("");
e2.setText("");
Toast.makeText(getApplicationContext(), "your data saved", Toast.LENGTH_LONG).show();
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sd=help.getWritableDatabase();
String result=help.getdata();
t.setText("your data is: "+result);
}
});
}
}