dimanche 28 février 2016

Fetch data from sqlite android

I have following table

private static final String DB_TABLE = "create table user (id integer primary key autoincrement, " 
                                            + "username text not null, password text not null, tel text not null, info text not null, job text);";

How can I fetch data using cursor and convert it to string? because I am getting null Should I have set gets? I have to fetch job and info by username and password?

Cursor theUser = dbHelper.fetchUser(thisUsername, thisPassword);
   if (theUser != null) {

How here also fetch a info and job in String?

            startManagingCursor(theUser);
            if (theUser.getCount() > 0) {
                //saveLoggedInUId(theUser.getLong(theUser.getColumnIndex(DatabaseAdapter.COL_ID)), thisUsername, thePassword.getText().toString());
                stopManagingCursor(theUser);
                theUser.close();
               // String text = dbHelper.getYourData();
                Intent i = new Intent(v.getContext(), InfoActivity.class);
                startActivity(i);
            }

            //Returns appropriate message if no match is made
            else {
                Toast.makeText(getApplicationContext(),
                        "You have entered an incorrect username or password.",
                        Toast.LENGTH_SHORT).show();
               // saveLoggedInUId(0, "", "");
            }
            stopManagingCursor(theUser);
            theUser.close();
        }

        else {
            Toast.makeText(getApplicationContext(),
                    "Database query error",
                    Toast.LENGTH_SHORT).show();
        }
    }


private static final String LOGIN_TABLE = "user";
    //Table unique id
    public static final String COL_ID = "id";
    //Table username and password columns 
    public static final String COL_USERNAME = "username";
    public static final String COL_PASSWORD = "password";
    private static final String KEY_PHONE = "tel";
    private static final String INFO = "info";
    private static final String JOB = "info";

public Cursor fetchUser(String username, String password) {
    Cursor myCursor = database.query(LOGIN_TABLE, 
            new String[] { COL_ID, COL_USERNAME, COL_PASSWORD }, 
            COL_USERNAME + "='" + username + "' AND " + 
            COL_PASSWORD + "='" + password + "'", null, null, null, null);

    if (myCursor != null) {
        myCursor.moveToFirst();
    }
    return myCursor;
}

thank you in advance!!!

Aucun commentaire:

Enregistrer un commentaire