简体   繁体   English

将数据库中的字符串与 Android Studio 中的字符串进行比较

[英]Compare String in Database to String in Android Studio

I am trying to compare a String in a Column in my SQLite Database to another String that i created while using cursor and Database Helper.我正在尝试将我的 SQLite 数据库中列中的字符串与我在使用游标和数据库助手时创建的另一个字符串进行比较。

When i run the app despite what the database has, the app only goes to the UserMenu.class当我运行应用程序时,不管数据库有什么,应用程序只会转到 UserMenu.class

Part of code: (the things commented out is me trying various things out. )部分代码:(注释掉的是我尝试了各种东西。)


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

You have defined:您已定义:

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

and these variables are not changed anywhere in your code.并且这些变量不会在您的代码中的任何地方更改。
So the condition:所以条件:

role.equals(test2)

returns false and the else block is executed.返回false并执行else块。

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

is not much use as the result, the Cursor it returns, is not used.结果没有多大用处,它返回的 Cursor 没有使用。

The line if(role.equals(test2)) will always be false as role will be " " and test2 will be "Admin". if(role.equals(test2))行将始终为 false,因为 role 将是“”,而 test2 将是“Admin”。 So you always go to userMenu.所以你总是去 userMenu。

I guessing you want similar to我猜你想要类似于

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

No good checking if Cursor is null it not be null from query.不好检查 Cursor 是否为 null 它不是从查询中为 null。 query fail or get good Cursor.查询失败或获得好的 Cursor。 Cursor if no rows Cursor not move.光标如果没有行光标不移动。

Using moveToFirst replaces getCount as moveToFirst will be false if count 0 if not it move the Cursor position to first row and values can be got from the Cursor.使用 moveToFirst 替换 getCount,因为如果计数为 0,则 moveToFirst 将为 false,否则它将光标位置移动到第一行,并且可以从光标中获取值。 So does two things.两件事也是如此。

Not know if row found has proper value for role but other query with comments now will get all row with role not just from email row so any ADMIN and always AdminMenu.不知道找到的行是否具有适当的角色值,但其他带有注释的查询现在将获得所有具有角色的行,而不仅仅是来自电子邮件行,因此任何 ADMIN 和始终是 AdminMenu。 So first query only needed I think.所以我认为只需要第一个查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM