Java 方法检查Android应用程序中SQLite数据库的登录详细信息不工作

Java 方法检查Android应用程序中SQLite数据库的登录详细信息不工作,java,android,database,sqlite,android-sqlite,Java,Android,Database,Sqlite,Android Sqlite,android开发相对较新。我正在创建一个androidstudio应用程序,允许用户通过检查使用SQLite创建的表中提供的现有电子邮件地址和密码来登录系统。当用户提供现有电子邮件和密码时,活动将启动更改主屏幕布局的意图。如果不正确,将显示登录尝试失败消息 数据库已成功创建,但无论用户提供的电子邮件地址或密码是否正确,都会自动登录。我不确定我在hasObject方法中使用的rawQuery是否正确 我在下面包含了RegistrationDatabaseHelper.java类,hasObjec

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

数据库已成功创建,但无论用户提供的电子邮件地址或密码是否正确,都会自动登录。我不确定我在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;
}
}
下面还列出了SignenActivity.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();
            }
        }
    });
}    
}

有人知道用户为什么登录而不考虑输入吗?正如我所说,我对安卓系统的开发还比较陌生,也不确定自己会出什么问题,所以任何建议都将不胜感激。非常感谢

我不确定我在hasObject方法中使用的rawQuery是否正确。
不,它是

Cursor cursor = db.rawQuery(selectString,null);
您没有传递要在
WHERE
子句中使用的参数(
)。
相反,您传递的是null

正确实施:

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

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

Ok,参数email是否应该添加到rawQuery而不是null?抱歉,我仍然不确定rawQuery应该如何正确编写:(请参阅下面的答案。谢谢Rotwang,我已经更新了rawQuery以包含正确的参数(Cursor Cursor=db.rawQuery(selectString,new String[]{email});),现在它神奇地工作了。别忘了接受我的答案(我想你得等几天才能打勾),因此,要从未回答问题队列中删除您的帖子。但是它仍然会登录任何用户名或电子邮件,无论它是否在SQLite数据库中,我想我的SignenActivity Classis中的signIn方法有问题,我还看不到复选标记,但一旦看到,我会确保这样做!
我想我对我的SignenActivity类中的e signIn方法
您可能有同样的问题。这次您需要传递两个参数,如在
db.rawQuery(selectString,new String[]{userID,userPWD});)