typedef struct XY XY;
struct XY {
int x;
int y;
};
XY *pXY;
/* When retrieving values, don't do this: */
pXY = (XY *)sqlite3_column_blob(...);
/* Instead do this: */
pXY = (XY *)malloc(sizeof(XY));
memcpy(pXY, sqlite3_column_bloc(...), sizeof(XY));
--- Nathan Kurz <[EMAIL PROTECTED]> wrote:
> On Wed, Dec 07, 2005 at 08:34:46PM -0800, Dan Kennedy wrote:
> > > To make it work in more than a superficial manner, you probably will
> > > need a good understanding of how structures are internally represented
> > > in C++ or C. You pass sqlite a pointer to the struct and tell it how
> > > long it is (using sizeof()). When you get the blob back, you then
> > > treat the data as an instance of your structure by casting it.
> >
> > I'm not sure this is always the best way to go. Any databases produced like
> > this will not be portable between architectures. In theory, they may not
> > be portable between compilers, but in practice you're probably Ok there.
>
> A good caveat. If any of the members of your structure are more than
> one byte long (ie, an int) this approach will fail terribly if you
> create the database on a little-endian machine (Intel/PC) and then try
> to use it on a little-endian one (Motorola/Mac). But I decided to
> take the question as 'How do I', rather than 'Should I'.
>
> > Also, SQLite makes no guarantees as to the alignment of returned blobs.
> > This could cause a random crash somewhere down the line. You could get
> > around this by making a copy of the blob to memory obtained from malloc().
>
> Can you offer more information about this? In what I'm working on, I
> am storing arrays of complex structures as blobs and then reconstituting
> them. I was concerned about alignment at one point, but it seems to
> be working without problems. Are there 'gotchas' that are likely to
> 'get me' in the future? This is server-side, and only for temporary
> data, so I'm not concerned about the endian-ness, only the alignment.
>
> Thanks!
>
> --nate
>
>
>
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com