我写的程序(关键部分),用来用户名,密码登陆匹配的
数据库创建:db.execSQL("create table user_pw(id int,name varchar(20),pw varchar(20))");
引用:DatabaseHelper dbHelper = new DatabaseHelper(KSIAActivity.this,"KSIA",null,1);
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("user_pw", new String[]{"name"}, "name like ?", new String[}
{username.getText().toString()}, null, null, null);//匹配输入的名字
if(cursor.moveToFirst()==false){
/*没有查找到,插入新的用户,已完成*/
}
if(cursor.moveToFirst()==true){//查找到
while(cursor.moveToNext()){
//输入密码与数据库密码匹配
if(cursor.getString(2).equals(password.getText().toString())){
Toast.makeText(getApplicationContext(), "登陆成功",Toast.LENGTH_LONG).show();
}
//输入密码错误
else{
Toast.makeText(getApplicationContext(), "用户名或密码错误",Toast.LENGTH_LONG).show();
}
}
}
可是运行时,无法进入while循环,只能进到if(cursor.moveToFirst()==true)判断语句,求各位大神指点怎么回事呢?(查询结果cursor只有一行)
------解决方案--------------------
这段要改
Cursor cursor = db.query("user_pw", new String[]{"name"}, "name like ?", new String[}
{username.getText().toString()}, null, null, null);
改成:
Cursor cursor = db.query("user_pw", null, "name like ?", new String[}
{username.getText().toString()}, null, null, null);
------解决方案--------------------
------解决方案--------------------
Cursor cursor = db.query("user_pw", new String[]{"name"}, "name like ?", new String[}
{username.getText().toString()}, null, null, null);//匹配输入的名字
你查询到的只有“name”这一列,所以要么用7楼的方法,要么改成cursor.getString(0)。因为getString(index)里index是你查询到的列的索引