Determining Column Data Type
Posted in 2000
Topics: Connectivity: ESQL/C, 4GL & Embedded SQL, Data Types & Schema Design, Third-Party Tools & Monitoring
Hello!
I have a problem to determinate what is the float type in a ESQL/C
function.
First, I created a table TA:
CREATE TABLE TA (a float, b smallfloat, c real)
Then; I made a program that only show the column data type:
printf ("\\nType a(%d):",da_ptr->sqlvar[0].sqltype);
printf ("\\nType b(%d):",da_ptr->sqlvar[1].sqltype);
printf ("\\nType c(%d):",da_ptr->sqlvar[2].sqltype);
And the output is:
Type a: 5
Type b: 5
Type c: 5
I don't understand why is DECIMAL type? (5)
How can I obtain the SMALLFLOAT type and FLOAT type?
Thank you
Hello! I only want to show the code. Thank you. ----------------------------------------------------------------------- #include <stdio.h> #include <ctype.h> EXEC SQL include sqltypes.h; EXEC SQL include sqlda.h; int init_sqlda (struct sqlda *in_da) { int i; struct sqlvar_struct *col_ptr; for (i=0, col_ptr= in_da->sqlvar; i<in_da->sqld; i++, col_ptr++) { /* Calcula el tamany per una estructura equivalent en C */ col_ptr->sqllen= rtypmsize (col_ptr->sqltype, col_ptr->sqllen); /* Reserva espai per dades */ col_ptr->sqldata= (char *)malloc (col_ptr->sqllen); if (!col_ptr->sqldata) return 0; } return(1); } void main (void) { struct sqlda *da_ptr; char buffer[33]; int i; EXEC SQL begin declare section; char pass[10]= "xxxxxxx"; char comanda[128]= "select * from a"; int ent; EXEC SQL end declare section; EXEC SQL connect to 'bd_prov2@ada_dbd' user 'becsgbd' using :pass; if (SQLCODE <0) { printf("Error conexio: %d\\n", SQLCODE); exit(0); } EXEC SQL prepare sel_id from :comanda; EXEC SQL declare sel_curs cursor for sel_id; EXEC SQL describe sel_id into da_ptr; if (!init_sqlda(da_ptr)) { printf("Error: out of memory"); exit(0); } printf("Num columns: %d \\n", da_ptr->sqld); for (i=0; i<da_ptr->sqld; i++) { printf("Name Col%d: %s \\t", i, da_ptr->sqlvar[i].sqlname); printf("Type Col%d: %d\\n", i, da_ptr->sqlvar[i].sqltype); } EXEC SQL free sel_id; free (da_ptr); EXEC SQL disconnect current; } -----------------------------------------------------------------------
Xavier Sáez wrote:
> I have a problem to determinate what is the float type in a ESQL/C
> function.
>
> First, I created a table TA:
> CREATE TABLE TA (a float, b smallfloat, c real)>
> Then; I made a program that only show the column data type:
> printf ("\\nType a(%d):",da_ptr->sqlvar[0].sqltype);
> printf ("\\nType b(%d):",da_ptr->sqlvar[1].sqltype);
> printf ("\\nType c(%d):",da_ptr->sqlvar[2].sqltype);
>
> And the output is:
> Type a: 5
> Type b: 5
> Type c: 5
>
> I don't understand why is DECIMAL type? (5)
> How can I obtain the SMALLFLOAT type and FLOAT type?
Xavier also submitted the code (see below).
Since there's nothing obviously wrong with the code, the question
becomes -- under what circumstances does Informix change FLOAT and
SMALLFLOAT to DECIMAL for you? One answer can be found by reading
the fine print in the Informix ESQL/C Programmer's Guide in the
section on the sqlca structure. For sqlca.sqlwarn.sqlwarn4 on
connecting
to a database, it says "Set to W when the database server stores the
FLOAT data type in DECIMAL form (done when the host system lacks support
for FLOAT types)". Well, very few systems these days don't have support
for float and double, and I don't think any server is compiled without
using that support. But there'd be another case (I think) where it
might be used, and that is when the machines at the two ends of a data
connection have very different machine types (eg Sparc and Intel). In
that case, you might get the FLOAT and SMALLFLOAT data transferred using
the DECIMAL format.
For example, I connected from my Linux PC to my Sparc workstation (with
SQLCMD, of course) and switched type printing on, and when I did:
create temp table t(x float);
insert into t values(3.14159);
select * from t;
The type came back as DECIMAL(16).
Thus, the manual entry (quoted from the 7.2 version, Doc) is misleading
because the described circumstance is very very rare and the common
circumstance is not mentioned, and incomplete because it doesn't mention
SMALLFLOAT which gets the same generic treatment (except for DECIMAL(8)
instead of DECIMAL(16)).
--
Yours,
Jonathan Leffler (Jonathan.Leffler@Informix.com) #include <disclaimer.h>
Guardian of DBD::Informix v1.00.PC1 -- http://www.perl.com/CPAN
"I don't suffer from insanity; I enjoy every minute of it!"
-------------------------------------
#include <stdio.h>
#include <ctype.h>
EXEC SQL include sqltypes.h;
EXEC SQL include sqlda.h;
int init_sqlda (struct sqlda *in_da)
{
int i;
struct sqlvar_struct *col_ptr;
for (i=0, col_ptr= in_da->sqlvar; i<in_da->sqld; i++, col_ptr++) {
/* Calcula el tamany per una estructura equivalent en C */
col_ptr->sqllen= rtypmsize (col_ptr->sqltype, col_ptr->sqllen);
/* Reserva espai per dades */
col_ptr->sqldata= (char *)malloc (col_ptr->sqllen);
if (!col_ptr->sqldata) return 0;
}
return(1);
}
void main (void)
{
struct sqlda *da_ptr;
char buffer[33];
int i;
EXEC SQL begin declare section;
char pass[10]= "xxxxxxx";
char comanda[128]= "select * from a";
int ent;
EXEC SQL end declare section;
EXEC SQL connect to 'bd_prov2@ada_dbd' user 'becsgbd' using :pass;
if (SQLCODE <0) {
printf("Error conexio: %d\\n", SQLCODE);
exit(0);
}
EXEC SQL prepare sel_id from :comanda;
EXEC SQL declare sel_curs cursor for sel_id;
EXEC SQL describe sel_id into da_ptr;
if (!init_sqlda(da_ptr)) {
printf("Error: out of memory");
exit(0);
}
printf("Num columns: %d \\n", da_ptr->sqld);
for (i=0; i<da_ptr->sqld; i++) {
printf("Name Col%d: %s \\t", i, da_ptr->sqlvar[i].sqlname);
printf("Type Col%d: %d\\n", i, da_ptr->sqlvar[i].sqltype);
}
EXEC SQL free sel_id;
free (da_ptr);
EXEC SQL disconnect current;
}