0

I am trying to write a SQL query like this

SELECT * 
FROM TABLE_USER 
WHERE theUser = userID; 

but Android uses Cursor, and I'm not sure if I am doing this right. Can someone confirm? This is the header for the Cursor query function:

public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)

selection: A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table.

And this is my code:

public ArrayList<Goal> getAllGoals(User user) {

    int currentUserID = user.getUserID();
    String theUser = Integer.toString(currentUserID);

    String[] columns = new String[] {userID, goal, activityType, target_steps, completed_steps};

    Cursor c = db.query(TABLE_GOAL, columns, theUser, null, null, null, null);

    ArrayList<Goal> allGoals = new ArrayList<Goal>();


    while(c.moveToNext()){
        c.getInt(currentUserID);
        c.getInt()
    }
    c.close();


    return allGoals;
}        
2
  • You are forgetting the 4th argument, it is your argument parameter. Should be something like userId. Commented Dec 4, 2014 at 20:37
  • Thank you, found my answer based on your guidance Commented Dec 4, 2014 at 20:51

1 Answer 1

2

selectionArgs replace any question marks in the selection string.

for example:

String[] args = { "first string", "[email protected]" };
Cursor cursor = db.query("TABLE_NAME", null, "name=? AND email=?", args, null);

reference:

Using String[] selectionArgs in SQLiteDatabase.query()

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

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.