带用户名和密码的android登录sqlite
我正在使用sqlite。我成功地创建了数据库和表。我还可以注册一些新用户。而且我只使用用户名编写了登录函数 这是我代码的一部分带用户名和密码的android登录sqlite,android,sqlite,android-sqlite,android-cursor,Android,Sqlite,Android Sqlite,Android Cursor,我正在使用sqlite。我成功地创建了数据库和表。我还可以注册一些新用户。而且我只使用用户名编写了登录函数 这是我代码的一部分 public String getSinlgeEntry(String username) { Cursor cursor = db.query(usm_Users, null, " UserName=?", new String[] { username }, null, null, null); if (cursor.getC
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();
}
}
公共字符串getSinlgeEntry(字符串用户名){
Cursor Cursor=db.query(usm_用户,null,“UserName=?”,
新字符串[]{username},null,null,null);
if(cursor.getCount()<1)//用户名不存在
{
cursor.close();
返回“不存在”;
}
cursor.moveToFirst();
字符串customername=cursor.getString(cursor
.getColumnIndex(“用户名”);
cursor.close();
返回客户名称;
}
字符串username=namefild.getText().toString();
字符串密码=passwordfild.getText().toString();
String storedusername=loginDataBaseAdapter.getSinlgeEntry(用户名);
if(username.equals(storedusername))
{
Toast.makeText(MainActivity.this,“恭喜:登录成功”,Toast.LENGTH_LONG.show();
意向ii=新意向(MainActivity.this,Home.class);
星触觉(ii);
}
其他的
if(username.equals(“”){
Toast.makeText(MainActivity.this,“请输入密码”,Toast.LENGTH_LONG.show();
}
其他的
{
Toast.makeText(MainActivity.this,“密码不正确”,Toast.LENGTH_LONG.show();
}
}
我可以只检查登录用户名。我如何写两个参数的功能,将检查用户名和密码一起
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;
}
public String getSinlgeEntry(字符串用户名、字符串密码){
Cursor Cursor=db.query(usm_用户,null,“UserName=?”,
新字符串[]{username},null,null,null);
if(cursor.getCount()<1)//用户名不存在
{
cursor.close();
返回“不存在”;
}
cursor.moveToFirst();
字符串customername=cursor.getString(cursor
.getColumnIndex(“用户名”);
cursor.close();
返回客户名称;
}
我必须更改光标上的某些内容。如果有人知道解决方案,请帮助我使用double?并在数组中发送两个参数 比如:
您可以使用下面的函数,这是更通用的
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;
}
谢谢,我添加了代码。我在按钮单击中进行验证。目前我如何编写if选项?\n if选项是什么意思?剩下的代码应该没问题。我的意思是,你应该在getSinlgeEntry函数username.equals(storedusername)中使用我的行。用户名是edit text.gettext.tostring,您的函数有2个参数,在我的if语句中,我只检查了用户名的get text.tostring@AdemI不知道您的布局,但您似乎已经有了密码字段。您将使用字符串password=passwordfild.getText().toString()获取值;因此,您只需要使用用户名和密码调用getSinlgeEntry函数。不要按程序检查密码。字符串username=namefild.getText().toString();字符串密码=passwordfild.getText().toString();字符串结果=loginDataBaseAdapter.getSinlgeEntry(用户名、密码);如果(结果等于(“不存在”))打印(“检查登录”);else打印(“您成功登录”);谢谢。但是我怎么能使用这个代码呢?我的意思是编辑文本验证。我也编写了这段代码,但不起作用。您可以将编辑文本值作为参数传递给函数(例如)Boolean check=check_User_Pin_Number(username.getText().toString(),password.getText().toString())。如果函数返回“true”,则允许用户继续,否则不允许。
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;
}