Java EclipseAndroid中的字符串空值
在我的代码中,我尝试在数据库中检查输入的secretInfo是否有空密码,如果是,它应该执行特定的功能。但是,代码可能有错误,因为它没有传递所需的if语句。 在我的数据库中,我有一个密码为空的秘密信息。对于此机密信息,我正在尝试通过我的代码添加密码。我尝试了toast以查看检索密码时得到了什么,但它写入的内容不存在,这意味着密码实际上是空的。但我不明白为什么它不通过if语句 这是函数的代码:Java EclipseAndroid中的字符串空值,java,android,eclipse,Java,Android,Eclipse,在我的代码中,我尝试在数据库中检查输入的secretInfo是否有空密码,如果是,它应该执行特定的功能。但是,代码可能有错误,因为它没有传递所需的if语句。 在我的数据库中,我有一个密码为空的秘密信息。对于此机密信息,我正在尝试通过我的代码添加密码。我尝试了toast以查看检索密码时得到了什么,但它写入的内容不存在,这意味着密码实际上是空的。但我不明白为什么它不通过if语句 这是函数的代码: if (loginDataBaseAdapter.getPassthruSecInfo(secretIn
if (loginDataBaseAdapter.getPassthruSecInfo(secretInfo) == null){
loginDataBaseAdapter.insertEntry(password);
Toast.makeText(getApplicationContext(), "Account Successfully Created",Toast.LENGTH_LONG).show();
}
if(loginDataBaseAdapter.getPassthruSecInfo(secretInfo) != null)
Toast.makeText(getApplicationContext(), "Account Already Exists",Toast.LENGTH_LONG).show();
}
这是getPassthruSecretInfo方法:
public String getPassthruSecInfo(String userName)
{
Cursor cursor=db.query("student", null, "SecretInfo"+"=?", new String[]{userName}, null, null, null);
if(cursor.getCount()<1)
return "DOES NOT EXIST";
cursor.moveToFirst();
String password= cursor.getString(cursor.getColumnIndex("StdPass"));
return password;
}
显然,SQL语句返回0行,因此cursor.getCount始终返回0。为了找出原因,我建议您使用从设备中提取SQLite数据库
然后,创建正确的SQL查询,然后将其输入到应用程序中。当前,您可能语法错误,因此没有返回任何行。我已从数据库中删除空值,并将密码留空。答案不是“null”值或“==”,而是.equals
if (loginDataBaseAdapter.getPassthruSecInfo(secretInfo).toString().equals("")){
loginDataBaseAdapter.insertEntry(password);
Toast.makeText(getApplicationContext(), "Account Successfully Created",Toast.LENGTH_LONG).show();
}
if(!loginDataBaseAdapter.getPassthruSecInfo(secretInfo).toString().equals(""))
Toast.makeText(getApplicationContext(), "Account Already Exists",Toast.LENGTH_LONG).show();
}
getPassthruSecInfo方法看起来如何?getPassthruSecInfo是否实际返回null?还是返回一个空字符串,例如?@REACHUS我刚刚在问题中包含了该方法。您的查询似乎有问题。@JasonC没有。我在数据库中写入了null,但我已经尝试在代码中用替换null,但没有效果。但是我在另一个方法中使用了相同的查询,它返回了1而不是0。。。这是itpublic字符串getSecretInfo字符串SecretInfo{String where=SecretInfo=?;Cursor Cursor=db.querystudent,null,where,新字符串[]{SecretInfo},null,null,null;ifcursor.getcount可能您传递的用户名在数据库中不存在?当然可以!!我不敢相信我没有注意到这些错误…非常感谢,我会修复它,现在就尝试它。它不起作用…即使getPassthruSecInfo方法返回null值,这意味着问题不再存在于该方法中.