dxiong4
dxiong4

Reputation: 66

SQLite Android Cursor sql statement

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;
}        

Upvotes: 0

Views: 1024

Answers (1)

dxiong4
dxiong4

Reputation: 66

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()

Upvotes: 2

Related Questions