Android 注册功能如何实现账号查重

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();//关闭数据库
        }

参考GPT的内容和自己的思路,您可以尝试使用字符串比较方法 equals() 来比较两个字符串是否相等,而不是使用 == 运算符或强制类型转换后的 == 比较。

以下是修改后的代码示例:

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();

    String sq = "select * from users where _account = ?";
    Cursor cursor = db.rawQuery(sq, new String[]{got_account});

    if (cursor.getCount() > 0) {
        // 如果结果集中存在该账号,则表示账号已存在
        Toast.makeText(DL.this, "账号重复", Toast.LENGTH_SHORT).show();
    } else {
        // 否则插入新账号
        String sql = "insert into users(_account,_password) values(?,?)";
        db.execSQL(sql, new Object[]{got_account, got_password});
        Toast.makeText(DL.this, "注册成功", Toast.LENGTH_SHORT).show();
    }

    cursor.close();
    db.close();
}


在修改后的代码中,使用了一个 select 语句来查询数据库中是否已经存在该账号,如果结果集中存在该账号,则表示账号已存在,否则插入新账号。请注意,在 select 语句中使用了一个参数占位符 ? 来代替要查询的账号,这是为了避免 SQL 注入攻击。同时,在 rawQuery 方法中传入了一个数组参数 new String[]{got_account},来代替参数占位符中的 ?,以此来查询指定的账号。

回答不易,还请采纳!!!

参考GPT和自己的思路,在你的代码中,出现问题的地方在于while循环里的逻辑处理。

如果当前遍历到的用户账号和输入的账号不相等,就会进入else分支,然后执行插入数据操作,这样就会导致重复的账号被插入数据库中。

你可以把else分支中的代码移到while循环结束后,用一个变量记录是否存在相同的账号,最后根据这个变量来判断是否执行插入数据操作。

下面是修改后的代码示例:

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是迭代游标,用于遍历操作

    boolean hasSameAccount = false; // 标记是否存在相同的账号

    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();
                hasSameAccount = true; // 存在相同的账号
                break;
            }
        }
        cursor.close();//关闭迭代游标
        db.close();//关闭数据库

        if (!hasSameAccount) { // 如果不存在相同的账号,则插入数据
            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();
        }
    }
}

在修改后的代码中,我们新增了一个布尔类型的变量hasSameAccount,用于记录是否存在相同的账号。如果遍历完所有用户数据后,该变量的值仍为false,则说明不存在相同的账号,此时就可以执行插入数据操作了。

希望这个解答能够帮助你解决问题!

根据您的代码,我看到问题出在判断账号是否重复的部分。

首先,您将从数据库中读取的账号字符串 _account 转换成整型 i,但是这个转换可能会失败(比如 _account 里面不是一个合法的整数字符串),所以最好使用 try-except 语句捕获这个可能出现的异常。

其次,判断两个字符串是否相等应该使用 equals 方法而不是 ==,因为 == 判断的是两个字符串的对象引用是否相等,而 equals 判断的是两个字符串的值是否相等。

请尝试修改您的代码如下所示,看看能否解决问题:

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();

    String sq = "select * from users";
    Cursor cursor=db.rawQuery(sq,null);
    if (db.isOpen()){
        try {
            while(cursor.moveToNext()) {
                int zhanghaoColumnIndex = cursor.getColumnIndex("_account");
                String _account = cursor.getString(zhanghaoColumnIndex);
                if (got_account.equals(_account)){
                    Toast.makeText(DL.this, "账号重复", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        } catch (NumberFormatException e) {
            // 转换异常
            e.printStackTrace();
            Toast.makeText(DL.this, "账号格式不正确", Toast.LENGTH_SHORT).show();
            return;
        } finally {
            cursor.close();
            db.close();
        }

        // 能够执行到这里说明账号不存在重复
        String sql = "insert into users(_account,_password) values(?,?)";
        db.execSQL(sql, new Object[]{got_account, got_password});
        Toast.makeText(DL.this, "注册成功", Toast.LENGTH_SHORT).show();
    }
}

在你的代码中,if (a==i)这个条件判断是有问题的。因为你用getInt方法获取了数据库中账号的值,所以在比较时无需将其转换为整型,直接用字符串比较即可。

另外,你的代码中用了break语句,这会导致只判断了游标的第一条数据就结束了,可能会出现判断不准确的情况。建议将break语句去掉。

以下是修改后的代码示例:

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();

    String sq = "select * from users";
    Cursor cursor=db.rawQuery(sq,null);
    boolean isExist = false;
    if (db.isOpen()){
        while(cursor.moveToNext()) {
            String _account = cursor.getString(cursor.getColumnIndex("_account"));
            if (got_account.equals(_account)){
                isExist = true;
                break;
            }
        }
        cursor.close();
        db.close();
    }

    if(isExist){
        Toast.makeText(DL.this, "账号重复", Toast.LENGTH_SHORT).show();
    }else{
        String sql = "insert into users(_account,_password) values(?,?)";
        SQLiteDatabase writeDB = helper.getWritableDatabase();
        writeDB.execSQL(sql, new Object[]{got_account, got_password});
        writeDB.close();
        Toast.makeText(DL.this, "注册成功", Toast.LENGTH_SHORT).show();
    }
}


修改后的代码会遍历游标中的每一条数据,将isExist标记为true如果找到了与输入的账号相同的账号。如果isExist为true,说明账号已存在,显示"账号重复"的提示;否则执行插入数据的操作,显示"注册成功"的提示。

注意,在执行插入操作前,需要获取一个可写的数据库对象,因此需要调用helper.getWritableDatabase()方法。