简体   繁体   English

如何在android工作室的不同活动中将数据插入sqlite的单行数据库中

[英]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我做了一个帮手 class 名字叫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.但是我无法将数据插入一行,注册活动的数据存储在第一行,高度活动的高度存储在第二行 rest 的列设置为 Null。知道在哪里我做错了。

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?)在第一个活动中你正在创建一个 object 的婴儿并将它插入到数据库(没有高度属性)在第二个活动中你正在创建另一个 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)解决方案:在第二个活动中,您应该更新以前添加的记录(在第一个活动中)而不是在数据库中添加新的婴儿项目(在第一个活动中使用最后添加的项目的 ID 并在第二个活动中更新该行)

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 :每次将新项目添加到 DB addBaby 方法时,都会返回一个长值作为表中添加项目的 ID,您需要捕获它,因此您在SQLiteOpenHelper class中的这个新的addBaby()

 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:每次您将新婴儿添加到表中时,都会像这样返回id

   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:当导航到第二个活动时,将检索到的ID放入意图中并发送到下一个活动:

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

now add updateHeight method inside your SQLiteOpenHelper class :现在在你的SQLiteOpenHelper class 中添加updateHeight 方法

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:最后,您可以在第二个活动中从 intent 中读取 id 并通过此 id,更新最后插入的行并为其添加高度属性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将照片从Android中的不同类插入sqlite数据库中的同一行 - insert photo to same row in sqlite database from different class in Android 无法将数据插入 android studio sqlite 数据库 - Unable to insert data into android studio sqlite database 如何同时(从不同线程)将数据插入一个Android SQLite数据库的不同表中? - How can I simultaneously (from different threads) insert data into different tables in one Android SQLite database? 如何将单行数据存储到android中的sqlite数据库的字符串数组中 - How to store a single row data into a string array of sqlite database in android 如何使用 SQLite 数据库从表 1 中检索列数据并将该列值插入到 Android Studio 中的表 2 中? - How to retrive column data from table 1 and insert that column values into table 2 in Android Studio using SQLite database? 如何在Android Studio中从Sqlite数据库插入和检索图像 - How to insert and retrieve image from Sqlite database in Android Studio Android Studio SQLite从数据库插入图像 - Android Studio SQLite insert image from database Android中SQLite数据库中的单行 - Single Row from SQLite Database in Android Android Studio-向SQLite数据库添加不同的数据 - Android Studio - Adding different data to SQLite Database 如何在sqlite android studio中插入多个数据 - How to insert multiple data in sqlite android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM