Solved

dbdata float problem

Posted on 2004-10-10
11
285 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
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

816 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

9 Experts available now in Live!

Get 1:1 Help Now