简体   繁体   中英

update not working in sqlite in android

public class editprofile extends Activity {

SQLiteDatabase db2;
Button btnSubmit;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pro3);

    btnSubmit=(Button)findViewById(R.id.button1);
    Button cancel = (Button) findViewById(R.id.buttonc);
    Button clear = (Button) findViewById(R.id.buttoncl);

    final EditText eid=(EditText) findViewById(R.id.editText1);
    final EditText ename=(EditText)findViewById(R.id.editText2);
    final EditText epass=(EditText)findViewById(R.id.editText3);
    final EditText eyear=(EditText)findViewById(R.id.editText5);
    final EditText edept=(EditText)findViewById(R.id.editText6);
    final EditText eemail=(EditText)findViewById(R.id.editText7);

    cancel.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            Intent inten=new Intent(getApplicationContext(),Elective1Activity.class);
            startActivity(inten);
        }
    });

          try{
    db2=openOrCreateDatabase("ElectiveDataBase2",SQLiteDatabase.CREATE_IF_NECESSARY,null);

    }catch(SQLException e)
    {       
    }
    btnSubmit.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
            ContentValues values=new ContentValues();
            values.put("studentid", eid.getText().toString());
            values.put("studentname", ename.getText().toString());
            values.put("password", epass.getText().toString());

            if((db2.update("Studentprofile1",values,"studentid"+"=?" , null))!=-1)
            {
                Toast.makeText(editprofile.this, "Profile updated Successfully ", 2000).show();
                Intent inten=new Intent(getApplicationContext(),Elective1Activity.class);
                startActivity(inten);

            }
            else
            {
                Toast.makeText(editprofile.this, "profile not updated", 2000).show();
            }
        }
    });

    clear.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            eid.setText("");
            ename.setText("");
            epass.setText("");
            edept.setText("");
            eyear.setText("");
            eemail.setText("");
        }
        });
}
@Override
protected void onStop() {
    db2.close();
    super.onStop();
}
}

i want to update the table Studentprofile1 in the database ElectiveDataBase2 . if the student enter his studentid, name, password and click submit button that should be updated in the database.. please help to solve this

You are missing the student id parameter in your "where" part of the update:

db2.update("Studentprofile1", // table
          values,             // values
          "studentid"+"=?",   // where clause
          new String[] {eid.getText().toString()}) // parameters for the where clause
public long editFenceInfo(int id, String title, String area, String desc,
            String tag) {
        ContentValues con = new ContentValues();
        con.put(TITLE, title);
        con.put(AREA, area);
        con.put(DESCR, desc);
        con.put(TAG, tag);

        return myDataBase.update(MASTERFENCE_TABLE, con, KEY_ID + "=" + id,
                null);

    }

hii..

you try this typ method your project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM