Solved

Saving user defined variables as objects in databases

Posted on 1997-04-28
6
175 Views
Last Modified: 2010-04-02
Is it possible to read and write a user defined struct variable into a DB2, Oracle, or Informix database (or any other database) as a single object?  I want to save structs which contain arrays, for example,


struct MyStruct
{
int firstmember[100]
char secondmember[200]
.....
}


I want to read and write whole structs to the database fields without breaking them down into individual data members. Please refer me to a source of technical info.
Thanks!



0
Comment
Question by:aderounm
6 Comments
 

Expert Comment

by:davidf
ID: 1250151
I don't have any technical references to give you but I think I can steer you in the right direction...

Look into what sort of support your DB has for BLOB fields (Binary Large OBjects).  Without support for binary data you won't be able to store C structures in your DB.
0
 

Expert Comment

by:jfbe
ID: 1250152

 Personally I know little about Oracle and DB2 since I've use
 codebase (dbase compatible) btrieve, ctree or other kinds of
 DB softwares. However some princips are identicals: you need
 to have a field that accept binary codes, special codes that
 are not a well known letter and this was the case of a char
 field in codebase as far I can remember. I've an oracle book  with me (old however) and it seems that a long datatype for a
 field can be as large as 65,536 characters, so enough to store
 a struct. You can use also a random access file in pure C
 that you create yourself if you have compatibility problems to
 cast binary numbers  into a numerical field, but normally it  should work with oracle. all you need is to cast your structure
(ex: in a specific database you'd write

write( (char *) &Mystruct)

 rather than write(MY_array_of_chars)

 and to retrieve it you cast the same way: x=(Mystruct *)read();

 These are arbitrary api's, depend of the database you use, you can replace read and write in such a case. The only thing you
have to do is to cast your structure and make a call by reference.
0
 

Author Comment

by:aderounm
ID: 1250153
Please include a technical reference so I can know where to look for info. Thanks.
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

Author Comment

by:aderounm
ID: 1250154
Adjusted points to 130
0
 

Accepted Solution

by:
cwestin earned 130 total points
ID: 1250155
The answer is yes, you can write a struct there.  As someone else commented above, you will have to save the structure to a binary data type.  I'm familiar with Oracle, so I'll describe that here.  You can look up more detailed information in the Oracle OCI (Oracle Call Interface) manual, sometimes also known as the HLI (High-Level Interface.)  The manual contains descriptions of the Oracle API for several languages, so be sure to look in the C section.

Essentially, you have to bind (for input) your struct as a LONG RAW datatype (in oradefs.h, SQLT_BIN).  You will do this bind using one of obndra() or its relatives (see the OCI manual.)  To get the data out, you will define output space using ocidfn() using the same type (SQLT_BIN).  For the size of the data, use sizeof(yourstruct).  In both cases, binding and defining, you have to provide the address of the struct.

Note that if you do this, the data is stored in a binary form that Oracle cannot interpret, so you can only store and retrieve the data; you will not be able to query against its content.  You will have to create some table that has another key you plan to use, e.g., "create table foo(your_key char(100), your_data long raw(500))".

You should use the LONG RAW datatype so that Oracle does not try to do character set conversions on the data.  Also note that the data is stored as its binary image looks in memory according to the compiler you are using.  You may not be able to retrieve and use this data on another platform other than the one that your wrote it from, because the structure contents (offsets) may be different on another platform.

0
 

Author Comment

by:aderounm
ID: 1250156
Thanks a million!
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to organize data in excel ? 2 114
mixing C++ and C code elegantly 10 156
UPD maximums on Red Hat 6 114
Windows 2012 R2 -- create 3.5TB GPT from unallocated space ? 1 32
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question