即使密码输入正确,密码也不正确

时间:2017-10-19 13:45:27

标签: android sqlite

我正在忙着创建一个投票系统,一个人只能在登录后投票。我遇到的问题是,当我尝试登录时,即使使用正确的密码,我的else if语句仍会触发不正确的密码行代码,我不知道它为什么要这样做。它以前工作,但在我的数据库中添加一个新表后,它就停止了工作。

这是我的数据库代码:

public class LoginDataBaseAdapter {

static final String DATABASE_NAME = "votersystem.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 4;

static final String CREATE_ADDRESS_TABLE = "create table "+"ADDRESS"+
        "( "+"ID integer primary key autoincrement,"+"PROVINCE text," +"MUNICIPALITY text,"+"STREET text,"+"CITY text,"+"FLAT text,"+"TRIBE text,"+"SUBURB text,"+"TOWN text,"+"POST text) ";

static final String CREATE_LOGIN_TABLE = "create table "+"LOGIN"+
        "( " +"ID integer primary key autoincrement,"+"UNAME text," +"EMAIL text,"+"IDNUMBER text,"+"UPHONE text,"+"DIS text," +"PASSWORD  text,"+"REPASSWORD text,"+ "SECURITYHINT text) ";

static final String CREATE_ADMIN_TABLE = "create table "+"ADMIN"+
        "( " +"ID integer primary key autoincrement,"+ "FIRSTNAME  text,"+"LASTNAME text,"+"PARTY text,"+ "SYMBOL text) ";

static final String CREATE_REPORT_TABLE = "create table "+"REPORT"+"(" +"ID integer primary key autoincrement,"+ "FULLNAME text,"+"PARTY text"+")";
public  SQLiteDatabase db;
private final Context context;
private DataBaseHelper dbHelper;
private Cursor mCursor;

public  LoginDataBaseAdapter(Context _context) 
{
    context = _context;
    dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);

}
public  LoginDataBaseAdapter open() throws SQLException 
{
    db = dbHelper.getWritableDatabase();
    return this;
}
public void close() 
{
    db.close();
}

public  SQLiteDatabase getDatabaseInstance()
{
    return db;
}

public void insertEntry(String Prov, String Mun, String str, String cty, String flt, String trb, String sburb, String twn, String pst)
{
    ContentValues newValues = new ContentValues();
    newValues.put("PROVINCE",Prov);
    newValues.put("MUNICIPALITY",Mun);
    newValues.put("STREET",str);
    newValues.put("CITY",cty);
    newValues.put("FLAT",flt);
    newValues.put("TRIBE",trb);
    newValues.put("SUBURB",sburb);
    newValues.put("TOWN",twn);
    newValues.put("POST",pst);

    db.insert("ADDRESS", null, newValues);
}

public void insertEntry(String User, String email,String idnumber,String Phone,String Dis, String password,String repassword,String securityhint)
{
    ContentValues newValues = new ContentValues();
    newValues.put("UNAME",User);
    newValues.put("EMAIL",email);
    newValues.put("IDNUMBER",idnumber);
    newValues.put("UPHONE",Phone);
    newValues.put("DIS",Dis);
    newValues.put("PASSWORD", password);
    newValues.put("REPASSWORD",repassword);
    newValues.put("SECURITYHINT",securityhint);

    db.insert("LOGIN", null, newValues);
}

public int deleteEntry(String User)
{
    String where="UNAME=?";
    int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{User}) ;
    return numberOFEntriesDeleted;
}   

public String getSinlgeEntry(String User)
{
    Cursor cursor=db.query("LOGIN", null, " UNAME=?", new String[]{User}, null, null, null);
    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;
}

public String getAllTags(String a) {


    Cursor c = db.rawQuery("SELECT * FROM " + "LOGIN" + " where SECURITYHINT = '" +a + "'" , null);
    String str = null;
    if (c.moveToFirst()) {
        do {
            str = c.getString(c.getColumnIndex("PASSWORD"));
        } while (c.moveToNext());
    }
    return str;
}

这是我的Java编码,其中我有if语句允许用户转到下一页,或者提供错误的密码错误消息:

public class LoginAndRegistrion extends Activity {

LoginDataBaseAdapter loginDataBaseAdapter;
Button login;
Button registerr;
EditText Username,enterpassword;
TextView forgetpass;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_registration);

    login=(Button)findViewById(R.id.login_btn);
    registerr=(Button)findViewById(R.id.register_btn);
    Username=(EditText)findViewById(R.id.nam_ent_edt);
    enterpassword=(EditText)findViewById(R.id.password_edt);
    forgetpass=(TextView)findViewById(R.id.textView2);

    loginDataBaseAdapter = new LoginDataBaseAdapter(getApplicationContext());
    loginDataBaseAdapter.open();

    registerr.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i=new Intent(LoginAndRegistrion.this,Registration.class);
            startActivity(i);
        }
    });

    login.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String Uusername=Username.getText().toString();
            String Password=enterpassword.getText().toString();


            String storedPassword=loginDataBaseAdapter.getSinlgeEntry(Uusername);

            if(Password.equals(storedPassword))
            {
                Intent ii=new Intent(LoginAndRegistrion.this,OtpSubmitScreen.class);
                startActivity(ii);
            }
            else
                if(Password.equals("")){
                    Toast.makeText(LoginAndRegistrion.this, "Please Enter Your Password", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(LoginAndRegistrion.this, "Password Incorrect", Toast.LENGTH_LONG).show();
                }
        }
    });

我最后一次错过了缩进。但这次我不知道为什么它会触发我错误的密码声明,即使我输入了正确的用户名和密码。

我错过了什么?

1 个答案:

答案 0 :(得分:-1)

请尝试使用 “创建表格”+“报告”+“”( 代替  “创建表格”+“报告”+“”(“

你应该关心间距。它应该是支架后的空格。