c++ - SQLITE CHECK constraint for hex values -
i want store ip address using c++ in sqlite3 db in hex format. using text format storing hex values. want perform check on value being inserted in db. value range want check 0x00000000 - 0xffffffff. integer type can check - check (num between 0 , 100). there way add check constraint hex values?
if there smarter way store ip address in sqlite please share me.
thanks
i think have 2 main choices: (a) store hex (i.e. text) , check "hex conformity", or (b) store integer , print hex when reading data.
there may several reasons preferring 1 on other, e.g. if application provides , receives integer values. anyway, examples option (a) , option (b).
option (a) - store text , check hex conformity
the simplest way use check based on glob remarked cl:
value text constraint "valueishex" check(value glob "0x[a-fa-f0-9][a-fa-f0-9][a-fa-f0-9][a-fa-f0-9][a-fa-f0-9][a-fa-f0-9][a-fa-f0-9][a-fa-f0-9]")
if logic goes beyond supported glob, install user defined function, either general regexp()
function or custom function specific needs. case, if want store complete ip-address in 1 field , still want conformity check. on general regexp()
function, confer this answer. on user defined functions confer, example, stackoverflow answer.
option (b) - store int , print hex
if application working integers, change db schema integer , add check like:
value integer constraint "valueisvalid" check(value <= 0xffffffff)
for convenience, can still print value hex using query (or defining corresponding view) following:
select printf('%08x', value) thetable
Comments
Post a Comment