?
Solved

dbdata float problem

Posted on 2004-10-10
11
Medium Priority
?
290 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
Technology Partners: 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.
Suggested Courses

801 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