Я пытаюсь войти в систему с SQLite после успешной регистрации. Теперь, когда я пытаюсь войти, я не могу войти в систему успешно. Я думаю, что есть проблема в RawQuery, которую я написал для входа в систему. Вот код активности входа в систему:
public class login extends AppCompatActivity {
SQLiteDatabase db;
SQLiteOpenHelper sqLiteOpenHelper;
private EditText email;
private EditText password;
private Button btnLogin;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initlizers();
sqLiteOpenHelper=new DatabaseHelper(this);
db=sqLiteOpenHelper.getReadableDatabase();
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String Eemail=email.getText().toString();
String pass=password.getText().toString();
cursor=db.rawQuery("SELECT * FROM "+ DatabaseHelper.TABLE_NAME+" WHERE "+ DatabaseHelper.col_5 + "=? AND " +
"" + DatabaseHelper.col_4 + "=?" ,new String[]{Eemail,pass});
// cursor=db.rawQuery("select * from "+TABLE_NAME+" where "+ "Email=? and Password=?",new String[]{Eemail,pass});
if(cursor!=null)
{
if(cursor.getCount()>0)
{
cursor.moveToNext();
Toast.makeText(login.this, "Login Successfully", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(login.this, "Error", Toast.LENGTH_SHORT).show();
Toast.makeText(login.this, "Email"+Eemail, Toast.LENGTH_SHORT).show();
Toast.makeText(login.this, "Password"+pass, Toast.LENGTH_SHORT).show();
Toast.makeText(login.this, "Curser"+cursor, Toast.LENGTH_SHORT).show();
}
}
}
});
}
private void initlizers() {
email=(EditText) findViewById(R.id.login_email);
password=(EditText) findViewById(R.id.login_password);
btnLogin=(Button) findViewById(R.id.login_button);
}
}
Вот код DataBaseHelper
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME="register.db";
public static final String TABLE_NAME="register";
public static final String col_1="ID";
public static final String col_2="FirstName";
public static final String col_3="LastName";
public static final String col_4="Password";
public static final String col_5="Email";
public static final String col_6="Phone";
public DatabaseHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+ TABLE_NAME+"(ID INTEGER PRIMARY KEY AUTOINCREMENT, FirstName
TEXT, LastName TEXT, Password TEXT, Email TEXT, Phone TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS '" + TABLE_NAME + "'");
onCreate(db);
}
}
Вот код MainActivity, который включает в себя код регистрации.
public class MainActivity extends AppCompatActivity {
SQLiteOpenHelper sqLiteOpenHelper;
SQLiteDatabase db;
private EditText firstName,lastName,email,phoneNumber,password;
private Button register_btn;
private Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initilizers();
register_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db=sqLiteOpenHelper.getWritableDatabase();
String fName=firstName.getText().toString();
String lName=lastName.getText().toString();
String rEmail=email.getText().toString();
String phNumber=phoneNumber.getText().toString();
String pass=password.getText().toString();
insertData(fName,lName,pass,rEmail,phNumber);
Toast.makeText(MainActivity.this, "Register Successfully", Toast.LENGTH_SHORT).show();
}
});
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,login.class);
startActivity(intent);
}
});
}
private void initilizers() {
firstName=(EditText) findViewById(R.id.register_first_name);
lastName=(EditText) findViewById(R.id.register_last_name);
email=(EditText) findViewById(R.id.register_email);
password=(EditText) findViewById(R.id.register_password);
phoneNumber=(EditText) findViewById(R.id.register_phone_number);
register_btn=(Button) findViewById(R.id.register_button);
sqLiteOpenHelper=new DatabaseHelper(this);
loginButton=(Button) findViewById(R.id.register_login_button);
}
public void insertData(String fName,String lName, String pass,String Eemail,String phNumber)
{
ContentValues contentValues=new ContentValues();
contentValues.put(DatabaseHelper.col_1,fName);
contentValues.put(DatabaseHelper.col_2,lName);
contentValues.put(DatabaseHelper.col_1,pass);
contentValues.put(DatabaseHelper.col_1,Eemail);
contentValues.put(DatabaseHelper.col_1,phNumber);
long id=db.insert(DatabaseHelper.TABLE_NAME,null,contentValues);
}
}