首页 文章

用户名和密码登录android登录sqlite

提问于
浏览
-1

我正在工作sqlite.i成功创建数据库和table.and我也可以注册一些新用户 . 我也用户名写入登录功能只是这是我的代码的一部分

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.how我可以写两个参数功能巫婆可以一起检查用户名和密码吗?

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;
}

我必须改变我的光标 . 如果有人知道解决方案,请帮助我

2 回答

  • 0

    用双?并在数组中发送两个参数 .

    喜欢:

    Cursor cursor = db.query(usm_Users, null, " UserName=? and Password=?",
                new String[] { username,password }, null, null, null);
    
  • 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;
    }
    

相关问题