How to sort an SQLite Table in Swift by Date object

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.

Replies

You seem to be using a third-party SQLite wrapper library. Given that, you might have more luck asking your question via the support channel provided for by that wrapper.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I don't practice SQLLite a lot.


But I wonder if order result is not only visible on the next query.