簡體   English   中英

我該如何解決我的android登錄問題?

[英]How can I solve my android login problems?

(我的母語不是英語。也許這個問題有一些錯誤的語法。)

當我嘗試登錄我的應用程序時,即使我輸入了錯誤的密碼,該應用程序也始終返回SUCCESS。

首先,我創建了數據庫表(PERSON)。 其次,我創建了檢查PW的方法(chkpPw)。 第三,我使用這種方法有兩種方式。

這就是我想要的結果。 1.空ID或空PW =>請輸入您的ID或PW。 2.錯誤的ID,錯誤的PW或正確的PW =>您的ID不存在。 3.正確的ID,錯誤的PW =>請檢查您的PW。 4.正確的ID,正確的PW =>成功

但是現在我的應用返回了這些。 1.空ID或空PW =>請輸入您的ID或PW。 2.錯誤的ID,錯誤的PW或正確的PW =>您的ID不存在。 3.正確的ID,錯誤的PW =>成功4.正確的ID,正確的PW =>成功

我的SQL查詢找到了數據庫中存在的ID,甚至不考慮pw是否存在。...T_T ....

這是PERSON表的數據:

personID | pId | pPw
1 | apple111 | 11111111
2 | banana222 | 22222222
3 | peache333 | 33333333

DBHelper.java

public class DBHelper extends SQLiteOpenHelper {

private static final String KEY_NAME = "pId";

private static final int DATABASE_VERSION = 1;

public DBHelper(Context context) {
    super(context, "tryangleDB", null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String personSql = "CREATE TABLE PERSON (" +
            "personID INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "pId TEXT NOT NULL, " +
            "pPw TEXT NOT NULL, " +
            "pName TEXT NOT NULL, " +
            "pBirth TEXT NOT NULL, " +
            "pEmail TEXT NOT NULL, " +
            "pPhone TEXT NOT NULL, " +
            "pProfile BLOB, " +
             "cDate TEXT, " +
            "dDate TEXT)";

    db.execSQL(personSql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (newVersion == DATABASE_VERSION) {
        db.execSQL("DROP TABLE IF EXISTS PERSON");
        onCreate(db);
    }
}


// check same id exists when user registers
public boolean chkpId (String pId){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("Select * from person where pId=?", new String[]{pId});
    if (cursor.getCount()==0) {
        return true;
    }
    else {
        return false;
    }
}

// when user tries to login, check pw with id
// ★Please help this part.
public boolean chkpPw (String pPw, String pId){
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.rawQuery("Select ? from person where pId= ?", new String[]{pPw, pId});
    if (cursor.getCount()>0) {
        return true;
    }
    else {
        return false;
    }
}


//    // this is what I tried.
//    public boolean chkpPw(String pPw, String pId) {
//        SQLiteDatabase db = this.getReadableDatabase();
//        String[] columns = {pPw, };
//        String[] parms = {pId };
//
//        Cursor cursor = db.query(" PERSON ", columns, "pId = ?", parms, null, null, null);
//        try {
//            if (cursor.getCount() > 0) {
//                return true;
//                // 트루라는 자체가 일치하는 id를 찾았다는 것

//            } else {
//                return false;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return false;
//        } finally {
//
//        }
//    }
}

LoginActivity.java

// skip upper part
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pPw = etPw.getText().toString();
                pId = etId.getText().toString();
//                boolean chkpId = db.chkpId(pId);
                boolean chkpPw = db.chkpPw(pPw, pId);

                // 1
                if (pId.equals("") || pPw.equals("")) {
                    Toast.makeText(getApplicationContext(),"Please enter your ID or PW.",Toast.LENGTH_SHORT).show();
                }
                else{
                    try{//★ I think this part has some problems.



if (chkpPw) {
                            Toast.makeText(getApplicationContext(),"success",Toast.LENGTH_SHORT).show();
                        }
                        else if (!chkpPw) {
                            Toast.makeText(getApplicationContext(),"failed",Toast.LENGTH_SHORT).show();
                        }





// ★ these are what I tried
//                        if(chkpPw==false) {
//                            save();
//                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
//                            startActivity(intent);
//                        }

//                        if(chkpId==false&&(chkpPw==true||chkpPw==false)) {
//                            Toast.makeText(getApplicationContext(),"not exist ID",Toast.LENGTH_SHORT).show();
//                        }
//
//                        if(chkpId==true&&chkpPw==false) {
//                            Toast.makeText(getApplicationContext(),"wrong PW",Toast.LENGTH_SHORT).show();
//                        }
//                        if(chkpPw==true) {
//                            Toast.makeText(getApplicationContext(),"not matched PW to this ID",Toast.LENGTH_SHORT).show();
//                        }

//                        if(chkpId==false && chkpPw==false) {
//                            save();
//                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
//                            startActivity(intent);
//                        }

//                        else if (chkpId==false && chkpPw==true) {
//                            Toast.makeText(getApplicationContext(),"not matched to PW",Toast.LENGTH_SHORT).show();
//                        }

//                        else if (chkpId==true && chkpPw==true) {
//                            Toast.makeText(getApplicationContext(),"Please register.",Toast.LENGTH_SHORT).show();
//                        }

                        } catch (Exception e) {
                            e.printStackTrace();
                            Toast.makeText(LoginActivity.this, "something wrong", Toast.LENGTH_SHORT).show();
                        } finally {
    //                        save();
                        }

                    }
                }
            });

您正在檢查ID和密碼是否分開存在。 一種更合適的方法是先檢查id是否存在,然后檢查id和密碼是否同時存在。 您可能需要按照以下幾行做一些事情。

public boolean chkpPw (String pPw, String pId){
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.rawQuery("Select * from person where pPw=? and pId=?", new String[]{pPw, pId});
    if (cursor.getCount()>0) {
        return true;
    }
    else {
        return false;
    }
}

將您的chkpPw方法中的查詢更改為

db.rawQuery("SELECT 1 FROM `person` WHERE `pPw`=? AND `pId`=?", new String[]{pPw, pId});

我認為您的db chkpPw方法是錯誤的。 您正在執行的操作是:存在此用戶時選擇密碼。 您可能會注意到,如果僅存在id,則此查詢將至少返回一行,因此您返回true,並且方法表示您是對的。

好的,現在讓我們來看看答案。

要檢查用戶名和密碼是否正確,可以根據您的代碼嘗試執行以下操作:

從your_table中選擇*,其中id =? 和密碼=?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM