使用SQLite更新密码

时间:2014-01-08 04:53:46

标签: android android-sqlite

我正在使用SQLite开发一个应用程序。我的要求是我想更新表格中的密码。

我写了一个更新查询,但它无法正常工作。错误:

android.database.sqlite.SQLiteException: unrecognized token: "' WHERE username = ?": , 
while compiling: UPDATE TABLE_USER SET password = god' WHERE username = ?` condition.   

代码:

UserDaoImpl.java

public void  updateEntry(String newPassword,String name)
 {
    Cursor cur= myDB.rawQuery("UPDATE "+UserDBHandler.USER_TABLE+" SET    
 "+UserDBHandler.PASSWORD+" = "+newPassword+"' WHERE "+ 
                   UserDBHandler.USER_NAME+" = ?",new String[]{name});  
 }       

  ChangePasswordActivity.java


public class ChngePasswordActivity extends Activity {

  private EditText oldPwdEdit;
  private EditText newPwdEdit;
  private EditText cnfrmEdit;

  private Button submitBtn;

  private String oldPwd;
  private String newPwd;
  private String cnfrmPwd;

  private UserDaoImpl userDetalsIml;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_changepassword);

    //Bundle extra = this.getIntent().getExtras();
    //String name= extra.getString("udp");
    Intent intent2=getIntent();
    final String name1=intent2.getStringExtra("val");
    Log.e("prasad2",name1);

    oldPwdEdit = (EditText)findViewById(R.id.oldEdt);
    newPwdEdit = (EditText)findViewById(R.id.newpswEdt);
    cnfrmEdit  = (EditText)findViewById(R.id.pswEdt);  

    submitBtn = (Button)findViewById(R.id.cnfrmPwdBtn);

   submitBtn.setOnClickListener(new OnClickListener() {

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


            oldPwd = oldPwdEdit.getText().toString().trim();
            newPwd = newPwdEdit.getText().toString().trim();
            cnfrmPwd=cnfrmEdit.getText().toString().trim();

            if(oldPwd==null||"".equalsIgnoreCase(oldPwd)){
                String header = "OLD PASSWORD REQUIRE";

                          Toast.makeText(getApplicationContext(),header,100).show();
            }
            else if(newPwd==null ||"".equalsIgnoreCase(newPwd)){
                String header = "NEW PASSWORD IS REQUIRE";
                Toast.makeText(getApplicationContext(), header,   
                                   100).show();
            }
            else if(cnfrmPwd==null ||"".equalsIgnoreCase(cnfrmPwd)){
                String header = "COINFIRM PASSWORD IS REQUIRE";
                Toast.makeText(getApplicationContext(), header, 
                                   100).show();
            }
            else if(!newPwd.equalsIgnoreCase(cnfrmPwd)){
                String header = "PASSWORD DOES NOT MATCH";
                Toast.makeText(getApplicationContext(), header,   
                                   100).show();
            }
            else{

                userDetalsIml = new   
   UserDaoImpl(getApplicationContext());

                userDetalsIml.updateEntry(newPwd,name1);


            }       

        }
    });  
  }
}

2 个答案:

答案 0 :(得分:5)

您的查询中缺少'此处UserDBHandler.PASSWORD + “=”

试试这个更新的

Cursor cur= myDB.rawQuery("UPDATE "+UserDBHandler.USER_TABLE+" SET    
 "+UserDBHandler.PASSWORD+" = '"+newPassword+"' WHERE "+ 
                   UserDBHandler.USER_NAME+" = ?",new String[]{name}); 

答案 1 :(得分:4)

哎呀!!你犯了很多错误。

请替换您的代码

Cursor cur= myDB.rawQuery("UPDATE "+UserDBHandler.USER_TABLE+" SET    
 "+UserDBHandler.PASSWORD+" = "+newPassword+"' WHERE "+ 
                   UserDBHandler.USER_NAME+" = ?",new String[]{name}); 

通过

 Cursor cur= myDB.rawQuery("UPDATE "+UserDBHandler.USER_TABLE+" SET    
 "+UserDBHandler.PASSWORD+" = '"+newPassword+"' WHERE "+ 
                   UserDBHandler.USER_NAME+" = ?",new String[]{name});

newPassword之前只缺少'