Kevin M. <athlo...@yahoo.com> 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.

Can't you just store the struct as a blob and be done with it?

> However, this method breaks down if a store a 16-bit integer value
> like "99"

Why is 99 a 16-bit integer value and not, say, an 8-bit or a 32-bit one? How is 
SQLite supposed to know?

> 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().

sqlite3_column_bytes is really only meaningful for strings and blobs, not for 
numbers.

> 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?

In order to preserve it, you need some way to specify it first. How do you plan 
to do that?

> 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. 

Well, if you want to invent a naming convention like that, why don't you act on 
it yourself? Retrieve the type name with sqlite3_column_decltype, handle it as 
you see fit.

> I'd prefer a way to do this without having to cast every last column
> in a SELECT query to the right size.

You can't do that anyway, even if you wanted to. There's no syntax in SQLite 
that would support that.
-- 
Igor Tandetnik


_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to