【问题标题】:Compare String in Database to String in Android Studio将数据库中的字符串与 Android Studio 中的字符串进行比较
【发布时间】:2019-12-13 20:19:00
【问题描述】:

我正在尝试将我的 SQLite 数据库中的列中的字符串与我在使用游标和数据库助手时创建的另一个字符串进行比较。

当我运行应用程序时,不管数据库有什么,应用程序只进入 UserMenu.class

部分代码: (注释掉的东西是我在尝试各种东西。)


public void onClick(View v) {

    String email = Username.getText().toString();

    String pass = Password.getText().toString();


    String role = " ";

    String test2 = "Admin"; 

    cursor = db.rawQuery("SELECT * FROM "+ DatabaseHelper.TABLE_NAME + " WHERE "+ DatabaseHelper.COL_3+ " =? AND "+ DatabaseHelper.COL_2+ "=?",new String[]{email, pass});
    if (cursor!=null){
        if (cursor.getCount()> 0){

            db.rawQuery("SELECT * FROM "+ DatabaseHelper.TABLE_NAME + " WHERE "+ DatabaseHelper.COL_4+ " =?", new String[]{role});
            if(role.equals(test2)) {
                cursor.moveToNext();
                Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_SHORT).show();
                //cursor = db.rawQuery("SELECT * FROM "+ DatabaseHelper.TABLE_NAME + " WHERE "+ DatabaseHelper.COL_3+ " =?", new String[]{role2});
                // Toast.makeText(getApplicationContext(),"Cursor count: "+cursor.getCount(),Toast.LENGTH_SHORT).show();
                Intent i = new Intent(MainActivity.this, AdminMenu.class);
                startActivity(i);
            } else {
                cursor.moveToNext();
                //moveToUser();
                Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_SHORT).show();
                // = db.rawQuery("SELECT * FROM "+ DatabaseHelper.TABLE_NAME + " WHERE "+ DatabaseHelper.COL_3+ " =?", new String[]{role});
                //Toast.makeText(getApplicationContext(),"Cursor count: "+cursor.getCount(),Toast.LENGTH_SHORT).show();
                Intent i = new Intent(MainActivity.this, UserMenu.class);
                startActivity(i);
            }

        } else {
            Toast.makeText(getApplicationContext(),"Invalid email and/or password. Please try again.",Toast.LENGTH_SHORT).show();
        }
    }
}

【问题讨论】:

    标签: java database sqlite android-studio


    【解决方案1】:

    db.rawQuery("SELECT * FROM "+ DatabaseHelper.TABLE_NAME + " WHERE "+ DatabaseHelper.COL_4+ " =?", new String[]{role});

    作为结果用处不大,它返回的Cursor,没有被使用。

    if(role.equals(test2)) 行将始终为 false,因为角色将为“”,而 test2 将为“管理员”。所以你总是去userMenu。

    我猜你想要类似于

    cursor = db.rawQuery("SELECT * FROM "+ DatabaseHelper.TABLE_NAME + " WHERE "+ DatabaseHelper.COL_3+ " =? AND "+ DatabaseHelper.COL_2+ "=?",new String[]{email, pass});
    if (cursor.moveToFirst()){
    
        //db.rawQuery("SELECT * FROM "+ DatabaseHelper.TABLE_NAME + " WHERE "+ DatabaseHelper.COL_4+ " =?", new String[]{role});
        if (cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_4)).equals(test2)) {
            Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_SHORT).show();
            Intent i = new Intent(MainActivity.this, AdminMenu.class);
            cursor.close();
            startActivity(i);
        }
        else{
            Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_SHORT).show();
            Intent i = new Intent(MainActivity.this, UserMenu.class);
            cursor.close();
            startActivity(i);
        }
    
    } else {
        Toast.makeText(getApplicationContext(),"Invalid email and/or password. Please try again.",Toast.LENGTH_SHORT).show();
        cursor.close();
    }
    

    不能很好地检查 Cursor 是否为空,它不是来自查询的空。查询失败或获得良好的光标。如果没有行则光标光标不移动。

    使用 moveToFirst 替换 getCount,因为如果 count 为 0,则 moveToFirst 将为 false,否则将光标位置移动到第一行,并且可以从光标中获取值。两件事也是如此。

    不知道找到的行是否具有正确的角色值,但现在使用 cmets 进行的其他查询将获取所有具有角色的行,而不仅仅是来自电子邮件行,因此任何 ADMIN 并且始终是 AdminMenu。所以我认为只需要第一个查询。

    【讨论】:

    • if (cursor.getString(DatabaseHelper.COL_4).equals(test2)) { 这部分代码得到一个错误,上面写着“ getString(int) in Cursor cannot be applied to (boolean) ”有什么办法解决吗?
    • @MarcosC 我的代码错误,抱歉。已编辑答案我忘记包括cursor.getColumnByIndex(DatabaseHelper.COL_4),这会从列名中获取列索引。 cursor.getString(?) 需要吗?是列索引(int)而不是列名(字符串)。没有运行测试代码,只是进行了简单的语法检查。
    【解决方案2】:

    你已经定义了:

    String role = " ";
    String test2 = "Admin";
    

    并且这些变量不会在您的代码中的任何地方更改。
    所以条件:

    role.equals(test2)
    

    返回false 并执行else 块。

    【讨论】:

      猜你喜欢
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      • 2017-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 2020-04-15
      相关资源
      最近更新 更多