Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of sqlite3_column_int to get possible NULL values

Tags:

sql

sqlite

swift

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)
like image 930
inexcitus Avatar asked Nov 24 '25 23:11

inexcitus


1 Answers

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

like image 92
Blindy Avatar answered Nov 26 '25 16:11

Blindy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!