Android SQLite:cursor.getcount()始终返回大于0

时间:2017-11-30 05:44:25

标签: android sqlite login

我正在进行基本的注册和登录活动,我创建了两个活动登录和注册。注册活动正常工作(我猜)但在登录活动中,即使输入错误的电子邮件和密码,它也始终允许用户成功登录。

myDbAdapter

public class myDbAdapter extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION=1;
    private static final String DATABASE_NAME="UserManager";
    private static final String TABLE_NAME="UserData";
    private static final String KEY_ID = "id";
    private static final String EMAIL = "email";
    private static final String PASSWORD = "password";


    public myDbAdapter(Context context) {
        super(context, DATABASE_NAME,null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_NAME + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + EMAIL + " TEXT,"
                + PASSWORD + " TEXT" + ")";
        sqLiteDatabase.execSQL(CREATE_USER_TABLE);
        System.out.println("Table created here");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);

    }
    //Add register data inside table
public void addDetails(String em,String pass)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
values.put(EMAIL,em);
values.put(PASSWORD,em);

db.insert(TABLE_NAME,null,values);
db.close();

}

    public boolean checkUser(String e1, String p1) {

        String[] columns = {
                KEY_ID
        };
        SQLiteDatabase db = this.getReadableDatabase();
        // selection criteria
        String selection = EMAIL + " = ?" + " AND " + PASSWORD + " = ?";

        // selection arguments
        String[] selectionArgs = {e1, p1};

        // query user table with conditions
        /**
         * Here query function is used to fetch records from user table this function works like we use sql query.
         * SQL query equivalent to this query function is
         * SELECT user_id FROM user WHERE user_email = 'jack@androidtutorialshub.com' AND user_password = 'qwerty';
         */
        Cursor cursor = db.query(TABLE_NAME, //Table to query
                columns,                    //columns to return
                selection,                  //columns for the WHERE clause
                selectionArgs,              //The values for the WHERE clause
                null,                       //group the rows
                null,                       //filter by row groups
                null);                      //The sort order

        int cursorCount = cursor.getCount();

        cursor.close();
        db.close();
        if (cursorCount > 0) {
            return true;
        }

        return false;
    }
}

登录

public class Login extends Activity {
    myDbAdapter ob =new myDbAdapter(this);
    String k,k1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        EditText ed= (EditText) findViewById(R.id.Lee);
        EditText ed1= (EditText) findViewById(R.id.Lep);
        Button bn1=(Button)findViewById(R.id.buttonL);
        k=ed.getText().toString();
        k1=ed1.getText().toString();
        bn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(ob.checkUser(k,k1))
                {
                    Toast.makeText(Login.this, "Login Successfully", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(Login.this, "Login Failed", Toast.LENGTH_SHORT).show();

                }
            }
        });



    }
}

注册

public class Register extends Activity{
    myDbAdapter ob =new myDbAdapter(this);
    String s,s1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        EditText edm= (EditText) findViewById(R.id.editE);
        EditText edp= (EditText) findViewById(R.id.editP);
        s=edm.getText().toString();
        s1=edp.getText().toString();
        Button bn=(Button)findViewById(R.id.RegiB);
        bn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ob.addDetails(s,s1);
                Toast.makeText(Register.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

在登录活动中,Toast文本始终为成功登录。我试图找到表格中是否已存在使用 checkuser 功能的电子邮件和密码,返回true 始终是cursor.getcount()>0  有什么帮助吗?

4 个答案:

答案 0 :(得分:0)

你必须尝试这样

登录部分

EditText ed= (EditText) findViewById(R.id.Lee);
        EditText ed1= (EditText) findViewById(R.id.Lep);
        Button bn1=(Button)findViewById(R.id.buttonL);

        bn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             k=ed.getText().toString();
             k1=ed1.getText().toString();
                if(ob.checkUser(k,k1))
                {
                    Toast.makeText(Login.this, "Login Successfully", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(Login.this, "Login Failed", Toast.LENGTH_SHORT).show();

                }
            }
        });

注册部分

        Button bn=(Button)findViewById(R.id.RegiB);
        bn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
           s=edm.getText().toString();
           s1=edp.getText().toString();
                ob.addDetails(s,s1);
                Toast.makeText(Register.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
            }
        });

答案 1 :(得分:0)

使用以下代码更改您的代码。您存储空白和查询空白,这就是您获得columnCount()>0

的原因

<强>登录

public class Login extends Activity {
    myDbAdapter ob =new myDbAdapter(this);
    String k,k1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        EditText ed= (EditText) findViewById(R.id.Lee);
        EditText ed1= (EditText) findViewById(R.id.Lep);
        Button bn1=(Button)findViewById(R.id.buttonL);

        bn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                k=ed.getText().toString();
                k1=ed1.getText().toString();
                if(ob.checkUser(k,k1))
                {
                    Toast.makeText(Login.this, "Login Successfully", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(Login.this, "Login Failed", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

<强>注册

public class Register extends Activity{
    myDbAdapter ob =new myDbAdapter(this);
    String s,s1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        EditText edm= (EditText) findViewById(R.id.editE);
        EditText edp= (EditText) findViewById(R.id.editP);
        Button bn=(Button)findViewById(R.id.RegiB);
        bn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                s=edm.getText().toString();
                s1=edp.getText().toString();
                ob.addDetails(s,s1);
                Toast.makeText(Register.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

答案 2 :(得分:0)

在初始化时从EditText中读取文本onClick,因为它始终为空

<强>注册

     Button bn=(Button)findViewById(R.id.RegiB);
        bn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              s=edm.getText().toString();
             s1=edp.getText().toString();
                ob.addDetails(s,s1);
                Toast.makeText(Register.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
            }
        });

<强>登录

   bn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

         k=ed.getText().toString();
         k1=ed1.getText().toString();
            if(ob.checkUser(k,k1))
            {
                Toast.makeText(Login.this, "Login Successfully", Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(Login.this, "Login Failed", Toast.LENGTH_SHORT).show();

            }
        }
    });

答案 3 :(得分:0)

为了可靠性,请考虑使用cursor.moveToFirst()来查找光标是否为空cursor.getCount()

if (null != cursor && cursor.moveToFirst() && cursor.getCount() > 0 ) {
    // doSomething
} else {
    // doOtherThing()
}