1

When I search for data in my database, my app forced to stop. It is only happening when there are more than 2 rows, otherwise it's fine. I can insert data well. How can I solve this?

//click event
public void OnButtonClick(View v) {
    dbhelper.open();

    EditText a = (EditText)findViewById(R.id.user_tf);
    String User_Name = a.getText().toString();
    EditText b = (EditText)findViewById(R.id.password_tf);
    String Password = b.getText().toString();
    sqlitedatabase = dbhelper.getReadableDatabase();
    String password = dbhelper.searchpass(User_Name);

    if(Password.equals(password)) {
        Toast.makeText(getBaseContext(), "User and pass correct",  Toast.LENGTH_LONG).show();
        dbhelper.close();
        Intent intent = new Intent(Login_activity.this, Homepage.class);
        startActivity(intent);
    } else {
        Toast.makeText(getBaseContext(),"User or pass incorrect", Toast.LENGTH_LONG).show();
        dbhelper.close();
        Intent intent = new Intent(Login_activity.this,  Login_activity.class);
        startActivity(intent);
    }

// Searchpass in DBHelper class
public String searchpass(String user_name) {
    db.isOpen();
    db = this.getReadableDatabase();
    String query = "SELECT " + UserConstruct.newUserinfo.UserName + ", " + UserConstruct.newUserinfo.Password + " FROM " + UserConstruct.newUserinfo.TableName + " ";
    // int a = query.length();
    Cursor cursor = db.rawQuery(query, null);
    String a, b;
    b = "not found";
    do {
        if (cursor.moveToFirst()) {
            a = cursor.getString(0);
            if (a.equals(user_name)) {
                b = cursor.getString(1);
            }
        }
    } while (cursor.moveToNext());

    return b;
}
Bryan
  • 14,756
  • 10
  • 70
  • 125
Aadhi
  • 15
  • 8
  • 2
    Please use logcat to determine the exception which causes the crash and the line of code where the exception occurred. – Code-Apprentice Oct 05 '16 at 11:17
  • Post your Logcat here to look into it – Shrenik Shah Oct 05 '16 at 11:21
  • debug searchpass() and see what is the query. There should not be any non required space – Shrenik Shah Oct 05 '16 at 11:23
  • writeStringToFile error: /sys/kernel/debug/tracing/tracing_on java.io.FileNotFoundException: /sys/kernel/debug/tracing/tracing_on: open failed: ENOENT (No such file or directory) writeStringToFile error: /sys/kernel/debug/binder/transaction_log_enable java.io.FileNotFoundException: /sys/kernel/debug/binder/transaction_log_enable: open failed: ENOENT (No such file or directory) – Aadhi Oct 05 '16 at 11:28
  • These error is showing in android monitor.. – Aadhi Oct 05 '16 at 11:29
  • @ShrenikShah query is working when the db have data not more than 2 row... – Aadhi Oct 05 '16 at 11:31
  • Using a where clause in your SQL would be so much faster than iterating over your entire data set. – Gary Bak Oct 05 '16 at 11:44

1 Answers1

-1

In this your are using both cursor.moveToNext and cursor.movetoFirst both. I don't think you should do both. use one that is cursor.moveToNext

    while (cursor.moveToNext()) {
        a = cursor.getString(0);
        if (a.equals(user_name)) {
            b = cursor.getString(1);
        }
     }
Anantha Raju C
  • 1,780
  • 12
  • 25
  • 35
sunil
  • 70
  • 8