Dear Friends,
I am in the process of forming a Generic API,(sql oriented and
BerkelyDB
and sister databases). In the process of integration ,i like to store a
Structure in Sqlite.
as far as my knowledge SQLITE allows me to declare the column types
suppoted by the programming languare or say i am using blob . My
requirement
is i wish to store a structure in the SQLite column.
I am unable to form a sql statement to store the structure ,i am also
not
clear with whether i can have a strucure as column type.
suggestions will be really helpful.
Thanking you,
B.Narendran
A C struct is already a blob. Inserting it to a table is quite
straightforward. I assume you already created your tables, and have your
connection open. You can try these to insert a struct:
struct MyStruct {
long nSomeStuff[1024];
};
MyStruct thisStruct = {0};
sqlite3* db; // already opened
sqlite3_stmt* pStmt = NULL;
const char* pszUnused;
sqlite3_prepare (db, "INSERT INTO TABLE (BLOBCOLUMN) VALUES (?);", -1,
&pStmt, &pszUnused);
sqlite3_bind_blob (pStmt, 1, &thisStruct, sizeof(MyStruct), SQLITE_STATIC);
sqlite3_step (pStmt);
sqlite3_finalize (pStmt);
Blob data must be prepared using a wildcard (?) and be bound later. Remember
that when binding, the index of the first column is 1, not 0. And you have
to check return values for each of the sqlite3_* functions, they may fail or
return busy. There's no need to do any memory copy. If you will destroy the
struct before sqlite3_step is called, then change SQLITE_STATIC to
SQLITE_TRANSIENT. This way, sqlite will make an internal copy when
sqlite3_bind_blob is called.
When retrieving data, the size of the column is determined by
sqlite3_column_bytes. You use the value returned by this function to decide
how much memory you needed to copy from the pointer returned by
sqlite3_column_blob to your own struct.
I'm not sure if it'll help you to understand, in the eye of a database
system, a C struct doesn't have any difference to the data in a block of
memory buffer, or something like long long nVars[100];. They only need two
things to get started, a pointer, and the size.
Best regards,
He Shiming
-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------