Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLITE_INTEGER value bytes

Tags:

sqlite

I have a question about the data types in sqlite3.

As a value of SQLITE_INTEGER can be stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value, if I only know that a column in SQlite database stores SQLITE_INTEGER, how can I know a value in this column is 4 bytes or 6-8 bytes integer, or which one should be used to get the value, sqlite3_column_int() or sqlite3_column_int64()?

Can I use sqlite3_column_bytes() in this case? but according to the documentation, sqlite3_column_bytes() is primarily used for TEXT or BLOB.

Thanks!

like image 974
Lewis Avatar asked Nov 04 '22 08:11

Lewis


1 Answers

When SQLite steps into a record, all integer values are expanded to 64 bits.
sqlite3_column_int() returns the lower 32 bits of that value without checking for overflows.

When you call sqlite3_column_bytes(), SQLite will convert the value to text, and return the number of characters.

You cannot know how large an integer value is before reading it. Afterwards, you can check the list in the record format documentation for the smallest possible format for that value, but if you want to be sure that integer values are never truncated to 32 bits, you have to always use sqlite3_column_int64(), or ensure that large values get never written to the DB in the first place (if that is possible).

like image 179
CL. Avatar answered Nov 20 '22 01:11

CL.