0

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!!!

Update:

String name = theUser.getString(theUser.getColumnIndex(DatabaseAdapter.COL_USERNAME));
String phone = theUser.getString(theUser.getColumnIndex(DatabaseAdapter.KEY_PHONE));
String tel = theUser.getString(theUser.getColumnIndex(DatabaseAdapter.KEY_PHONE));
String info = theUser.getString(theUser.getColumnIndex(DatabaseAdapter.INFO));
String job = theUser.getString(theUser.getColumnIndex(DatabaseAdapter.JOB));
String id = theUser.getString(theUser.getColumnIndex(DatabaseAdapter.COL_ID));

How can I wrote request fir this one:

public Cursor fetchAllUsers() {
    return database.query(LOGIN_TABLE, new String[] { COL_ID, COL_USERNAME, 
            COL_PASSWORD }, null, null, null, null, null);
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Sasha
  • 644
  • 8
  • 22

1 Answers1

1

To move between individual data rows, you can use the moveToFirst() and moveToNext() methods. The isAfterLast() method allows to check if the end of the query result has been reached.

Cursor provides typed get*() methods, e.g. getLong(columnIndex), getString(columnIndex) to access the column data for the current position of the result. The "columnIndex" is the number of the column you are accessing.

Cursor also provides the getColumnIndexOrThrow(String) method which allows to get the column index for a column name of the table.

A Cursor needs to be closed with the close() method call when you are finished with it.


Your code should look something like this to get the info column, but please note in your question that the JOB and INFO columns have the same string, so they'll return the same column index until you fix that

String info = theUser.getString(theUser.getColumnIndex(DatabaseAdapter.INFO))
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • that means all data comes with Cursor and I dont have to make other methods – Sasha Feb 29 '16 at 15:16
  • You already made a method that returns a Cursor, so no, you don't have to make other methods, unless you want to. – OneCricketeer Feb 29 '16 at 15:34
  • Thank you Finally I figure it out!!! but here is only way how to get that?I update questin – Sasha Feb 29 '16 at 15:40
  • I'm not sure what you're asking. If you have additional questions, you are welcome to make another post and I'm sure someone will be willing to help. You have already accepted the answer here. – OneCricketeer Feb 29 '16 at 15:43
  • How to get all data using that cursor – Sasha Feb 29 '16 at 15:51
  • like String info = theUser.getString(theUser.getColumnIndex(DatabaseAdapter.INFO)) in previous – Sasha Feb 29 '16 at 15:51
  • You are only querying the three columns `{ COL_ID, COL_USERNAME, COL_PASSWORD }` in your code, so you won't be able to get the `INFO` column anyways. Please read this answer. http://stackoverflow.com/a/10601764/2308683 – OneCricketeer Feb 29 '16 at 15:58