Niroj
Niroj

Reputation: 1114

Why the values are not being inserted in the database?

My database code looks like this.

        public void addprofileinfo(HashMap<String, String> queryValues){

        SQLiteDatabase database = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put("StudentName", queryValues.get("StudentName"));
        values.put("RegistrationNo", queryValues.get("RegistrationNo"));
        values.put("Faculty", queryValues.get("Faculty"));
        values.put("TotalFeePaid", queryValues.get("TotalFeePaid"));

        database.insert("FEE", null, values);

        database.close();

And Activity Code is: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.insertfield); editName=(EditText)findViewById(R.id.editTextName); editFaculty=(EditText)findViewById(R.id.editTextfaculty); editFee=(EditText)findViewById(R.id.editTexttotalfeepaid); editRegid=(EditText)findViewById(R.id.editTextid); save = (Button) findViewById(R.id.buttonsave);

addData();
}

public void addData(){
    save.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            try{

                HashMap<String, String> queryValuesMap = new HashMap<String, String>();

            queryValuesMap.put("StudentName",editName.getText().toString());
            queryValuesMap.put("Faculty", editFaculty.getText().toString());
            queryValuesMap.put("TotalFee",      editFee.getText().toString());
            queryValuesMap.put("RegistrationNo",        editRegid.getText().toString());

            dbtools.addprofileinfo(queryValuesMap);
            Toast.makeText(insertData.this, "Successful!!!", Toast.LENGTH_LONG).show();

            Intent intent;
            intent = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(intent);
            dbtools.close();
            insertData.this.finish();



            }catch(Exception e){
                Toast.makeText(insertData.this, e.getMessage(), Toast.LENGTH_LONG).show();
            }}
            });

Successful saved message appears but the value is not found into database. Please tell why is this happening?

Upvotes: 3

Views: 184

Answers (3)

Niroj
Niroj

Reputation: 1114

but it is more better to use

    dbtools.close();
    insertData.this.finish(); 
    Intent intent;
    intent = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(intent);

i.e close your database and start the ativity

Upvotes: 0

Niroj
Niroj

Reputation: 1114

 Intent intent;
        intent = new Intent(getApplicationContext(), MainActivity.class);
        startActivity(intent);
        dbtools.close();
        insertData.this.finish();

This works..

Upvotes: 0

Rohit Sharma
Rohit Sharma

Reputation: 2017

Try to put:

        dbtools.close();
        insertData.this.finish(); 

Before:

        Intent intent;
        intent = new Intent(getApplicationContext(), MainActivity.class);
        startActivity(intent);

Upvotes: 2

Related Questions