Search code examples
c++mysqlbigintunsigned-long-long-intudf

c++ usigned long long range vs mysql unsigned bigint range


I have a MySQL UDF that returns unsigned long long value. When i call this function in MySQL and the Data has been saved in database, MySQL returns warning:
"BIGINT UNSIGNED value is out of range".
How can i save large unsigned long long numbers in database?

MySQL version: 5.5.43
OS : Ubuntu 14.04

please help.


Solution

  • Assuming your unsigned long long data type is 64-bit, it should fit quite well into the MySQL BIGINT UNSIGNED type, which is also 64 bits.

    I would suggest checking your C implementation limits.h ( or climits for C++) file for the macro ULONG_MAX to ensure it is only 64 bits.


    You should be aware however that there are certain operations you can perform on an unsigned value that will also give you this error. It may not be applicable if you're simply storing the exact value returned from the UDF but, if you're doing something like:

    select my_udf() - 22 from somewhere
    

    and my_udf() returns 15 (for example), you'll see that error message because -7 cannot be represented in an unsigned type.

    If that is the case, the solution may be as simple as casting the return value to a signed type, but we would need more information on what it can return to advise correctly.