Subject | Storing XSQLDA SQLVAR data in an array using C++ |
---|---|
Author | William L. Thomson Jr. |
Post date | 2002-08-07T23:04:07Z |
Ok,
I have a C++ app that I would like to store the data retrieved from my
DB in an array. I originally thought about using a multidimensional
array. Due to having to dynamically resize it, using a single
dimensional array has been allot easier.
So I have been able so far to accomplish most of my needs. Now my
problem seems to be the data type of my array. I thought using a C++
string array would be best, since anything can become a string. Then
later I can convert say a string into a short, long, or etc. Since I do
know what data type each column should return, the reverse casting
should not be to much of a problem.
Now as I have stated before that I am still new to C/C++, but not so
much to programming. However the whole pointer, and mem allocation thing
I am still trying to grasp. The light has not gone on just yet for me,
so I am still feeling around in the dark. So if my code looks crazy,
please understand that is the point I am at.
So one with the code. I am only going to include the part I am having
trouble with. Hopefully that should be enough to understand what I am
trying to do, and where I am going wrong.
I assume my problem now to be a casting problem. Also at the moment I am
trying to work with only a few data types, and am not to concerned about
the Blobs, Arrays, and Quad data types. Mostly just the numeric data
types. The TimeStamp, Time, and Date I am not worried about, as I have
other ideas/plans for them.
So here goes.
if(isc_dsql_execute(status,&tr_handle,&stmt_handle,1,NULL))
ERREXIT(status,1); // Execute the statement
while((fetch_code = isc_dsql_fetch(status,&stmt_handle,1,osqlda)) == 0)
{
num_rows++;
string *temp2 = new string[num_rows*num_cols]; // create a
new array larger than our current one
for(int r=0;r<num_rows-1;r++) for(int c=0;c<num_cols;c++)
temp2[r*num_cols+c] = temp1[r*num_cols+c]; // set each value of our
temp array from the current one
for(int c=0;c<num_cols;c++) {
switch(osqlda->sqlvar[c].sqltype) {
case SQL_TEXT :
temp2[(num_rows-1)*num_cols+c] = osqlda->sqlvar[c].sqldata;
break;
case SQL_SHORT :
temp2[(num_rows-1)*num_cols+c] = *((short *)osqlda->sqlvar[c].sqldata);
break;
case SQL_LONG :
char d = osqlda->sqlvar[c].sqldata;
//temp2[(num_rows-1)*num_cols+c] = *((long
*)osqlda->sqlvar[c].sqldata);
temp2[(num_rows-1)*num_cols+c] = osqlda->sqlvar[c].sqldata;
break;
case SQL_FLOAT :
temp2[(num_rows-1)*num_cols+c] = osqlda->sqlvar[c].sqldata;
break;
case SQL_DOUBLE :
temp2[(num_rows-1)*num_cols+c] = osqlda->sqlvar[c].sqldata;
break;
case SQL_D_FLOAT :
break;
case SQL_TIMESTAMP :
isc_decode_timestamp(((ISC_TIMESTAMP
*)osqlda->sqlvar[c].sqldata),&date_time);
//temp2[(num_rows-1)*num_cols+c] = (date_time.tm_year+1900) << "-" <<
(date_time.tm_mon+1) << "-" << date_time.tm_mday << " " <<
date_time.tm_hour << ":" << date_time.tm_min << ":" << date_time.tm_sec
break;
case SQL_BLOB :
//
break;
case SQL_ARRAY :
//
break;
case SQL_QUAD :
//
break;
case SQL_TYPE_TIME :
isc_decode_sql_time(((ISC_TIME
*)osqlda->sqlvar[c].sqldata),&date_time);
//temp2[(num_rows-1)*num_cols+c] = date_time.tm_hour << ":" <<
date_time.tm_min << ":" << date_time.tm_sec;
break;
case SQL_TYPE_DATE :
isc_decode_sql_date(((ISC_DATE
*)osqlda->sqlvar[c].sqldata),&date_time);
//temp2[(num_rows-1)*num_cols+c] = (date_time.tm_year+1900) << "-" <<
(date_time.tm_mon+1) << "-" << date_time.tm_mday;
break;
}
}
delete [] temp1;
temp1 = temp2;
}
// This is here to show the content of my final array.
for(int r=0;r<num_rows-1;r++) {
for(int c=0;c<num_cols;c++) cout << temp1[r*num_cols+c] << " ";
cout << endl;
}
So in the end, I can get most of the data out of my db into an array. In
the end I will create a final multidimensional array, and delete temp1.
Now I do not delete temp2, and I am not sure I need to, as it only
points to a location which is past to temp1.
My biggest problem that I can't get shorts, and longs into my string
arrays? When I try certain types of cast, all data in my array goes
away? Or I end up with funky characters that are not my numbers.
Sorry if this is vague, I can send the entire file to you if needed, but
this should be enough to get the idea of what I want to do. And where I
have gone wrong.
Not sure if a string array is the best thing to use, but is working so
far. I am not sure what other type of array I could have made?
Also for the record, I am aware of IBPP, but would prefer not to use it
if I do not have to. I have looked into it, but there are things I do
not really understand. So I do not want to be including code or adding
dependencies to my apps that I can't understand or work with. So.
Thanks for any input or responses.
--
Sincerely,
William L. Thomson Jr.
Support Group
Obsidian-Studios Inc.
439 Amber Way
Petaluma, Ca. 94952
Phone 707.766.9509
Fax 707.766.8989
http://www.obsidian-studios.com
I have a C++ app that I would like to store the data retrieved from my
DB in an array. I originally thought about using a multidimensional
array. Due to having to dynamically resize it, using a single
dimensional array has been allot easier.
So I have been able so far to accomplish most of my needs. Now my
problem seems to be the data type of my array. I thought using a C++
string array would be best, since anything can become a string. Then
later I can convert say a string into a short, long, or etc. Since I do
know what data type each column should return, the reverse casting
should not be to much of a problem.
Now as I have stated before that I am still new to C/C++, but not so
much to programming. However the whole pointer, and mem allocation thing
I am still trying to grasp. The light has not gone on just yet for me,
so I am still feeling around in the dark. So if my code looks crazy,
please understand that is the point I am at.
So one with the code. I am only going to include the part I am having
trouble with. Hopefully that should be enough to understand what I am
trying to do, and where I am going wrong.
I assume my problem now to be a casting problem. Also at the moment I am
trying to work with only a few data types, and am not to concerned about
the Blobs, Arrays, and Quad data types. Mostly just the numeric data
types. The TimeStamp, Time, and Date I am not worried about, as I have
other ideas/plans for them.
So here goes.
if(isc_dsql_execute(status,&tr_handle,&stmt_handle,1,NULL))
ERREXIT(status,1); // Execute the statement
while((fetch_code = isc_dsql_fetch(status,&stmt_handle,1,osqlda)) == 0)
{
num_rows++;
string *temp2 = new string[num_rows*num_cols]; // create a
new array larger than our current one
for(int r=0;r<num_rows-1;r++) for(int c=0;c<num_cols;c++)
temp2[r*num_cols+c] = temp1[r*num_cols+c]; // set each value of our
temp array from the current one
for(int c=0;c<num_cols;c++) {
switch(osqlda->sqlvar[c].sqltype) {
case SQL_TEXT :
temp2[(num_rows-1)*num_cols+c] = osqlda->sqlvar[c].sqldata;
break;
case SQL_SHORT :
temp2[(num_rows-1)*num_cols+c] = *((short *)osqlda->sqlvar[c].sqldata);
break;
case SQL_LONG :
char d = osqlda->sqlvar[c].sqldata;
//temp2[(num_rows-1)*num_cols+c] = *((long
*)osqlda->sqlvar[c].sqldata);
temp2[(num_rows-1)*num_cols+c] = osqlda->sqlvar[c].sqldata;
break;
case SQL_FLOAT :
temp2[(num_rows-1)*num_cols+c] = osqlda->sqlvar[c].sqldata;
break;
case SQL_DOUBLE :
temp2[(num_rows-1)*num_cols+c] = osqlda->sqlvar[c].sqldata;
break;
case SQL_D_FLOAT :
break;
case SQL_TIMESTAMP :
isc_decode_timestamp(((ISC_TIMESTAMP
*)osqlda->sqlvar[c].sqldata),&date_time);
//temp2[(num_rows-1)*num_cols+c] = (date_time.tm_year+1900) << "-" <<
(date_time.tm_mon+1) << "-" << date_time.tm_mday << " " <<
date_time.tm_hour << ":" << date_time.tm_min << ":" << date_time.tm_sec
break;
case SQL_BLOB :
//
break;
case SQL_ARRAY :
//
break;
case SQL_QUAD :
//
break;
case SQL_TYPE_TIME :
isc_decode_sql_time(((ISC_TIME
*)osqlda->sqlvar[c].sqldata),&date_time);
//temp2[(num_rows-1)*num_cols+c] = date_time.tm_hour << ":" <<
date_time.tm_min << ":" << date_time.tm_sec;
break;
case SQL_TYPE_DATE :
isc_decode_sql_date(((ISC_DATE
*)osqlda->sqlvar[c].sqldata),&date_time);
//temp2[(num_rows-1)*num_cols+c] = (date_time.tm_year+1900) << "-" <<
(date_time.tm_mon+1) << "-" << date_time.tm_mday;
break;
}
}
delete [] temp1;
temp1 = temp2;
}
// This is here to show the content of my final array.
for(int r=0;r<num_rows-1;r++) {
for(int c=0;c<num_cols;c++) cout << temp1[r*num_cols+c] << " ";
cout << endl;
}
So in the end, I can get most of the data out of my db into an array. In
the end I will create a final multidimensional array, and delete temp1.
Now I do not delete temp2, and I am not sure I need to, as it only
points to a location which is past to temp1.
My biggest problem that I can't get shorts, and longs into my string
arrays? When I try certain types of cast, all data in my array goes
away? Or I end up with funky characters that are not my numbers.
Sorry if this is vague, I can send the entire file to you if needed, but
this should be enough to get the idea of what I want to do. And where I
have gone wrong.
Not sure if a string array is the best thing to use, but is working so
far. I am not sure what other type of array I could have made?
Also for the record, I am aware of IBPP, but would prefer not to use it
if I do not have to. I have looked into it, but there are things I do
not really understand. So I do not want to be including code or adding
dependencies to my apps that I can't understand or work with. So.
Thanks for any input or responses.
--
Sincerely,
William L. Thomson Jr.
Support Group
Obsidian-Studios Inc.
439 Amber Way
Petaluma, Ca. 94952
Phone 707.766.9509
Fax 707.766.8989
http://www.obsidian-studios.com