为什么没有在数据库中插入值?

时间:2016-01-20 03:25:34

标签: android database android-sqlite

我的数据库代码如下所示。

        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();

活动代码是:         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();
            }}
            });

显示成功保存的消息,但未在数据库中找到该值。请告诉我为什么会这样?

3 个答案:

答案 0 :(得分:1)

尝试放置:

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

之前:

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

答案 1 :(得分:0)

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

这有效..

答案 2 :(得分:0)

但使用

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

即关闭数据库并启动活动