作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我在数据库中搜索数据时,我的应用程序被迫停止。只有当行数超过 2 行时才会出现这种情况,否则就没有问题。我可以很好地插入数据。我该如何解决这个问题?
//click event
public void OnButtonClick(View v) {
dbhelper.open();
EditText a = (EditText)findViewById(R.id.user_tf);
String User_Name = a.getText().toString();
EditText b = (EditText)findViewById(R.id.password_tf);
String Password = b.getText().toString();
sqlitedatabase = dbhelper.getReadableDatabase();
String password = dbhelper.searchpass(User_Name);
if(Password.equals(password)) {
Toast.makeText(getBaseContext(), "User and pass correct", Toast.LENGTH_LONG).show();
dbhelper.close();
Intent intent = new Intent(Login_activity.this, Homepage.class);
startActivity(intent);
} else {
Toast.makeText(getBaseContext(),"User or pass incorrect", Toast.LENGTH_LONG).show();
dbhelper.close();
Intent intent = new Intent(Login_activity.this, Login_activity.class);
startActivity(intent);
}
// Searchpass in DBHelper class
public String searchpass(String user_name) {
db.isOpen();
db = this.getReadableDatabase();
String query = "SELECT " + UserConstruct.newUserinfo.UserName + ", " + UserConstruct.newUserinfo.Password + " FROM " + UserConstruct.newUserinfo.TableName + " ";
// int a = query.length();
Cursor cursor = db.rawQuery(query, null);
String a, b;
b = "not found";
do {
if (cursor.moveToFirst()) {
a = cursor.getString(0);
if (a.equals(user_name)) {
b = cursor.getString(1);
}
}
} while (cursor.moveToNext());
return b;
}
最佳答案
在此,您同时使用了 cursor.moveToNext
和 cursor.movetoFirst
。我认为你不应该两者都做。使用 cursor.moveToNext
while (cursor.moveToNext()) {
a = cursor.getString(0);
if (a.equals(user_name)) {
b = cursor.getString(1);
}
}
关于java - 当我搜索数据时,我的应用程序被迫停止。我该如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39872462/
我是一名优秀的程序员,十分优秀!