从Swift中的SQL查询中排除值

时间:2015-06-18 02:13:30

标签: sqlite swift

此代码触发错误,指出rightAnswernil。它应该很简单;在添加namesSoFar变量和查询的and name not in部分之前,它工作正常。我还尝试将not in更改为in,可能会将我的查询限制为这三个值(我肯定会在那里)并得到相同的错误。

namesSoFar = ["Bob", "Susan", "Gordon"]
let querySQL = "SELECT name, picture_name from member_data where picture_name is not ? and name not in ? ORDER BY RANDOM() LIMIT 1";
rightAnswer = memberDatabase!.executeQuery(querySQL, withArgumentsInArray: ["None", namesSoFar])
rightAnswer!.next()

编辑:

仍在尝试调试。

这有效:

    let querySQL = "SELECT name, picture_name from member_data where picture_name is not ? and name is ? ORDER BY RANDOM() LIMIT 1";
    rightAnswer = memberDatabase!.executeQuery(querySQL, withArgumentsInArray: ["None", "Bob"])

这些不是:

    let querySQL = "SELECT name, picture_name from member_data where picture_name is not ? and name in ? ORDER BY RANDOM() LIMIT 1";
    rightAnswer = memberDatabase!.executeQuery(querySQL, withArgumentsInArray: ["None", ["Bob"]])

rightAnswer = memberDatabase!.executeQuery(querySQL, withArgumentsInArray: ["None", ("Bob")])

1 个答案:

答案 0 :(得分:0)

显然正确的语法是

    let querySQL = "SELECT name, picture_name from member_data where picture_name is not ? and name not in (?, ?) ORDER BY RANDOM() LIMIT 1";
    rightAnswer = memberDatabase!.executeQuery(querySQL, withArgumentsInArray: ["None", "Bob", "Susan"])