无法更新数据库中的值

时间:2016-01-04 03:34:37

标签: java android

我想更新数据库中的值并使用按钮

启动它
Button b1 = (Button) findViewById(R.id.update);
b1.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v){
        ContentValues cv=new ContentValues(); 
        cv.put("word",word); 
        cv.put("definition", def); 
        database.rawQuery("update priv_dbms set word ='"+word+"',
                              definition='"+def+"'", null);
        Toast.makeText(getApplicationContext(),
                    "Successfully Updated!!",
        Toast.LENGTH_SHORT).show();
        Intent intent = new Intent (v.getContext(), Priv_DBMS.class);
        startActivityForResult(intent, 0);
    }
});

当我启动应用程序时,没有错误,但数据库中的值不会更新。

这是我从编辑文本中获取值的代码:

passedVar=getIntent().getStringExtra(WordExtend_priv_dbms.ID_EXTRA2);
passedView = (EditText)findViewById(R.id.editword);
passedView.setText(passedVar);
passedVar1=getIntent().getStringExtra(WordExtend_priv_dbms.ID_EXTRA1);
passedView1 = (EditText)findViewById(R.id.editdef);
passedView1.setText(passedVar1)
word = passedView.getText().toString();
def = passedView1.getText().toString();

2 个答案:

答案 0 :(得分:1)

在onclick ::

中试试这个
String query = "update priv_dbms set word =\""+word+"\", definition=\""+def+"\"";

SQLiteDatabase db = getWritableDatabase();
db.execSQL(query);

db.close();

Toast.makeText(getApplicationContext(),
                    "Successfully Updated!!",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent (v.getContext(), Priv_DBMS.class);
startActivityForResult(intent, 0);

答案 1 :(得分:0)

我忘了把它放在onClick

String word = passedView.getText().toString();
String def = passedView1.getText().toString();

passedViewpassedView1来自此处

passedView = (EditText)findViewById(R.id.editword);
passedView1 = (EditText)findViewById(R.id.editdef);

感谢您的回答!!