在Android应用程序中检查针对SQLite数据库的登录详细信息的方法无效

时间:2017-03-18 16:04:46

标签: java android database sqlite android-sqlite

Android开发相对较新。我正在创建一个Android Studio应用程序,允许用户通过检查针对使用SQLite创建的表提供的现有电子邮件地址和密码来登录系统。当用户提供现有电子邮件和密码时,活动开始意图将布局更改为主屏幕。如果不正确,则会显示登录尝试Toast消息失败。

数据库已成功创建,但无论用户提供的电子邮件地址或密码是否正确,用户都会自动登录。我不确定我在hasObject方法中使用的rawQuery是否正确。

我在下面包含了RegistrationDatabaseHelper.java类,其中hasObject方法是最终方法:

public class RegistrationDatabaseHelper extends SQLiteOpenHelper {

//declaring variable so we can find the name of our database
public static final String DATABASE_NAME = "belfast.db";
public static final String TABLE_NAME = "reg_details_table";
public static final String COL_1 ="ID";
public static final String COL_2 ="EMAIL";
public static final String COL_3 ="USERNAME";
public static final String COL_4 ="PASSWORD";
public static final String COL_5 ="DOB";
public static final String COL_6 ="MOBILE";

//default constructor below
public RegistrationDatabaseHelper(Context context) {
    //when the constructor is called it will create your database
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    //creating the table WITHIN the database below
    db.execSQL(" create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,EMAIL TEXT,USERNAME TEXT,PASSWORD STRING,DOB TEXT,MOBILE LONG) ");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    onCreate(db);
}

//creating a method below to insert data
public boolean insertData(String emailAddress, String userName, String password, String dob, Long mobileNumber){
    //we will use this SQLDatabase instance to insert our data
    SQLiteDatabase db = this.getWritableDatabase();
    //now we need to create an instance of the class ContentValue
    ContentValues contentValues = new ContentValues();
    //we will now take this contentValues instance and insert it into the data columns
    //the first arg is the column name itself, the second is the data itself.
    contentValues.put(COL_2, emailAddress);
    contentValues.put(COL_3, userName);
    contentValues.put(COL_4, password);
    contentValues.put(COL_5, dob);
    contentValues.put(COL_6, mobileNumber);
    //we then insert our data using the db instance created above
    //this takes three arguments. The first is the table name, The second is null and the third
    //is the contentValues which we have created.
    long result = db.insert(TABLE_NAME,null,contentValues);
    if (result==-1){
        return false;
    } else {
        return true;
    }
}

//creating a method that will show all data that has been entered into the database, using
//elements from the Cursor class. The Cursor interface allows read-write access to the result
public Cursor getAllData(){
    //creating an instance of the database class firstly to allow us to get all the data
    SQLiteDatabase db = this.getWritableDatabase();
    //now we will create an instance of the Cursor class called result and use the
    //rawQuery method. Basically creates a SQL query.
    Cursor result = db.rawQuery("select * from "+ TABLE_NAME,null);
    //we will now return the instance of this cursor, which is "result"
    return result;

}
//creating a method that will update all data in our database, using 4 args all of which are
//string, they are id, name, surname and marks
public boolean updateData(String emailAddress, String userName, String password, String dob, Long mobileNumber){
    //creating an instance of the database class firstly to allow us to get all the data
    SQLiteDatabase db = this.getWritableDatabase();
    //now we need to create an instance of the class ContentValue
    ContentValues contentValues = new ContentValues();
    //we will now take this contentValues instance and insert it into the data columns
    //the first arg is the column name itself, the second is the data itself.
    contentValues.put(COL_2, emailAddress);
    contentValues.put(COL_3, userName);
    contentValues.put(COL_4, password);
    contentValues.put(COL_5, dob);
    contentValues.put(COL_6, mobileNumber);
    //the below update method will update any args you pass through here
    //the first argument is the table name itself, the second is the contentValues, the third
    //is the condition you want to impose, such as "ID = ?" where the ? is the ID provided. The
    //fourth arg is the String[] array
    db.update(TABLE_NAME, contentValues, "EMAIL = ?", new String[] {emailAddress});
    //we will return true to see if the data is really updated or not
    return true;
}

public Integer deleteData(String emailAddress){
    //creating an instance of the database class firstly to allow us to get all the data
    SQLiteDatabase db = this.getWritableDatabase();
    //calling the delete function on our db instance. It takes 3 args, 1st is the name of the
    //table, the second is the ID number represented by "ID = ?" and the third arg is the String
    //Array[] of the argument type id. The return below returns the integer of the ID
    return db.delete(TABLE_NAME, "EMAIL = ?", new String[] {emailAddress});

}

public boolean hasObject(String emai){
    SQLiteDatabase db = this.getWritableDatabase();
    String selectString = "SELECT * FROM " + TABLE_NAME + " WHERE " + COL_2 + "= ?";

    Cursor cursor = db.rawQuery(selectString,null);
    boolean exist;
    if(cursor.getCount()>0){
        exist=true;
    } else {
        exist=false;
    }
    db.close();
    cursor.close();

    return exist;
}
}

下面还列出了SignInActivity.java类,signIn方法确定用户是否成功登录:

public class SignInActivity extends Activity {

//creating an instance of the RegistrationDatabaseHelper class
RegistrationDatabaseHelper myDb;

EditText userName;
EditText password;
Button emailSignIn;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_in_screen);

    myDb = new RegistrationDatabaseHelper(this);

    userName = (EditText) findViewById(R.id.etUserName);
    password = (EditText) findViewById(R.id.etPass);

    emailSignIn = (Button) findViewById(R.id.btnSignIn);

    signIn();


}

public void signIn(){
    emailSignIn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean recordExists = myDb.hasObject(userName.getText().toString());
            if(recordExists=true){
                Intent intentSignIn = new Intent(getApplicationContext(), HomePageActivity.class);
                Toast.makeText(getApplicationContext(), "Login successful, redirecting to Home Page.", Toast.LENGTH_LONG).show();
                startActivity(intentSignIn);
            } else {
                Toast.makeText(getApplicationContext(), "Invalid credentials, please try again.", Toast.LENGTH_LONG).show();
            }
        }
    });
}    
}

有谁知道用户登录的原因,无论输入如何?正如我所说,我对Android开发相对较新,并且不确定我哪里出错了,所以任何建议都会非常感激。非常感谢提前!

1 个答案:

答案 0 :(得分:0)

I am not sure if the rawQuery I am using in the hasObject method is correct.不,这是

Cursor cursor = db.rawQuery(selectString,null);

您没有传递要在?子句中使用的参数(WHERE)。
相反,您传递 null

正确实施:

Cursor cursor = db.rawQuery(selectString, new String[]{"Your search string here"});

不要担心单引号(SQL字符串分隔符):它们将根据需要自动添加。