dimanche 1 novembre 2015

sqlite table consider any String as correct username and pwd

** register table consider any String as valid**

login class

there is a register table and there are fields FirstName,LastName and Password i check edittext values to the register table .... but it returns true for any String.....

 package com.example.app;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class login extends Activity{

    EditText e1,e2;
    Button b1;

    DBHandler handler=new DBHandler(this);
    //SQLiteDatabase db=handler.getWritableDatabase();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        e1=(EditText)findViewById(R.id.e_l_fnm);
        e2=(EditText)findViewById(R.id.e_l_lnm);
        b1=(Button)findViewById(R.id.b_login);
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                String fname=e1.getText().toString();
                String pwd=e2.getText().toString();
                if(fname.equals("") || pwd.equals(""))
                {
                    Toast.makeText(getApplicationContext(), "fields are required", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    handler.login(fname, pwd);
                    Toast.makeText(getApplicationContext(), "login successfull", Toast.LENGTH_SHORT).show();
//                  Intent i=new Intent(login.this,home.class);
//                  startActivity(i);
                }
            }
        });
    }

}

login method (DBHandler class)

public boolean login(String fname,String pwd)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        Cursor c=db.rawQuery("SELECT * FROM " + TABLE_REGISTER + " WHERE FirstName=? AND Password=?", new String[]{fname,pwd});
        if(c!=null)
        {
            if(c.getCount()<0)
            {
                return false;
            }
        }
        return true;
    }

Aucun commentaire:

Enregistrer un commentaire