Update by ID
1/ Include a DAO function for updating:
@Update
public void updateEmployee(Employee e);
2/ Check the employee table class and see if we have a way to instinatie new employee objects with an id
@Entity(tableName = "employees")
public class Employee {
// database to automatically assign values
@PrimaryKey(autoGenerate = true)
int id;
String name;
String date_hired;
// constructors
public Employee(String name, String date_hired) {
this.name = name;
this.date_hired = date_hired;
}
// add an additional constructor that enables us to create employees with an id
@Ignore
public Employee(int id, String name, String date_hired) {
this.id = id;
this.name = name;
this.date_hired = date_hired;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", date_hired='" + date_hired + '\'' +
", id=" + id +
'}';
}
}
3/ In your main activity, call the updateEmployee function
public void updatePressed(View view) {
Employee e1 = new Employee(99, "Peter", "");
empDAO.updateEmployee(e1);
}
Share with your friends: |