DEV Community

Bo
Bo

Posted on

SQLite integer can save up to signed 64-bit with Golang

From this page, it seems SQLite can save up to the max number of uint64:

The sqlite3_int64 and sqlite_int64 types can store integer values between -9223372036854775808 and +9223372036854775807 inclusive. The sqlite3_uint64 and sqlite_uint64 types can store integer values between 0 and +18446744073709551615 inclusive.

But I tried this with a simple table which has id INTEGER PRIMARY KEY and id UNSIGNED INTEGER PRIMARY KEY and with Golang's two SQLite drivers:

  • github.com/mattn/go-sqlite3
  • modernc.org/sqlite

Both throw the error below when I tried to insert an id with the value 9223372036854775808, it works when the number is 9223372036854775807, but not bigger.

sql: converting argument $1 type: uint64 values with high bit set are not supported

The max number it can successfully write to the db is the max number of "signed int64", 9223372036854775807.

I am not sure if this is Golang driver's problem or SQLite itself's INTEGER doesn't really support a big uint64. Since I currently need to support the uint64 data, I guess I'll need to make the id field as a TEXT PRIMARY KEY.

Discussion (0)