Ok so I am currently trying to make a Simple Register and Log in page using SQL Lite. I currently have no problems with creating or inserting values into the database. What I want to do is using an if-else statement for the Login, whereby it will deny the user if there is no record (or mistyped) found in the database and will only allow when they enter the username and password correctly. Attached is my code. Please advise. Thanks
package mdad.project;
import com.example.manandhowproject.R;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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 {
SQLiteDatabase db;
Button btnLogin, btnSignUp;
EditText etUsername, etPassword;
SQLiteOpenHelper dbhelper;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
etUsername = (EditText)findViewById(R.id.etUsername);
etPassword = (EditText)findViewById(R.id.etPassword);
Button button = (Button)findViewById(R.id.btnLogin);
db = dbhelper.getReadableDatabase();
button.setOnClickListener(new OnClickListener(){
public void onClick (View V){
Toast.makeText(getApplicationContext(), "Log In Success ! ", Toast.LENGTH_LONG).show();
Intent msg1 = new Intent(Login.this, Userreg.class);
startActivity(msg1);
}
});
Button btnSignUp = (Button) findViewById (R.id.btnSignUp);
btnSignUp.setOnClickListener(new OnClickListener (){
public void onClick (View V) {
Toast.makeText(getApplicationContext(), "Sign Up Success ! ", Toast.LENGTH_LONG).show();
String username= etUsername.getText().toString();
String password= etPassword.getText().toString();
String sql = "insert into Registration (Username ,Password) values( '"+username+"','"+password+"')";
String result = updateTable(sql);
etUsername.setText("");
etPassword.setText("");
}});
String sql="create table if not exists Registration (recld integer PRIMARY KEY autoincrement, Username text, Password text)";
String result = createDatabase(sql, "Reg.db");
}
String createDatabase(String sql, String dbName) {
try{
System.out.println(sql);
db = SQLiteDatabase.openOrCreateDatabase("sdcard/" + dbName,null);
db.beginTransaction();
db.execSQL(sql);
db.setTransactionSuccessful();
db.endTransaction();
}
catch (Exception e){
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
System.out.println(e.toString());
return ("error open DB");
}
return "";
}
String updateTable(String sql)
{
try{
System.out.println(sql);
db.beginTransaction();
db.execSQL(sql);
db.setTransactionSuccessful();
db.endTransaction();
}catch (Exception e) { System.out.println(e.toString());
return ("Error updating DB");
}
return ("DB updated");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}