Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Android Sqlite
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) {
                // TODO Auto-generated method stub
                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) {
                // TODO Auto-generated method stub
                sd=help.getWritableDatabase();
                String result=help.getdata();
                t.setText("your data is: "+result);
 
            }
        });
 
    }
 
}
Posted 7-Jul-13 12:44pm
Edited 7-Jul-13 13:47pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 400
1 Sergey Alexandrovich Kryukov 329
2 Afzaal Ahmad Zeeshan 264
3 BillWoodruff 245
4 CPallini 195
0 OriginalGriff 5,560
1 DamithSL 4,476
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 8 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100