Home > Software engineering >  Equivalent of sqlite3_column_int to get possible NULL values
Equivalent of sqlite3_column_int to get possible NULL values

Time:02-15

in my app, I am using a SQLite database to store some data. One of the integer fields is optional, so it is defined like so:

I have the following CREATE statement:

CREATE TABLE IF NOT EXISTS Test (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp FLOAT NOT NULL, testProperty INT);

I can get the float property using sqlite3_column_double. For the int column I could use 'sqlite3_column_int' but this always returns a value (0) even if the row does not contain a value.

How can I check if the row actually has a value for this property?

I have the following code to get all rows:

var statement: OpaquePointer? = nil
let sql = "SELECT * FROM Test;"
   
sqlite3_prepare_v2(self.connection, sql, -1, &statement, nil)
    
while sqlite3_step(statement) == SQLITE_ROW
{
  let testProperty = sqlite3_column_int(statement, 2) // always returns 0
}
    
sqlite3_finalize(statement)

CodePudding user response:

You can use sqlite3_column_type() to check if it's SQLITE_INTEGER or SQLITE_NULL.

  • Related