j11488 2023-03-03 21:22 采纳率: 100%
浏览 50
已结题

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

  • 写回答

4条回答 默认 最新

  • dahe0825 2023-03-03 21:36
    关注

    参考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},来代替参数占位符中的 ?,以此来查询指定的账号。

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 3月11日
  • 已采纳回答 3月3日
  • 赞助了问题酬金15元 3月3日
  • 修改了问题 3月3日
  • 展开全部

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程