SQLite Forum

Type of the column
Login
Other answers are excellent but maybe this will be helpful to you (or someone).

SQLITE_INTEGER doesn't tell you anything about the size of the integer. Only that it is integral. It could be stored in one of several ways, from 8- up to 64-bit (though not all variants are used internally; there is no such thing as a 10-bit integer within the SQLite storage model, for example).

If you truly don't know in advance what size of data you expect to get back from the database, you must assume it is the full 64-bit and use the int64 API.

If you want to know "in advance" what size of an integer you will need, you'll need something like the following pseudo-code:

```
#define EXTENDED_INT8    -1
#define EXTENDED_INT16   -2
#define EXTENDED_INT32   -3
#define EXTENDED_INT64   -4

#define EXTENDED_BETWEEN(v, l, h) ((l <= v) && (v <= h))

int extended_column_type(sqlite3_stmt* pStmt, int iCol)
{
  int type = sqlite3_column_type(pStmt, iCol);
  if (type == SQLITE_INTEGER)
  {
    sqlite3_int64 value = sqlite3_column_int64(pStmt, iCol);
    if (EXTENDED_BETWEEN(value, -128, 127))
      type = EXTENDED_INT8;
    else if (EXTENDED_BETWEEN(value, -32768, 32767))
      type = EXTENDED_INT16;
    else if (EXTENDED_BETWEEN(value, -2147483648, 2147483647))
      type = EXTENDED_INT32;
    else
      type = EXTENDED_INT64;
  }
  return type;
}
```

I've not tried to compile that so it might not be perfect, and there are other ways to do it which might be "better" based on some criteria, but I hope it is illustrative of how you can build your own type checker that provides finer granularity than what SQLite offers by default.