简体   繁体   中英

How to Insert data in a single row of database of sqlite from different activites in android studio

I have made a helper class named baby

public class Baby {
    private int baby_id;
    private String baby_name;
    private String birthdate;
    private String gender;
    private String age;
    private String height;
    private String weight;
    private String sleep;
    private String medicate;


    public int getBaby_id() {
        return baby_id;
    }

    public void setBaby_id(int baby_id) {
        this.baby_id = baby_id;
    }

    public String getBaby_name() {
        return baby_name;
    }

    public void setBaby_name(String baby_name) {
        this.baby_name = baby_name;
    }

    public String getBirthdate() {
        return birthdate;
    }

    public void setBirthdate(String birthdate) {
        this.birthdate = birthdate;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getHeight() {
        return height;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    public String getWeight() {
        return weight;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }

    public String getSleep() {
        return sleep;
    }

    public void setSleep(String sleep) {
        this.sleep = sleep;
    }

    public String getMedicate() {
        return medicate;
    }

    public void setMedicate(String medicate) {
        this.medicate = medicate;
    }
}

and the database activity

private static final int DATABASE_VERSION = 10;
private static final String DATABASE_NAME = "BabyDetails.db";
private static final String TABLE_BABY = "baby";
private static final String COLUMN_BABY_ID = "baby_id";
private static final String COLUMN_BABY_NAME = "baby_name";
private static final String COLUMN_BABY_DATE = "birthdate";
private static final String COLUMN_BABY_GENDER = "gender";
private static final String COLUMN_BABY_AGE = "Age_group";
private static final String COLUMN_BABY_HEIGHT = "height";
private static final String COLUMN_BABY_WEIGHT = "weight";
private static final String COLUMN_BABY_SLEEP = "sleep";
private static final String COLUMN_BABY_MEDICATE = "medicate";


private String CREATE_BABY_TABLE = "CREATE TABLE "+ TABLE_BABY + "(" +
    COLUMN_BABY_ID + " INTEGER  PRIMARY KEY AUTOINCREMENT , " + COLUMN_BABY_NAME + " TEXT, "
    + COLUMN_BABY_DATE + " TEXT, " + COLUMN_BABY_GENDER + " TEXT, " + COLUMN_BABY_AGE + " TEXT, "
    +COLUMN_BABY_HEIGHT+ " TEXT, " + COLUMN_BABY_WEIGHT + " TEXT, " +COLUMN_BABY_SLEEP+ " TEXT, "
    +COLUMN_BABY_MEDICATE+ " TEXT " + ")";


private String DROP_BABY_TABLE = "DROP TABLE IF EXISTS " + TABLE_BABY;

public DbaseBaby( Context context) {
    super(context,DATABASE_NAME,null,DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(CREATE_BABY_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL(DROP_BABY_TABLE);
    onCreate(db);
}


public void addBaby(@NonNull Baby baby){
    SQLiteDatabase db= this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COLUMN_BABY_NAME,baby.getBaby_name());
    values.put(COLUMN_BABY_DATE,baby.getBirthdate());
    values.put(COLUMN_BABY_GENDER,baby.getGender());
    values.put(COLUMN_BABY_AGE,baby.getAge());
    values.put(COLUMN_BABY_HEIGHT,baby.getHeight());
    values.put(COLUMN_BABY_WEIGHT,baby.getWeight());
    values.put(COLUMN_BABY_SLEEP,baby.getSleep());
    values.put(COLUMN_BABY_MEDICATE,baby.getMedicate());
    db.insert(TABLE_BABY,null, values);
    db.close();
}


public Cursor getData(){
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.rawQuery(" SELECT * FROM " + TABLE_BABY , null );

    return cursor;
}

and the Sign up activity from where I am insert name, birthdate, gender and age

try {
    baby.setBaby_name(et_baby.getText().toString());
    baby.setBirthdate(et_date.getText().toString());
    baby.setGender(radioButton.getText().toString());
    baby.setAge("0-3 years");
    dbaseBaby.addBaby(baby);
} catch (Exception e) {
    e.printStackTrace();
}

and the acivity from where I want to insert the height is

dbaseBaby = new DbaseBaby( Heightone.this);
baby = new Baby();
try {
    baby.setHeight(height.getText().toString()+ "cm");
    dbaseBaby.addBaby(baby);
} catch (Exception e) {
    e.printStackTrace();
}

But I am unable to insert the data in a single row instead, the data from the Sign up activity is stored in 1st row and the height from the height activity is stored in 2nd row rest of the columns are set to Null. Any idea where I am doing wrong.

hello,

in first activity you are creating an object of baby and insert it to DB (without height attribute) in second activity again you are creating another object form baby and insert it to DB(this time just whit height attribute and others are null) actually you run two independent insert command for data-base?!(how should database understand you need add height attribute for last inserted object?)

solution: in second activity you should Update previously added record(in first activity) not add a new baby-item in DB (use id of last added item in first activity and update that row in second activity)

Edited:

every time you add new item to DB addBaby method return a long value as id of added item in table you need to catch it so this new addBaby() by yours in SQLiteOpenHelper class :

 public Long addBaby(@NonNull Baby baby){
    SQLiteDatabase db= this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COLUMN_BABY_NAME,baby.getBaby_name());
    values.put(COLUMN_BABY_DATE,baby.getBirthdate());
    values.put(COLUMN_BABY_GENDER,baby.getGender());
    values.put(COLUMN_BABY_AGE,baby.getAge());
    values.put(COLUMN_BABY_HEIGHT,baby.getHeight());
    values.put(COLUMN_BABY_WEIGHT,baby.getWeight());
    values.put(COLUMN_BABY_SLEEP,baby.getSleep());
    values.put(COLUMN_BABY_MEDICATE,baby.getMedicate());
    Long id = db.insert(TABLE_BABY,null, values);
    db.close();
    return id;
}

every time you add new baby to table catch returned id like this:

   try {
    baby.setBaby_name(et_baby.getText().toString());
    baby.setBirthdate(et_date.getText().toString());
    baby.setGender(radioButton.getText().toString());
    baby.setAge("0-3 years");
    Long id = dbaseBaby.addBaby(baby);
} catch (Exception e) {
    e.printStackTrace();
}

when navigate to second activity , put retrieved id in intent and send to next activity:

 Intent intent = new Intent(this,SecondActivity.class);
 intent.putExtra("added_id",id);
 startActivity(intent);

now add updateHeight method inside your SQLiteOpenHelper class :

public void updateHeight(Long id, String height) {
    sqLiteDatabase = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_BABY_HEIGHT,height);
    sqLiteDatabase.update(TableName, cv, "baby_id = ?", new String[]{String.valueOf(id)});

}

and finally you can in second activity read id from intent and by this id, update last inserted row and add height attribute for that:

    dbaseBaby = new DbaseBaby(Heightone.this);
    baby = new Baby();
    Long id = getIntent().getLongExtra("added_id", 0);
    String height = "50cm";
    dbaseBaby.updateHeight(id, height);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM