我是Android编程的新手。我有一个包含电子邮件和密码的SQLite数据库,我想在新用户创建新帐户时检查用户是否已经存在。谁能帮我 ? 这是我检查用户是否存在的方法。
usersAcc.setEmailUser(e);
usersAcc.setPasswordUser(a);
String query = "SELECT email FROM Emails WHERE email='"+ usersAcc.getEmailUser() +"';";
Cursor cursor = helper.sqlite.rawQuery(query, null);
cursor.moveToFirst();
if(cursor.moveToNext()){
helper.intput(usersAcc);
Toast toast = Toast.makeText(RegistrationForm.this, "OK!", Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0,0);
toast.show();
}else{
Toast toast = Toast.makeText(RegistrationForm.this, "Fail!", Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0,0);
toast.show();
}
答案 0 :(得分:0)
您没有正确使用光标移动,因为在移动到第一个之后您将移动到不存在的下一个,请使用以下方法:
usersAcc.setEmailUser(e);
usersAcc.setPasswordUser(a);
String query = "SELECT email FROM Emails WHERE email='"+ usersAcc.getEmailUser() +"';";
Cursor cursor = helper.sqlite.rawQuery(query, null);
if(cursor.moveToFirst()){
helper.intput(usersAcc);
Toast toast = Toast.makeText(RegistrationForm.this, "OK!", Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0,0);
toast.show();
}else{
Toast toast = Toast.makeText(RegistrationForm.this, "Fail!", Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0,0);
toast.show();
}
答案 1 :(得分:0)
不要使用if(cursor.moveToNext())
--->如果光标已经超出结果集中的最后一个条目,则此方法将返回false。
您应该使用if(cursorOBJECT.moveToFirst())
。
将光标移到第一行。如果 光标为空。
Cursor cursor=helper.sqlite.rawQuery("SELECT * FROM user WHERE email ='"+ usersAcc.getEmailUser()+"'", null);
if(cursor.moveToFirst())
{
helper.intput(usersAcc);
Toast toast = Toast.makeText(RegistrationForm.this, "OK!", Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0,0);
toast.show();
}
else
{
Toast toast = Toast.makeText(RegistrationForm.this, "Fail!", Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0,0);
toast.show();
}
仅供参考
"java.lang.IllegalStateException: Could not execute method of the activity"
表示已在非法或不适当的时间调用了方法。换句话说,Java环境或Java应用程序未处于所请求操作的适当状态。