public boolean isPasswordOK(String name,String password){//判断密码是否正确
try{
SQLiteDatabase db=helper.getReadableDatabase();
Cursor cursor=db.rawQuery(Users.QUERY_NAME,new String[]{name});
while ( cursor.moveToNext()) {//这里只有一个对象被查询或没有
String pwd = cursor.getString(cursor.getColumnIndex(Users.CLOMNS[2]));//查询得到的密码
db.close();
if(pwd.equals(password)){//判断密码是否正确
return true;
}
}
}catch (Exception e){
e.printStackTrace();
}
return false;
}
public boolean isNameExist(String name) {//判断用户名是否重复
try {
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery(Users.QUERY_NAME, new String[]{name});
cursor.moveToLast();
if(cursor.getCount()<=0){
return false;
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
注意这里我们使用cursor.moveToLast();cursor.getCount()来获取cursor的步程,如何步程大于零证明数据已存在。
在使用数据库对象时要采用try,catch,不然会闪退。