3

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)
inexcitus
  • 2,471
  • 2
  • 26
  • 41
  • 1
    Does this answer your question? [How to check a value in a SQLite column is NULL or not with C API?](https://stackoverflow.com/questions/8961457/how-to-check-a-value-in-a-sqlite-column-is-null-or-not-with-c-api) – Anon Coward Feb 14 '22 at 19:08
  • 1
    Why did you delete your last question? If you undelete it I can post a solution if you haven't figure it out yet. – Leo Dabus Feb 22 '22 at 14:20

1 Answers1

3

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

Blindy
  • 65,249
  • 10
  • 91
  • 131