SQLite Android Cursor sql语句

时间:2014-12-04 20:28:06

标签: android database sqlite cursor

我正在尝试编写像这样的SQL查询

SELECT * 
FROM TABLE_USER 
WHERE theUser = userID; 

但Android使用Cursor,我不确定我是否正确这样做。有人可以证实吗?这是Cursor查询函数的标题:

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

1 个答案:

答案 0 :(得分:2)

selectionArgs替换选择字符串中的任何问号。

例如:

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

参考:

     

Using String[] selectionArgs in SQLiteDatabase.query()