我正在尝试查找用户名是否存在于数据库中,如果存在,则布尔变量将设置为 true,否则将设置为 false。即使表中不存在用户名,该变量似乎也始终设置为 true。我检查了表中的数据并尝试了很多次,但变量始终设置为 true,并且无论用户名是否在数据库中,始终显示消息“用户名存在”。这是我的代码。我究竟做错了什么?
----登录类,定义按钮和编辑文本字段并在单击登录按钮时触发消息-------------
public class Login extends Activity{
Button sqllogin;
EditText sqlusername, sqlpassword;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sqlusername = (EditText) findViewById(R.id.etuname1);
sqlpassword = (EditText) findViewById(R.id.etpass);
sqllogin = (Button) findViewById(R.id.bLogin);
sqllogin.setOnClickListener(new View.OnClickListener() {
//Method that fires a message when the Login button is clicked
public void onClick(View v) {
switch (v.getId()){
case R.id.bLogin:
String username = sqlusername.getText().toString();
String password = sqlpassword.getText().toString();
DBAdapter entry = new DBAdapter(Login.this);
entry.open();
boolean r = entry.findUsername(username);
if(r == true){
Toast.makeText(Login.this, "Username Exists", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(Login.this, "Username does not Exists", Toast.LENGTH_LONG).show();
}
entry.close();
break;
}
}
});
}
}
---------DBAdpater 类中检查用户名是否存在于表中的方法------------
public boolean findUsername(String username) {
Cursor c = ourDatabase.query(true, DATABASE_TABLE_L, new String[] {
KEY_USERNAME, KEY_PASSWORD}, KEY_USERNAME + "='"
+ username +"'", null, null, null, null, null);
if (c!= null) {
return false;
}
else{
return false;
}
}