我正在使用sqlite.i成功创建数据库和表。我也可以注册一些新用户。我也只用用户名编写登录功能 这是我的代码的一部分
public String getSinlgeEntry(String username) {
Cursor cursor = db.query(usm_Users, null, " UserName=?",
new String[] { username }, null, null, null);
if (cursor.getCount() < 1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String customername = cursor.getString(cursor
.getColumnIndex("UserName"));
cursor.close();
return customername;
}
String username=namefild.getText().toString();
String password=passwordfild.getText().toString();
String storedusername=loginDataBaseAdapter.getSinlgeEntry(username);
if(username.equals(storedusername))
{
Toast.makeText(MainActivity.this, "Congrats: Login Successfully", Toast.LENGTH_LONG).show();
Intent ii=new Intent(MainActivity.this,Home.class);
startActivity(ii);
}
else
if(username.equals("")){
Toast.makeText(MainActivity.this, "Please Enter Your Password", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this, "Password Incorrect", Toast.LENGTH_LONG).show();
}
}
我可以只登录username.h我可以写两个参数函数,巫婆可以一起检查用户名和密码吗?
public String getSinlgeEntry(String username,String password) {
Cursor cursor = db.query(usm_Users, null, " UserName=?",
new String[] { username }, null, null, null);
if (cursor.getCount() < 1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String customername = cursor.getString(cursor
.getColumnIndex("UserName"));
cursor.close();
return customername;
}
我必须改变我的光标。如果有人知道解决方案,请帮助我
答案 0 :(得分:0)
使用双?并在数组中发送两个参数。
像:
Cursor cursor = db.query(usm_Users, null, " UserName=? and Password=?",
new String[] { username,password }, null, null, null);
答案 1 :(得分:0)
您可以使用以下功能,这是更通用的
public Boolean check_User_Pin_Number(String username, String password)
{
SQLiteDatabase sql_Db = this.getReadableDatabase();
// Setting up the cursor which points to the desired table
Cursor cursor = sql_Db.rawQuery("SELECT * FROM " + usm_Users, null);
Boolean records_Exist = false;
// Checking if the table has values other than the header using the cursor
if(cursor != null && cursor.getCount() > 0)
{
// Moving the cursor to the first row in the table
cursor.moveToFirst();
do
{
// Checking if the user name provided by the user exists in the database
if(cursor.getString(cursor.getColumnIndex(NAME)).equals(username))
{
if(password.equals(cursor.getColumnIndex(PASSWORD))
{
records_Exist = true;
break;
}
}
}while(cursor.moveToNext()); // Moves to the next row
};
// Closing the cursor
cursor.close();
// Closing the database
sql_Db.close();
return records_Exist;
}