How to fetch all column values based on email?

76 Views Asked by At

I want to fetch my all the column from table tblReg. It has column Registration, Name, Password, Email and Contact out which Email is checked while registration, it is unique column; value exist only once in table of one Email.

I am having login by matching Email with password and passing value of an email through intent in my home activity as KeyName. Now I want to fetch all detail from SQLlite based on my Email, I am receiving from getExtra through Intent.

Here is my code of Home Activity

public class activity_home extends Activity {
    DatabaseHandler dbh = new DatabaseHandler(this);
String regid,emailid,name,contact,data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_home);
        TextView lblEmail = (TextView) findViewById(R.id.textView1);
        TextView lblReg = (TextView) findViewById(R.id.textView1);
        TextView lblName = (TextView) findViewById(R.id.textView1);
        dbh.open();
        try {
            data = getIntent().getExtras().getString("keyName");

            Cursor m1 = dbh.getAllData(data);
            if (m1 != null) {
                m1.moveToFirst();
                name = m1.getString(m1.getColumnIndex("Name"));
                regid = m1.getString(m1.getColumnIndex("Registration"));
                contact = m1.getString(m1.getColumnIndex("Contact"));
                emailid = m1.getString(m1.getColumnIndex("Email"));
                m1.close();
            }

            if (!data.equals("")) {

                lblEmail.setText(Html.fromHtml("Email: <b>" + emailid + "</b>"));
                lblReg.setText(Html.fromHtml("Email: <b>" + regid + "</b>"));
                lblName.setText(Html.fromHtml("Email: <b>" + name + "</b>"));
            } else {
                Toast.makeText(getApplicationContext(), "Data Null Aya hai Bhai", Toast.LENGTH_SHORT).show();
            }
        }
        catch (Exception e)
        {
            String ex=e.toString();
            Toast.makeText(getApplicationContext(), ex, Toast.LENGTH_SHORT).show();
        }
            }

I have used a database handler class where I am using a query, I think there is a problem in query, please have a look in that. I am sharing the cursor code, where am I wrong?

 public Cursor getAllData(String parentKey) {

    String[] fields = new String[] {
            KEY_REGID,
            KEY_NAME,
            KEY_PASSWORD,
            KEY_EMAIL,
            KEY_MOBILENO + " as _id "
    };

    Cursor cursor = mDb.query(tblReg,
            fields,
            KEY_EMAIL + " = ?",
            new String[] {
                    "" + parentKey
            },
            null,
            null,
            KEY_NAME);

    cursor.moveToFirst();
    return cursor;
}
1

There are 1 best solutions below

0
On BEST ANSWER

I have solved my problem of fetching data from tables based on column values and now I want to share the sloppy mistakes.

TextView lblEmail = (TextView) findViewById(R.id.textView1);
            TextView lblReg = (TextView) findViewById(R.id.textView1);
            TextView lblName = (TextView) findViewById(R.id.textView1);

This code has to be written like as stated below

TextView lblEmail = (TextView) findViewById(R.id.textView1);
                TextView lblReg = (TextView) findViewById(R.id.textView2);
                TextView lblName = (TextView) findViewById(R.id.textView3);

The rest is all working great, any viewer want to learn how to fetch all column values based upon value passed by user can refer the above code.