的SQLite Android的游标的SQL语句

问题描述:

我想写这样的SQLite Android的游标的SQL语句

SELECT * 
FROM TABLE_USER 
WHERE theUser = userID; 

一个SQL查询,但Android使用游标,我不知道如果我这样做的权利。有人可以确认吗?这是为光标查询功能的报头:

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

selection:一个过滤器声明返回哪些行,格式化为SQL WHERE子句(不包括WHERE本身)。传递null将返回给定表的所有行。

这是我的代码:

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

你就忘记了第四个参数,它是你的论点参数。应该是'userId'。 – zgc7009 2014-12-04 20:37:42

+0

谢谢,根据你的指导找到我的答案 – dxiong4 2014-12-04 20:51:18

selectionArgs两个更换任何问号在选择字符串。

例如:

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

参考:

Using String[] selectionArgs in SQLiteDatabase.query()