Clifton Labrum
Clifton Labrum

Reputation: 14070

Detect the Type of a Column with SQLite.swift

I am reading two different SQLite files and reading the 4th column. In one of the files, the type is REAL and in the other, it's INTEGER.

When I try to extract the value, I am unable to ever determine the type:

for row in try! db.prepare("SELECT * FROM things") {
  let value = row[4]

  print(value) //This logs "Optional(7)" or "Optional(1.2)" etc.

  switch value{

    case let double as Double:
      print("Double: \(double)") //This never gets called...

    case let int as Int:
      print("Int: \(int)") //This never gets called

    default:
      return 0 //This always gets called
  }
}

How can I accurately detect the type of the column?

Upvotes: 1

Views: 652

Answers (2)

Ted
Ted

Reputation: 23746

        switch sqlite3_column_type(sqliteStatement, index) {
        case SQLITE_TEXT:
            // handle text
        case SQLITE_BLOB:
            // handle image
        case SQLITE_NULL:
            // handle null
        case SQLITE_FLOAT:
            // handle float
        case SQLITE_INTEGER:
            // handle integer
        default:
            break
        }

Upvotes: 1

Clifton Labrum
Clifton Labrum

Reputation: 14070

I figured this out. I needed to check against Int64 instead of Int.

Upvotes: 0

Related Questions