登录(无法从数据库中检索数据)

时间:2016-09-25 04:18:46

标签: android sqlite login

我是Android编程的初学者,我正在尝试创建登录功能。该系统将由管理员和用户两个用户访问。管理员只需输入“admin”作为用户名和密码即可访问系统。但是,用户需要在登录系统之前注册其帐户。

我认为注册没有问题。但是,当我尝试登录帐户时,它表示用户名和密码不匹配。我认为它无法从数据库中检索数据。请帮我查一下注册和登录功能代码。谢谢

MainActivity.java

public void onclickLogin(View view) {
Toast.makeText(getBaseContext(), "Please wait....", Toast.LENGTH_LONG).show();
txtusername = Username.getText().toString();
txtpassword = Password.getText().toString();
DatabaseOperations dop = new DatabaseOperations(this, null, null, 1);
Cursor CR = dop.getInformation(dop);
CR.moveToFirst();
boolean login_status = false;
String NAME = "";
if (txtusername.equals("Admin") && (txtpassword).equals("Admin")) {
    Intent in = new Intent(MainActivity.this, AdminMenu.class);
    startActivity(in);
} else {
    do {
        if (txtusername.equals(CR.getColumnIndex(TableData.TableInfo.USERNAME)) && (txtpassword.equals(CR.getColumnIndex(TableData.TableInfo.USERPASS)))) {
            login_status = true;
            NAME = CR.getString(CR.getColumnIndex(TableData.TableInfo.USERNAME));
        }
    } while (CR.moveToNext());
    if (login_status) {
        Toast.makeText(getBaseContext(), "Login Success....\n Welcome " + NAME, Toast.LENGTH_LONG).show();
        Intent i=new Intent(MainActivity.this,UserMenu.class);
        Bundle bundle=new Bundle();
        bundle.putString("username",NAME);
        i.putExtras(bundle);
        startActivity(i);
        finish();
    } else {
        Toast.makeText(getBaseContext(), "Login Failed...", Toast.LENGTH_LONG).show();
    }

DatabaseOperations.java

public Cursor getInformation(DatabaseOperations dob)
{
SQLiteDatabase database=dob.getReadableDatabase();
String[] columns={TableInfo.USERNAME,TableInfo.USERPASS};
Cursor cr=database.query(TableInfo.TABLE_NAME,columns,null,null,null,null,null);
return cr;
}

public void putInformation(Account account) {
ContentValues cv = new ContentValues();
cv.put(TableInfo.USERNAME, account.get_username());
cv.put(TableInfo.USERPASS, account.get_password());
cv.put(TableInfo.USERPHONE, account.get_Phone());
cv.put(TableInfo.USEREMAIL, account.get_Email());
cv.put(TableInfo.USERCARPLATE1, account.get_CarPlate1());
cv.put(TableInfo.USERFULLNAME, account.get_FullName());
cv.put(TableInfo.USERIC, account.get_IdentityCard());
SQLiteDatabase SQ = this.getWritableDatabase();
SQ.insert(TableInfo.TABLE_NAME, null, cv);
Log.d("Database operations", "One row inserted");
SQ.close();
}

RegisterPage.java

 public void onclickRegistering(View view)
    {
            registerusername=RegisterUsername.getText().toString();
            registerpassword=RegisterPassword.getText().toString();
            conpassword=ConPassword.getText().toString();
            phone=Phone.getText().toString();
            email=Email.getText().toString();
            identitycard=IdentityCard.getText().toString();
            fullname=FullName.getText().toString();
            carplate=CarPlate.getText().toString();
            if(!(registerpassword.equals(conpassword)))
            {
                Toast.makeText(getBaseContext(),"Passwords are not matching",Toast.LENGTH_LONG).show();
                RegisterUsername.setText("");
                RegisterPassword.setText("");
                ConPassword.setText("");
                Phone.setText("");
                Email.setText("");
                IdentityCard.setText("");
                FullName.setText("");
                CarPlate.setText("");
            }
            else
            {
                DatabaseOperations db=new DatabaseOperations(this,null,null,1);
                Account user=new Account(registerusername,registerpassword,Integer.parseInt(phone),email,Integer.parseInt(identitycard),fullname,carplate);
                db.putInformation(user);
                Toast.makeText(getBaseContext(),"Register Successfully...",Toast.LENGTH_LONG).show();
                Intent in=new Intent(register_page.this,MainActivity.class);
                startActivity(in);
                finish();
            }

        }

1 个答案:

答案 0 :(得分:1)

在下面的代码片段中,您试图将从数据库中检索到的用户名和密码与仅用户名进行比较 - 希望这可能是您收到错误的原因。

do {
    if (txtusername.equals(CR.getColumnIndex(TableData.TableInfo.USERNAME)) && (txtpassword.equals(CR.getColumnIndex(TableData.TableInfo.USERPASS)))) {
        login_status = true;
        NAME = CR.getString(CR.getColumnIndex(TableData.TableInfo.USERNAME));
    }
} while (CR.moveToNext());

参考以下链接 Using SQlite to validate Logins in Android