错误在任何if语句或查询 [英] Error in either if statement or in query

查看:179
本文介绍了错误在任何if语句或查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有我想知道的是天气我的问题在于我的如果语句或我的查询...如果你想成为慷慨,帮我比,这将是AP preciated但作为一个初学者我觉得它的对我好麻烦拍摄自己。

All I want to know is weather my problem resides in my "if" statement or in my query... If you want to be generous and help me further than that it would be appreciated but as a beginner I feel its good for me to trouble shoot for myself.

我的问题是问题,任何什么价值我投入EDITTEXT框中的结果返回true(带我去我的应用程序的下一个画面)。除非我离开它留空它,然后做什么,它应该是显示文字说在我的问题在于您没有输入密码,有人可以告诉我吗?

My problem is that no matter what value I put into the editText box the result is returning true(taking me to the next screen of my app). Unless I leave it blank which it is then doing what it is supposed to which is showing a text saying "You did not enter a password" Can someone please tell me where my problem lies?

public void buttonWork() {
        button_credCheck.setOnClickListener(new View.OnClickListener() {

        @Override
       public void onClick(View v) {
        Integer rpq = regPwdQuery();
            String isEnteredStr = editText_pwdInput.getText().toString();
            if (TextUtils.isEmpty(isEnteredStr)) {
                Toast.makeText(LogInActivity.this, "You did not enter a password!", Toast.LENGTH_LONG).show();
            }
            else if
                (rpq.equals(1)) {
                Intent myIntent = new Intent(LogInActivity.this, FindInfoActivity.class);
                startActivity(myIntent);
            }
            else {
                Toast.makeText(LogInActivity.this, "Incorrect Password", Toast.LENGTH_LONG).show();
            }
        }
    });
}

public Integer regPwdQuery() {
    int count = 0;
    String regPwdData = editText_pwdInput.getText.toString();
    String regQuery = "SELECT COUNT(*) AS COUNT FROM UsrPass_table WHERE Pwrd ='" + regPwdData + "'";
    SQLiteDatabase uDB = usrDB.getReadableDatabase();
    Cursor cursor = uDB.rawQuery(regQuery, null);
    while (cursor.moveToNext()){
       count = cursor.getCount();
    }
    cursor.close();
    return count;
}

编辑:
我试图改变我否则,如果到:rpq.equals(2),因为我被告知getCount将总是返回1.这引起了所有密码(即使是正常数值,验证了DB存在)来将挡说法不正确的密码..所以如果我离开我的if语句来检查1,那么它不会阻止不正确的密码,如果设置我的if语句来检查2,那么它会阻止一切..我的假设是错误的是我的查询某处.. 。也许我需要一种方法来加1CNT如果查询返回true ......我试着在我的regPwdQuery的几个不同的地方,但没有运气加入了CNT ++ ..谁能帮助我?

I have tried changing my else if to: rpq.equals(2) since I was told that getCount will always return 1. This caused the all passwords (even correct ones, verified to exist in DB) to be blocked saying incorrect password.. so if I leave my if statement to check for a 1 then it doesn't block incorrect passwords and if I set my if statement to check for 2 then it blocks everything.. my assumption is that the error is in my query somewhere... maybe I need a way to add 1 to "cnt" if the query returns true... I tried adding a cnt++ in a few different places of my regPwdQuery but no luck.. can anyone help me?

推荐答案

在试图与数据库核对密码,而不是的onclick函数内部的点击监听器设置!
移动

you are trying to check the password with database while setting on click listener not inside the onclick function!! Move

String rpq = regPwdQuery().toString();   

的onclick函数内。

inside onclick function.

和由@Blackbelt指出,内部regPwdQuery()使用获得的EditText文本

and as pointed out by @Blackbelt, inside regPwdQuery() get text from edittext using

editText_pwdInput.getText().toString().

侧面说明:

获取readableDatabase只要您不将任何内容写入数据库。

get readableDatabase whenever you are not writing anything to database.

是不是更好直接比较整数。我不明白为什么它需要进行比较的字符串进行转换。你可以

Isn't better to directly compare integers. I dont see why it needs to be converted to strings to be compared. you can

替换

rpq.equals("1")

if (regPwdQuery() == 1) 

分贝未关闭。检查它是否意。

Db is not closed. check if it is intended.

在查询中摆脱AS计数,因为你没有被查询做任何组。

Get rid of AS COUNT in your query since you are not doing any group by query.

假设错误taht getCount将默认返回1。它给0的情况下,没有记录是匹配的。既然你一直得到1作为结果,不管是什么,你总是有一个记录匹配。

Wrong assumption taht getCount returns 1 by default. It gives 0 in case no records are matching. Since you are consistently getting 1 as result, no matter what, you always have one record matching.

这一个记录是由数作为其价值之一。所以你需要查询它得到计数。 (如果您的计数为0,你也有一个光标记录给结果作为0)

this one record is the one which consists of count as its value. so you need to query it to get count. (In case your count is 0 also you have one cursor record to give the result as 0)

最后请尝试以下code。而不是

finally please try following code. Instead of

count = cursor.getCount();

您可以尝试用这个?

count= cursor.getInt(0);

这篇关于错误在任何if语句或查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆