I want to be able to either set a sorting condition for my SQLite Table or sort it every time a new entry is inserted.
The SQlite docs say to use
query.order(expression)
Currently this is what I have:
class SQLiteData {
var fileURL : URL
var tasks : Table
var titleDb : Expression
var descriptionDb : Expression
var dateDb : Expression
init() {
//...
tasks = Table("Tasks")
titleDb = Expression("Title")
descriptionDb = Expression("Description")
dateDb = Expression("Date")
tasks.order(dateDb)
}
but the line
tasks.order(dateDb)
doesn't seem to have any effect...I also tried it in my
uploadData
function: func uploadData(_ title: String, _ description: String, _ date: String) {
do {
let db = try Connection(fileURL.path)
try db.run(tasks.create(ifNotExists: true) { t in
t.column(titleDb, unique : true)
t.column(descriptionDb)
t.column(dateDb)
})
let insert = tasks.insert(titleDb <- title, descriptionDb <- description,
dateDb <- date)
_ = tasks.order(dateDb)
let rowid = try db.run(insert)
}
catch {
print(error)
}
}
but alas have not had any success doing this either. Any ideas how would I setup the table to always be sorted by
dateDb
? Thank you.