Why the values are not being inserted in the database?
我的数据库代码如下所示。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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(); }} }); |
将显示成功保存的消息,但未在数据库中找到该值。 请告诉我为什么会这样?
试着把:
1 2 | dbtools.close(); insertData.this.finish(); |
之前:
1 2 3 | Intent intent; intent = new Intent(getApplicationContext(), MainActivity.class); startActivity(intent); |
但它更好用
1 2 3 4 5 | dbtools.close(); insertData.this.finish(); Intent intent; intent = new Intent(getApplicationContext(), MainActivity.class); startActivity(intent); |
即关闭数据库并启动动画
1 2 3 4 5 | Intent intent; intent = new Intent(getApplicationContext(), MainActivity.class); startActivity(intent); dbtools.close(); insertData.this.finish(); |
这有效..