Solved

dbdata float problem

Posted on 2004-10-10
11
289 Views
Last Modified: 2008-03-04
I'm attempting to assign a column to the FloatVar below via dbdata, but I keep getting an unspecified run-time error.  The dbdata assignment to CharVar works fine.  What could be causing this?

#define DBNTWIN32
#include "windows.h"
#include <sqlfront.h>
#include <sqldb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

PDBPROCESS dbproc;
PLOGINREC login;
RETCODE result_code;
DBFLT8 FloatVar;

char sql_stmt[256];
char CharVar[51];

void main() {
  void sqlconn();

  sqlconn();

  sprintf(sql_stmt,"SELECT FloatVar,CharVar FROM TestTable");
  dbcmd(dbproc,sql_stmt);
  dbsqlexec(dbproc);
  dbresults(dbproc);

  while (dbnextrow(dbproc) != NO_MORE_ROWS) {
    FloatVar = *((DBFLT8 *)dbdata(dbproc,1));
    strncpy(CharVar,(char *)dbdata(dbproc,2),(int)dbdatlen(dbproc,2));

    fprintf(stdout,"FloatVar: %s\tCharVar: %s\n",FloatVar,CharVar);
  }
}
0
Comment
Question by:arichexe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 17

Expert Comment

by:rstaveley
ID: 12272375
Would you expect DBFLT8 to be printable with format specifier %s ??
0
 

Author Comment

by:arichexe
ID: 12272527
I still get the run-time crash.
0
 

Author Comment

by:arichexe
ID: 12279591
I think the problem has to do with nulls, because when I change my select to isnull FloatVar to 0, it works.  How would I change dbdata to accommodate nulls in FloatVar?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:rstaveley
ID: 12279903
0
 

Author Comment

by:arichexe
ID: 12279933
I can't get dbbind to work, because of a linkage problem.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12280023
It is a C library I gather (must confess I've not used it). Is it possible that you are calling it from a C++ module and the header hasn't done extern "C"... and thus it has expected a C++ name-manged function...? Just a guess. Can you show me the linker error?
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12280180
VC7 and 7.1 doesn't have support for DB-Library (probably because they are tying to get you to use ADO, OLE DB, or ODBC to access SQL Server nowadays), but I notice that the headers in VC 6 do...

#ifdef __cplusplus
      extern "C" {
#endif

If you are using VC6 my guess isn't going to be right... but do let me know what the linker error is.
0
 

Author Comment

by:arichexe
ID: 12290389
I'm using VC6.  Here's the link error on dbbind:

Performing Custom Build Step on .\test.sc
ESQL test.sc:
Compiling...
test.cpp
c:\test\test.cpp(25) : error C2664: 'dbbind' : cannot convert parameter 5 from 'char *' to 'unsigned char *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12290431
That's a compiler error not a link error. Do the reinterpret_cast<unsigned char*> and you'll be in business.
0
 

Author Comment

by:arichexe
ID: 12291285
How would I code the "reinterpret_cast<unsigned char*>" in the below module?

PDBPROCESS dbproc;
PLOGINREC login;
RETCODE result_code;

char sql_stmt[256];
char testvar[51];

void main() {
  void sqlconn();

  sqlconn();

  sprintf(sql_stmt,"SELECT testvar FROM testtable");
  dbcmd(dbproc,sql_stmt);
  dbsqlexec(dbproc);

  while ((result_code = dbresults(dbproc)) != NO_MORE_RESULTS) {
    if (result_code == SUCCEED) {
       dbbind (dbproc,1,NTBSTRINGBIND,(DBINT) 0,(char *) testvar);

       while (dbnextrow(dbproc) != NO_MORE_ROWS) {
         fprintf(stdout,"testvar: %s\n",testvar);
       }
     }
  }
}
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 125 total points
ID: 12291369
Here's the C-cast way to do it:

  dbbind (dbproc,1,NTBSTRINGBIND,(DBINT) 0,(unsigned char *) testvar);

But it's more in the spirit of C++ to write

  dbbind (dbproc,1,NTBSTRINGBIND,(DBINT) 0,reinterpret_cast<unsigned char*>(testvar));

Either will work, but C-casting is less specific than C++'s casting.
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

705 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