Android 在使用sqlite完成注册功能时,使用if语句判定注册账号是否和数据库内账号列一致,使用equlas判定一直为否,将两个string强制转换类型后进行==比较依然为否。想询问一下如何才能在此基础上实现该功能。
即,无论账号是否重复,运行结果一直显示else里的内容,除了使用账号:123,密码:123,这个例子能成功进入if。
public void login(View view){//对应注册按钮
EditText account=findViewById(R.id.zhanghao);
EditText password=findViewById(R.id.pass);
String got_account=account.getText().toString();
String got_password=password.getText().toString();
SQLiteOpenHelper helper=MySqliteOpenHelper.getmInstance(this);
SQLiteDatabase db=helper.getReadableDatabase();
int a = Integer.parseInt(got_account);
String sq = "select * from users";
Cursor cursor=db.rawQuery(sq,null);//取users数据表中所有用户数据,cursor是迭代游标,用于遍历操作
if (db.isOpen()){//判断数据库是否成功打开
while(cursor.moveToNext()) {
int zhanghaoColumnIndex = cursor.getColumnIndex("_account");
String _account = cursor.getString(zhanghaoColumnIndex);
int i = Integer.parseInt(_account);
if (a==i){
Toast.makeText(DL.this, "账号重复", Toast.LENGTH_SHORT).show();
break;
}else {
String sql = "insert into users(_account,_password) values(?,?)";//因为注册账号和密码需要获取用户输入的信息,所以我们暂时用问号代替,下面再用Object替代
db.execSQL(sql, new Object[]{got_account, got_password});
Toast.makeText(DL.this, "注册成功", Toast.LENGTH_SHORT).show();
break;
}
}
cursor.close();//关闭迭代游标
db.close();//关闭数据库
}