0

what is wrong with this code

when i run it the program don,t response

    public Cursor checkauth(String username,String password)
{
    Ecommerce=getReadableDatabase();
//  Cursor curser =Ecommerce.rawQuery("select * from customer where username = ? and password =?", new String []{username,password});
    //String [] details={"id","custname","gender"};
    Cursor c = Ecommerce.rawQuery("select id  from customer where username = ? AND password = ?" , new String[] {username,password });
    //Cursor cursor = Ecommerce.query(true, "customer",details,"username = ? AND password = ?", new String[] { username, password },null, null, null, null, null);
    while(c != null)
    {
    c.moveToFirst();
    }
    return c;
}

in activity

public void onClick(View arg0) {
            // TODO Auto-generated method stub

            user_name=username.getText().toString();
            passwords=password.getText().toString();
            Cursor c=data.checkauth(user_name, passwords);
            while(!c.isAfterLast())
            {
              username.setText(String.valueOf(c.getInt(0)));
              c.moveToNext();
            }
            }

![enter image description here][1]

3 Answers 3

1

You're blocking your UI thread.

This loop never completes:

while(c != null)
    {
    c.moveToFirst();
    }

The while condition is always true. The loop doesn't seem to be doing anything useful, you can probably just leave a plain c.moveToFirst() there.

Sign up to request clarification or add additional context in comments.

Comments

1

Try with this..

String selectQuery = "Write select query" ;
SQLiteDatabase db = this.getWritableDatabase();

Cursor cursor = db.rawQuery(selectQuery, null);
boolean result = false;

if (cursor.moveToFirst())
{
        do
        {
            result = true;
        } while (cursor.moveToNext());
    }
    else
    {
        result = false;
    }

Comments

0

Even though this question is answered already, I'd like to suggest a simple improvement. What you're doing is kind of useless. Your goal is to know whether or not a user exists and what his or her id is.

public int checkAuth(String username, String password) {
    String sql = "select id from customer where username = ? AND password = ?";
    Ecommerce = getReadableDatabase(); //field names never start with uppercase
    Cursor c;
    try {
        c = Ecommerce.rawQuery(sql, new String[] { username, password });
        if (c.moveToFirst()) //if the cursor has any records, this returns true, else false
            return c.getInt(0); //so if a user exists with given params, we can return an id
        else
            return -1; //if not, we return -1, which means: user/pass-combination not found
    }
    finally { //but after the whole method, we need to do some things: 
        if (c != null && !c.isClosed())
            c.close(); //close the cursor
        closeDatabase(); //and close the database
    }
}

This way you keep your UI code separated from your database code. A cursor shouldn't appear in UI-code. Which above modifications, the UI code looks a lot cleaner

int i = data.checkAuth(username, password);
if (i == -1) {
    //alert of some kind
}
else
    username.setText(String.valueOf(i));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.