Would declaring INTEGER column as BIGINT help?
I am using Lazarus/FPC with SQLite through SQLDB / TSQLQuery.
I store data in SQLite with millisecond timestamps, for example:
CREATE TABLE mytable (
...,
timestamp_ms INTEGER NOT NULL,
...)
);
Example value:
timestamp_ms = 1780324920000
The value is stored correctly in SQLite. Queries directly in SQLite show the correct value.
However, when I read it through TSQLQuery, this gives a 32-bit overflowed value:
C.Timestamp := Q.FieldByName('timestamp_ms').AsLargeInt;
The result becomes something like:
-2146207840instead of:
1780324920000Using AsLongint is obviously wrong because the value is too large for 32-bit, but I expected `AsLargeInt` to work.
I found a workaround:
SELECT CAST(timestamp_ms AS TEXT) AS timestamp_ms_text
FROM mytable
and then:
C.Timestamp := StrToInt64(Q.FieldByName('timestamp_ms_text').AsString);
This works correctly.
My questions:
1. Is this expected behavior with SQLite + SQLDB/TSQLQuery?
2. Does SQLDB infer the field type from the declared SQLite column type?
3. Would changing the schema from:
timestamp_ms INTEGER NOT NULL
to:
timestamp_ms BIGINT NOT NULL
or:
timestamp_ms INT8 NOT NULL
make TSQLQuery.FieldByName(...).AsLargeInt return the correct 64-bit value?
4. Is there a recommended way to read SQLite 64-bit integer values safely through SQLDB?
5. Should I continue using CAST(timestamp_ms AS TEXT) + StrToInt64, or is there a better solution?
I know SQLite itself stores integers as up to 64-bit regardless of names like INTEGER, BIGINT, etc., but I am wondering whether the declared type name affects the field class/type chosen by SQLDB.
Environment:
Lazarus/FPC: [trunk/trunk]
Database: SQLite
Access layer: SQLDB / TSQLQuery / TSQLite3Connection
Platform: Linux x86_64
Thanks.