storing "small binary objects"

Started by Swaminathan Natarajanabout 25 years ago2 messagesgeneral
Jump to latest
#1Swaminathan Natarajan
swami@dcs.uky.edu

hi,

I am relatively new to postgresql. Sorry if this is a rather naive
question.

I am trying to store a fixed sized c++ class into the database and
retreive it. What is the simplest (dirtiest?!) way to do it? I couldnt
find any example on the web or in the pgsql/src directories that I could
get to work.

Here is what i did.

I tried defining a column as char(sizeof class) using...
create table firstTable(id integer,className char(sizeofclass));

Then in my c++ program, I used....

char* someVariable=(char*)(&classInstance)
//character handle to the class

exec sql insert into firstTable(id,className)
values(:temp,:someVariable);
//save "someVariable" 'asis' into the column

Then I tried reading the value back in...

someVariable=(char*)malloc(sizeof(class));
memset(someVariable,0,sizeof(class));
exec sql select id, className
into :id,:someVariable
from firstTable
where id = 5;
//read "someVariable" back in

While I got the value of id (and there is definitely a valid record), the
"someVariable" memory location is blank. What am I doing wrong?

In addition to examples available with the distribution, pointers to some
more sample code would help.

Thanks,
Swami.

#2Eric G. Miller
egm2@jps.net
In reply to: Swaminathan Natarajan (#1)
Re: storing "small binary objects"

On Wed, Mar 28, 2001 at 07:41:36PM -0500, Swaminathan Natarajan wrote:

hi,

I am relatively new to postgresql. Sorry if this is a rather naive
question.

I am trying to store a fixed sized c++ class into the database and
retreive it. What is the simplest (dirtiest?!) way to do it? I couldnt
find any example on the web or in the pgsql/src directories that I could
get to work.

Map your public instance data to the fields of one or more tables, then
when you reread the tuple(s) map them back to a "new" instance of the
class using its "set" methods. Look at the libpq++ methods rather than
embedded C. It may not be the simplest/dirtiest method, but then for
what you're doing, why not just use the filesystem? Or DBM embedded
database file?

Here is what i did.

I tried defining a column as char(sizeof class) using...
create table firstTable(id integer,className char(sizeofclass));

Then in my c++ program, I used....

char* someVariable=(char*)(&classInstance)
//character handle to the class

exec sql insert into firstTable(id,className)
values(:temp,:someVariable);
//save "someVariable" 'asis' into the column

Then I tried reading the value back in...

someVariable=(char*)malloc(sizeof(class));
memset(someVariable,0,sizeof(class));
exec sql select id, className
into :id,:someVariable
from firstTable
where id = 5;
//read "someVariable" back in

While I got the value of id (and there is definitely a valid record), the
"someVariable" memory location is blank. What am I doing wrong?

In addition to examples available with the distribution, pointers to some
more sample code would help.

--
Eric G. Miller <egm2@jps.net>