Solved

dbdata float problem

Posted on 2004-10-10
11
281 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
  • 6
  • 5
11 Comments
 
LVL 17

Expert Comment

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

Author Comment

by:arichexe
Comment Utility
I still get the run-time crash.
0
 

Author Comment

by:arichexe
Comment Utility
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
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
0
 

Author Comment

by:arichexe
Comment Utility
I can't get dbbind to work, because of a linkage problem.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now