使用Spinner值作为WHERE子句更新SQLite中的记录

时间:2015-09-13 18:58:06

标签: android sqlite

我有这个:

enter image description here

我有一个微调器,提供从SQLite数据库中取出的员工选择。我尝试使用选择作为where值的选项来更新记录。例如,如果选择了Lysandros Lysandrou,则应在数据库中更新具有相应名称的记录。

到目前为止,我的研究让我在这里:

代码正在运行,但没有发生实际更新。我该如何实现更新?

我的微调器:

private void loadSpinnerData() {
    //database handler
    LysandrosDatabaseAdapter db = new LysandrosDatabaseAdapter(getApplicationContext());
    //spinner drop down elements
    List<DataBean> list = db.getAllDat();
    String[] nameList = new String[list.size()];

    for (int i=0; i<list.size(); i++) {
        nameList[i] = list.get(i).getName() + " " + list.get(i).getSurname();

    }
    //creating adapter for spinner
    ArrayAdapter<String > dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, nameList);
    //drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //attaching data adapter to spinner
    SelectName.setAdapter(dataAdapter);
}

我的DataBean类:

public class DataBean {

//employee fields
protected int id;
protected String name;
protected String surname;
protected String department;
protected String workplace;

public DataBean (int id, String name, String surname, String department, String workplace ) {
    this.id = id;
    this.name = name;
    this.surname = surname;
    this.department = department;
    this.workplace = workplace;
}

public DataBean (String name, String surname, String department, String workplace, String absentName, String absenceStartdate, String absenceEnddate, String absenceNotes) {
    this.name = name;
    this.surname = surname;
    this.department = department;
    this.workplace = workplace;

}

public int getID() {
    return this.id;
}

public int setID(int id) { return this.id = id; }

public String getName() {
    return this. name;
}

public String getSurname() {
    return this.surname;
}

public String getDepartment() {
    return this.department;
}

public String getWorkplace() {
   return this.workplace;
}

请耐心等待我,因为我是Android和Java的新手。我错过了什么?我认为这可能与我的WHERE条款有关,但我不知道。此外,我想我已经包含了所需的一切,但如果您需要其他任何内容,请告诉我。

编辑2:我的数据库类:

public class LysandrosDatabaseAdapter {

LysandrosHelper helper;

public LysandrosDatabaseAdapter (Context context) {

    helper = new LysandrosHelper(context);
}

public long insertData (String name,
                        String surname,
                        String tnumber,
                        String bnumber,
                        String email,
                        String address,
                        String dob,
                        String department,
                        String managerid,
                        String workplace,
                        String stardate,
                        String notes)

{
    SQLiteDatabase db = helper.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(LysandrosHelper.NAME, name);
    contentValues.put(LysandrosHelper.SURNAME, surname);
    contentValues.put(LysandrosHelper.NUMBER, tnumber);
    contentValues.put(LysandrosHelper.BNUMBER, bnumber);
    contentValues.put(LysandrosHelper.EMAIL, email);
    contentValues.put(LysandrosHelper.ADDRESS, address);
    contentValues.put(LysandrosHelper.DOB, dob);
    contentValues.put(LysandrosHelper.DEPARTMENT, department);
    contentValues.put(LysandrosHelper.MANAGERID, managerid);
    contentValues.put(LysandrosHelper.WORKPLACE, workplace);
    contentValues.put(LysandrosHelper.STARTDATE, stardate);
    contentValues.put(LysandrosHelper.NOTES, notes);


    long id = db.insert(LysandrosHelper.TABLE_NAME, null, contentValues);
    db.close();
    return id;
}

public int updateEmployee(String nameupdate,
                          String surnameupdate,
                          String departmentupdate,
                          String workplaceupdate) {

    SQLiteDatabase db = helper.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(LysandrosHelper.NAME, nameupdate);
    contentValues.put(LysandrosHelper.SURNAME, surnameupdate);
    contentValues.put(LysandrosHelper.DEPARTMENT, departmentupdate);
    contentValues.put(LysandrosHelper.WORKPLACE, workplaceupdate);

    return db.update(LysandrosHelper.TABLE_NAME, contentValues, LysandrosHelper.NAME + " = ?",
            new String[] {String.valueOf(DataBean.class.getName())});
}

static class LysandrosHelper extends SQLiteOpenHelper {
    //database info and version
    static final String DATABASE_NAME = "LysandrosDatabase";
    static final String TABLE_NAME = "LysandrosTable";
    static final int DATABASE_VERSION = 10;

    //employees info
    static final String UID = "_id";
    static final String NAME = "Name";
    static final String SURNAME = "Surname";
    static final String NUMBER = "TelephoneNumber";
    static final String BNUMBER = "BusinessNumber";
    static final String EMAIL = "Email";
    static final String ADDRESS = "Address";
    static final String DOB = "DateofBirth";
    static final String DEPARTMENT = "Department";
    static final String WORKPLACE = "Workplace";
    static final String MANAGERID = "ManagerID";
    static final String STARTDATE = "StartDate";
    static final String NOTES = "Notes";

    //absences info
    static final String ABSENTNAME = "AbsentName";
    static final String ABSENCESTARTDATE = "AbsenceStartDate";
    static final String ABSENCEENDDATE = "AbsenceEndDate";
    static final String ABSENCENOTES = "AbsenceNotes";

    private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+"("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, " +
            ""+NAME+" VARCHAR(255), " +
            ""+SURNAME+" VARCHAR(255), "+
            ""+NUMBER+" INTEGER," +
            ""+BNUMBER+" INTEGER," +
            ""+EMAIL+" VARCHAR(255)," +
            ""+ADDRESS+" VARCHAR(255)," +
            ""+DOB+" DATE," +
            ""+DEPARTMENT+" VARCHAR(255)," +
            ""+WORKPLACE+" VARCHAR(255)," +
            ""+MANAGERID+" INTEGER," +
            ""+STARTDATE+" DATE," +
            ""+ABSENTNAME+" VARCHAR(255)," +
            ""+ABSENCESTARTDATE+" DATE," +
            ""+ABSENCEENDDATE+" DATE," +
            ""+ABSENCENOTES+" TEXT,"+
            ""+NOTES+" TEXT);";

0 个答案:

没有答案