0

我想在 sqlite 中更新我的数据。我有个人资料主页。在页面中有 3 个按钮(添加、查看、编辑)。在编辑页面中,我可以从 sqlite 检索数据并显示它,现在,我要做的是,当用户编辑并保存它时,它将更新 sqlite。

以下是我的活动代码和数据库代码

public class ProfileEdit extends Activity 
{
private SQLiteDatabase database;
private ArrayList<Integer> idList = new ArrayList<Integer>();
ArrayList<String> list = new ArrayList<String>();
private DBHelper helper;
Button btnSave;
EditText addAddress, addHeight, addDrName, addDrContact,
         addGuardianName1, addGuardianContact1, addGuardianName2, 
         addGuardianContact2, addHospitalName, addHospitalContact;
TextView viewName, viewDOB, viewBloodType;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.profile_edit);

    btnSave = (Button)findViewById(R.id.btnSave);

    addAddress = (EditText)findViewById(R.id.editText4);
    addHeight = (EditText)findViewById(R.id.editText6);
    addDrName = (EditText)findViewById(R.id.editText7);
    addDrContact = (EditText)findViewById(R.id.editText8);
    addGuardianName1 = (EditText)findViewById(R.id.editText9);
    addGuardianContact1 = (EditText)findViewById(R.id.editText10);
    addGuardianName2 = (EditText)findViewById(R.id.EditText01);
    addGuardianContact2 = (EditText)findViewById(R.id.EditText02);
    addHospitalName = (EditText)findViewById(R.id.editText11);
    addHospitalContact = (EditText)findViewById(R.id.editText12);

    viewName = (TextView)findViewById(R.id.editText1);
    viewDOB = (TextView)findViewById(R.id.dobbtn);
    viewBloodType = (TextView)findViewById(R.id.editText5);

    helper = new DBHelper(this);
    database = helper.getWritableDatabase();

    String sql = "select * from " + DBHelper.TABLE_PATIENT;
    Cursor cursor = database.rawQuery(sql, null);

    try{
        if( cursor != null ){
            if (cursor.moveToFirst()) {
               do {
                   idList.add(cursor.getInt(0));
                   list.add(cursor.getString(1));
                   list.add(cursor.getString(2));
                   list.add(cursor.getString(3));
                   list.add(cursor.getString(4));
                   list.add(cursor.getString(5));
                   list.add(cursor.getString(6));
                   list.add(cursor.getString(7));
                   list.add(cursor.getString(8));
                   list.add(cursor.getString(9));
                   list.add(cursor.getString(10));
                   list.add(cursor.getString(11));
                   list.add(cursor.getString(12));
                   list.add(cursor.getString(13));
               } while (cursor.moveToNext());
            }
        }
    }
    catch(SQLException e){  
    }

    cursor.moveToFirst();

    viewName.setText(cursor.getString(1));
    viewDOB.setText(cursor.getString(2));
    addAddress.setText(cursor.getString(3));
    viewBloodType.setText(cursor.getString(4));
    addHeight.setText(cursor.getString(5));
    addDrName.setText(cursor.getString(6));
    addDrContact.setText(cursor.getString(7));
    addGuardianName1.setText(cursor.getString(8));
    addGuardianContact1.setText(cursor.getString(9));
    addGuardianName2.setText(cursor.getString(10));
    addGuardianContact2.setText(cursor.getString(11));
    addHospitalName.setText(cursor.getString(12));
    addHospitalContact.setText(cursor.getString(13));
    cursor.close();

    btnSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            String address = addAddress.getText().toString();
            String height = addHeight.getText().toString();
            String drname = addDrName.getText().toString();
            String drcontact = addDrContact.getText().toString();
            String guardianname1 = addGuardianName1.getText().toString();
            String guardiancontact1 = addGuardianContact1.getText().toString();
            String guardianname2 = addGuardianName2.getText().toString();
            String guardiancontact2 = addGuardianContact2.getText().toString();
            String hospitalname = addHospitalName.getText().toString();
            String hospitalcontact = addHospitalContact.getText().toString();

            helper.updateDataProfile(database, 1, address, height, 
                    drname, drcontact, guardianname1, guardiancontact1,
                    guardianname2, guardiancontact2, hospitalname,
                    hospitalcontact);
            Toast.makeText(ProfileEdit.this, "Successfully Edit", Toast.LENGTH_SHORT).show();

            Intent i = new Intent(ProfileEdit.this, ProfileMain.class);
            startActivity(i);

        }
    });
}

public void onClickCancel(View v){
    startActivity (new Intent(getApplicationContext(), ProfileView.class));
}
 } // end class

这是我的 DBHelper 更新代码

public void updateDataProfile(SQLiteDatabase db, int id, 
            String address, String height, String drname, 
            String drcontact, String guardianname1, String guardiancontact1, 
            String guardianname2, String guardiancontact2, String hospitalname, 
            String hospitalcontact)
    {
        ContentValues editProfile = new ContentValues();
        editProfile.put(ADDRESS, address);
        editProfile.put(HEIGHT, height);
        editProfile.put(DRNAME, drname);
        editProfile.put(DRCONTACT, drcontact);
        editProfile.put(GUARDIANNAME1, guardianname1);
        editProfile.put(GUARDIANCONTACT1, guardiancontact1);
        editProfile.put(GUARDIANNAME2, guardianname2);
        editProfile.put(GUARDIANCONTACT2, guardiancontact2);
        editProfile.put(HOSPITALNAME, hospitalname);
        editProfile.put(HOSPITALCONTACT, hospitalcontact);
        db.update(TABLE_PATIENT, editProfile, PATIENT_ID + " = " +1, null);

    }
4

1 回答 1

0

使用此模式进行更新并提供 PATIENT_ID=id ;

我希望它会起作用。

'db.update(TABLE_CONTACTS, values, KEY_jobid + "= ?", new String[] { String.valueOf(contact.getjobid()) });'

于 2013-04-27T19:05:57.640 回答