views:

160

answers:

4

Hi, I need to create a table (in SQLite) with a column which contains either "-1" or "+1". To save memory it is better not to use "int" as a type of the column. So, I thought about "smallint" and "tinyint". But smallint is not so small (from -32,768 to 32,767) and tinyint can be only positive (from 0 to 255). Are there any other options or I have select between these twp?

Thank you in advance.

+2  A: 

If your field can have only two values in it, why not use a bit/boolean?

snomag
+3  A: 

Boolean would do the job with a tiny processing in the application to map boolean to -1 or +1.

erelender
Why not just store the integer directly? It would be stored as a 1-byte signed integer. There is no native boolean type in sqlite. (See the link in Ionut G. Stan's answer.) A boolean would just be stored as an integer anyway.
dangph
A: 

I recommend you do as other answers suggest and use 0/1 or a bit instead, but if you really really need to do this, Can you in your code, cast the value from a unsigned byte to a char, and then to a signed byte? If you can, then Then 1 (00000001) casts to 1, and 255 (11111111) casts to -1

Charles Bretana
+2  A: 

SQLite 3 presents only an INTEGER data type which...

[...] is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.

Source: http://www.sqlite.org/datatype3.html

Ionuț G. Stan
Well, it *is* SQ**Lite** but they should have called this data type *positive integer* or *ordinal* because -1 seems like a perfectly good 'integer' to me.
pavium
@parvium, it says *signed integer*.
dangph
@pavium: "signed integer" means it can be either positive or negative, you can think of it as having a *+ sign* or *- sign* on it.
Adam Bellaire