简体   繁体   中英

How can i login in my android application using Username and Email both(SQLite Database used)?

I am working on sample in which user can login using their USERNAME and EMAIL both, but in my code it is allow USERNAME only and my method for authentication is below,

public String getSingleEntry(String userNameOrEmail)
    {

        Cursor cursor=db.rawQuery("select * from SIGNUP1 where USERNAME=?", new String[]{userNameOrEmail});


        if(cursor.getCount()<1) // UserName Not Exist
        {
            cursor.close();
            return "NOT EXIST";
        }
        cursor.moveToFirst();
        String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
        cursor.close();
        return password;                
    }

And my another method that will get all user data from the SQLite database,

public String getUserData(String userNameOrEmail){

        Cursor c = db.rawQuery("select * from SIGNUP1 where USERNAME="+"\""+userNameOrEmail+"\"", null); 


        c.moveToFirst();
        String result="";
        String iFirstName = c.getString(c.getColumnIndex("FIRSTNAME"));
        String iLastName = c.getString(c.getColumnIndex("LASTNAME"));
        String iUserName = c.getString(c.getColumnIndex("USERNAME"));
        String iEmail = c.getString(c.getColumnIndex("EMAIL"));
        String iPassword = c.getString(c.getColumnIndex("PASSWORD"));
        String iGender = c.getString(c.getColumnIndex("GENDER"));
        String iHobbies = c.getString(c.getColumnIndex("HOBBIES"));

        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            result = result + "\nName : "+iFirstName + " " +iLastName + "\nUserName : "+iUserName + "\nEmail : "+iEmail + "\nPassword : "+ iPassword + "\nYou are : " +iGender + "\nYour Hobbies : "+iHobbies;

        }
        return result;
    }

Is there any Query which i have to use? I dont know how to use SELECT query using WHERE clause and have two parameters suppose (USERNAME=? or EMAIL=?).

Thanks In advance.

The query should be this:

select * from SIGNUP1 where (USERNAME='someusername' or EMAIL='someemail')

and you should replace the strings between ''

public String getSingleEntry(String userNameOrEmail,userEmail)
    {

        Cursor cursor=db.rawQuery("select * from SIGNUP1 where (USERNAME=? or EMAIL=?)", new String[]{userNameOrEmail,userEmail});


    if(cursor.getCount()<1) // UserName Not Exist
    {
        cursor.close();
        return "NOT EXIST";
    }
    cursor.moveToFirst();
    String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
    cursor.close();
    return password;                
}

I'll point to a resource that allows you to experiment and view results of queries so you can learn: http://www.w3schools.com/sql/sql_and_or.asp

Once you understand how to use the OR function you'll have no problem :).

(if you want i can post the actual answer, but i'd rather you learnt instead of copied)

EDIT: I cannot see any reason you won't be able to use OR in SQLite. There is even a doc page for OR optimization. Have you tried to use it?

解决了,我刚刚将查询修改为“从SIGNUP1中选择*,其中USERNAME =“ +” \\'“ + userNameOrEmail +” \\'+ \\“ OR EMAIL =” +“ \\'” + userNameOrEmail +“'”,null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM