let query = words.select(*).order(random()).limit(1) is not work
it always show the first one
but "SELECT * FROM words order by random() LIMIT 1" is work
Can you try the following?
words.order(Expression<Int>.random()).limit(1)
This API seems like it can be improved. Thanks for the report!
Here's a test case. Currently it hangs my Xcode 7.2 (_Perhaps Xcode is tired after a long session of bug hunting 馃榾):
import SQLite
let db = try! Connection()
db.trace { print($0) }
let emails = Table("emails")
let to = Expression<String>("to")
let subject = Expression<String?>("subject")
try! db.run(emails.create(ifNotExists: true) {t in t.column(to); t.column(subject)} )
try! db.run(emails.insert(to <- "[email protected]", subject <- "Hello, world!" ))
try! db.run(emails.insert(to <- "[email protected]", subject <- "RE: Hello, world!" ))
try! db.run(emails.insert(to <- "[email protected]", subject <- "Wookies" ))
for user in try db.prepare(emails.order(random()).limit(1)) {
print("id: \(user[to])")
}
for user in try db.prepare("select * from emails by random() limit 1") {
print("id: \(user)")
}
@mikemee This was moved away from being a module function to a static one:
-emails.order(random())
+emails.order(Expression<Int>.random())
Maybe this is a bad API, though?
Most helpful comment
@mikemee This was moved away from being a module function to a static one:
Maybe this is a bad API, though?