lundi 21 décembre 2015

updating a tuple in a sqlite table in android studio

before voting down please read my question , which I have searched a lot but I couldn't find the answer yet, so I would appreciate if you give me hand to overcome the problem. Actually I need to update the information in a table named "Demographics".I don't know how can I point to the user which is currently using the app? So that I can fetch the primary key of this user and use in "where clause". here are the codes:

public class DemographicsCRUD extends HospitalDBDAO {
private static final String WHERE_ID_EQUALS = DataBaseHelper.patientID + " =?";

private DataBaseHelper dbHelper;
//  private SQLiteDatabase db;

public DemographicsCRUD(Context context) {
    super(context);
    dbHelper = new DataBaseHelper(context);
}

public long UpdateDemographics(Demographics_to demoId) {
    //SQLiteDatabase db = dbHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(DataBaseHelper.lastName, demoId.getD_lastName());
    values.put(DataBaseHelper.firstName, demoId.getD_firstName());
    values.put(DataBaseHelper.dateOfBirth, demoId.getD_dateOfBirth());
    values.put(DataBaseHelper.sex, demoId.getD_sex());
    values.put(DataBaseHelper.address1, demoId.getD_address1());
    values.put(DataBaseHelper.address2, demoId.getD_address2());
    values.put(DataBaseHelper.city, demoId.getD_city());
    values.put(DataBaseHelper.state, demoId.getD_state());
    values.put(DataBaseHelper.postalCode, demoId.getD_postalCode());
    values.put(DataBaseHelper.country, demoId.getD_country());
    values.put(DataBaseHelper.tel1, demoId.getD_tel1());
    values.put(DataBaseHelper.tel2, demoId.getD_tel2());
    values.put(DataBaseHelper.generalPractitioner, demoId.getD_generalPractitioner());
    values.put(DataBaseHelper.height, demoId.getD_height());
    values.put(DataBaseHelper.weight, demoId.getD_weight());
    values.put(DataBaseHelper.foodPreferences, demoId.getD_foodPreferences());
    values.put(DataBaseHelper.foodLimitation, demoId.getD_foodLimitation());
    values.put(DataBaseHelper.email, demoId.getD_email());
    values.put(DataBaseHelper.syncedURL, demoId.getD_syncedURL());

    long result = database.update(dbHelper.Demographics_Table, values,
            WHERE_ID_EQUALS,
            new String[]{String.valueOf(demoId.getD_patientID())});

    Log.d("Update Result:", "=" + result);
   // db.close();
    return result;
}

here is where I call the above code:

public class EditPersonal extends Fragment implements OnClickListener {

Button btn1,btnc,btns;
EditText ep_name,ep_family,ep_happy,ep_add1,ep_add2,ep_city,ep_state,ep_post,ep_country,ep_tel1,ep_tel2,ep_gp,ep_he,ep_we,ep_fp,ep_fl,ep_em,ep_sr;
RadioButton ep_male,ep_female;

Demographics_to ep_demoId = null;
private DemographicsCRUD ep_demoCRUD;
private AddAccTask addaccTask;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ep_demoCRUD = new DemographicsCRUD(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    final View rootView = inflater.inflate(R.layout.edit_personal_info, container, false);
    findViewsById(rootView);
    setListeners();
    return rootView;
}
private void setListeners() {
    btn1.setOnClickListener(this);
    btnc.setOnClickListener(this);
    btns.setOnClickListener(this);
}
public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getActivity().getSupportFragmentManager(), "datePicker");

}
private void findViewsById(View rootView) {
    btn1=(Button) rootView.findViewById(R.id.btnCal_editP);
    btnc=(Button) rootView.findViewById(R.id.btnClearAll_editP);
    btns=(Button) rootView.findViewById(R.id.btnSubmit_editP);
    ep_name=(EditText) rootView.findViewById(R.id.txtFirstName_editP);
    ep_family=(EditText) rootView.findViewById(R.id.txtLastName_editP);
    ep_happy=(EditText) rootView.findViewById(R.id.txtDateOfBirth_editP);
    ep_add1=(EditText) rootView.findViewById(R.id.txtAddress1_editP);
    ep_add2=(EditText) rootView.findViewById(R.id.txtAddress2_editP);
    ep_city=(EditText) rootView.findViewById(R.id.txtCity_editP);
    ep_state=(EditText) rootView.findViewById(R.id.txtState_editP);
    ep_post=(EditText) rootView.findViewById(R.id.txtPostal_editP);
    ep_country=(EditText) rootView.findViewById(R.id.txtCountry_editP);
    ep_tel1=(EditText) rootView.findViewById(R.id.txtTel1_editP);
    ep_tel2=(EditText) rootView.findViewById(R.id.txtTel2_editP);
    ep_gp=(EditText) rootView.findViewById(R.id.txtGP_editP);
    ep_fl=(EditText) rootView.findViewById(R.id.txtFoodL_editP);
    ep_fp=(EditText) rootView.findViewById(R.id.txtFoodP_editP);
    ep_he=(EditText) rootView.findViewById(R.id.txtHeight_editP);
    ep_we=(EditText) rootView.findViewById(R.id.txtWeight_editP);
    ep_em=(EditText) rootView.findViewById(R.id.txtEmail_editP);
    ep_sr=(EditText) rootView.findViewById(R.id.txtSyncedURL_editP);
    ep_male=(RadioButton) rootView.findViewById(R.id.rbtn_male);
    ep_female=(RadioButton)rootView.findViewById(R.id.rbtn_female);
}
protected void resetAllFields() {
    ep_name.setText("");
    ep_family.setText("");
    ep_happy.setText("");
    ep_add1.setText("");
    ep_add2.setText("");
    ep_city.setText("");
    ep_state.setText("");
    ep_post.setText("");
    ep_country.setText("");
    ep_tel1.setText("");
    ep_tel2.setText("");
    ep_gp.setText("");
    ep_fl.setText("");
    ep_fp.setText("");
    ep_he.setText("");
    ep_we.setText("");
    ep_em.setText("");
    ep_sr.setText("");
}

private void updateDemographicsTable()
{
    ep_demoId = new Demographics_to();
    String ep_na = ep_name.getText().toString();
    String ep_fa = ep_family.getText().toString();
    String ep_bd = ep_happy.getText().toString();
    String ep_a1 = ep_add1.getText().toString();
    String ep_a2 = ep_add2.getText().toString();
    String ep_ci = ep_city.getText().toString();
    String ep_st = ep_state.getText().toString();
    String ep_po = ep_post.getText().toString();
    String ep_co = ep_country.getText().toString();
    String ep_t1 = ep_tel1.getText().toString();
    String ep_t2 = ep_tel2.getText().toString();
    String ep_g = ep_gp.getText().toString();
    String ep_l = ep_fl.getText().toString();
    String ep_p = ep_fp.getText().toString();
    String ep_s=ep_sr.getText().toString();
    float h=0,w=0;
    try{
        h = Float.valueOf(ep_he.getText().toString());
        w=Float.valueOf(ep_we.getText().toString());
   }catch (NumberFormatException e) {
        e.printStackTrace();
    }
    String ep_e = ep_em.getText().toString();
    ep_demoId.setD_address1(ep_a1);
    ep_demoId.setD_address2(ep_a2);
    ep_demoId.setD_city(ep_ci);
    ep_demoId.setD_country(ep_co);
    ep_demoId.setD_postalCode(ep_po);
    ep_demoId.setD_state(ep_st);
    ep_demoId.setD_tel1(ep_t1);
    ep_demoId.setD_tel2(ep_t2);
    ep_demoId.setD_dateOfBirth(ep_bd);
    ep_demoId.setD_firstName(ep_na);
    ep_demoId.setD_lastName(ep_fa);
    ep_demoId.setD_foodLimitation(ep_l);
    ep_demoId.setD_foodPreferences(ep_p);
    ep_demoId.setD_generalPractitioner(ep_g);
    ep_demoId.setD_height(h);
    ep_demoId.setD_weight(w);
    ep_demoId.setD_email(ep_e);
    ep_demoId.setD_syncedURL(ep_s);
    if (ep_male.isChecked()) {
     ep_demoId.setD_sex("Male");}
    else if(ep_female.isChecked()){
     ep_demoId.setD_sex("Female");}
}
@Override
public void onClick(View view) {

    if (view.equals( btn1))
        showDatePickerDialog(view);
     else if (view.equals(btnc))
        resetAllFields();
    else
        updateDemographicsTable();
        addaccTask = new AddAccTask(getActivity());
        addaccTask.execute((Void) null);
}
public class AddAccTask extends AsyncTask<Void, Void, Long> {

    private final WeakReference<Activity> activityWeakRef;

    public AddAccTask(Activity context) {
        this.activityWeakRef = new WeakReference<Activity>(context);
    }

    @Override
    protected Long doInBackground(Void... arg0) {
        long result = ep_demoCRUD.UpdateDemographics(ep_demoId);
        return result;
    }

    @Override
    protected void onPostExecute(Long result) {
        if (activityWeakRef.get() != null
                && !activityWeakRef.get().isFinishing()) {
            if (result != -1)
                Toast.makeText(activityWeakRef.get(), "Information Updated!",
                        Toast.LENGTH_LONG).show();
        }
    }
}

}

In fact after running the app , I got the result "0" for updating which means nothing get updated.

12-21 12:34:54.190 2351-2367/? D/Update Result:: =0

Thank you in advance for your suggestions.

Aucun commentaire:

Enregistrer un commentaire