Kevin M. wrote:
> I have a C/C++ application in which I want to store data from a struct into a
> table (using SQLite 3.6.23) and later retrieve data from the table and store
> it back in the struct. But, I need a general interface for this as there are
> many types of structs used. So, what I have is a function that accepts a
> pointer to the struct (ptr), the struct size, and a sqlite3_stmt* variable
> for the current row. I then iterate through each column of the row and store
> the value retrieved at ptr's address. Then I increment ptr based on the size
> of the column. Thus, as long as I store and retrieve columns in the same
> order I should be storing the right values in the right variables for a
> particular struct instance.
>
> However, this method breaks down if a store a 16-bit integer value like "99"
> and SQLite stores it internally as an 8-bit value (to save space) and
> subsequently retrieves it and gives me a value of 1 for
> sqlite3_column_bytes(). This causes alignment issues with the variables in
> the struct. So, is there a way I can tell SQLite to preserve the data size
> in a particular column? E.g.:
>
> CREATE TABLE test ( val1 INTEGER, val2 INT2, val3 INT1 ... );
>
> Here val1 is always 4-bytes, val2 is always 2 bytes, and val3 is always 1
> byte.
>
> I'd prefer a way to do this without having to cast every last column in a
> SELECT query to the right size.
>
The layout of a column is an implementation detail and not part of
SQLite's type system. I wouldn't recommend depending on it.
You might want to consider writing a user-defined function to convert a
column row into a BLOB that matches your struct layout. Then you can
just write
SELECT PACK('ihb', val1, val2, val3) FROM test
and memcpy the resulting BLOB into your struct.
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users