androiddatabaseandroid-studioandroid-edittextandroid-applicationinfo

In android studio, I want to compare the values entered in a Edit text fields to the DB


i am having a sign up page and getting username and password through edit text fields and storing them in a database without ID,now i need to compare these username and password stored in the database to that of the edit texts in another activity named "login" and want to check if they match. This is my Signup.java code:

    package com.example.admin.studentdb;


    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.Intent;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;

    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;




    public class Signup extends AppCompatActivity implements OnClickListener{
  EditText username,password,cpassword;
   Button signup;
   SQLiteDatabase sdb;

   @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.signup);
    username = ((EditText) findViewById(R.id.username));
    password = ((EditText) findViewById(R.id.password));
    cpassword = ((EditText) findViewById(R.id.cpassword));
    signup = ((Button) findViewById(R.id.btnsing));


    signup.setOnClickListener(this);

    sdb = openOrCreateDatabase("SignUP", Context.MODE_PRIVATE, null);
    sdb.execSQL("CREATE TABLE IF NOT EXISTS sign ( username VARCHAR ,password VARCHAR);");
    }

    public void onClick(View view) {

    String chkpass = password.getText().toString();

    String cmpass = cpassword.getText().toString();

    if (view == signup) {
        if (username.getText().toString().trim().length() == 0) {
            showMessage("Error", "Please enter the username");
            return;
        } else if (password.getText().toString().trim().length() == 0) {
            showMessage("Error", "Please enter the password");
            return;
        } else if (cpassword.getText().toString().trim().length() == 0) {
            showMessage("Error", "Confirm password");
            return;
        } else if (!chkpass.equals(cmpass)) {
            showMessage("Error", "passwords dont match");
            return;



            } else if (getCount()==0){
            sdb.execSQL("INSERT INTO sign VALUES('" + username.getText() + "','" + password.getText() + "');");
            Toast a = Toast.makeText(getApplicationContext(),"Registered Sucessfully",Toast.LENGTH_LONG);
            a.show();
            Intent abc = new Intent(Signup.this,Login.class);
            startActivity(abc);
            clearText();

            }
        else{
            showMessage("Error", "Username already Exists");
        }

        }



       }



      public int getCount() {
      Cursor c = null;
      try {

        c = sdb.rawQuery("SELECT COUNT(*) FROM sign WHERE username='" + username.getText() + "'", null);
        if (c.moveToFirst()) {
            return c.getInt(0);
        }
        return 0;
       }
      finally {

    }
    }





   public void showMessage(String title,String message)
   {
    AlertDialog.Builder builder=new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.show();
   }
   public void clearText()
   {
    username.setText("");
    password.setText("");
    cpassword.setText("");
    username.requestFocus();
   }
   }

Solution

  • Haven't you forgotten to add .toString() after username.getText() as it returns Editable not String?

    c = sdb.rawQuery("SELECT COUNT(*) FROM sign WHERE username='" + username.getText() + "'", null);