I am attempting to convert a string to unsigned char. The Method I use appears to work but the function call fails.
I am using MSVC++ Express 2.0.50727 on Win XP with an AMD Processor. Forgive me if the following is verbose, however, I am ctumped as to what little element is hindering me so i'll try to be as clear as I can.
The aim of my code is to call an SQL database (MS SQL Server 2005) using ODBC and all works well up until now. -
//inside a sqlext.h file there is a typedef of UCHAR
-
typedef UCHAR unsigned char
-
-
//in my main() function
-
UCHAR SQLstr[128] = "execute stored_procedure_FOOBAR 3, 'spam' "
-
retcode = SQLPrepare (hStmt, SQLstr, sizeof SQLstr);
-
if(retcode == something_bad){
-
cout << "sqlprepare error \n";
-
return 9;
-
}
-
retcode = SQLNumResultCols(hStmt, &cols);
-
if(retcode == something_bad){
-
cout<<"SQLNUmResultCols error \n";
-
return 9
-
}
-
(Here hStmt is a handle to the sql statement) However, I now need to concatenate the different sql statement variables together and turn them into UCHAR. To demonstrate this, suppose I have made a string of my SQL statement: stringySQL, exactly the same as the above UCHAR (which works btw) but now a string... -
string stringySQL = "execute stored_procedure_FOOBAR 3, 'spam' ";
-
char temp[128] ;
-
strcpy(temp, stringySQL.c_str());
-
UCHAR SQLstr[128];
-
strcpy(reinterpret_cast<char*>(SQLstr), temp);
-
This code complies and runs but gives the error "SQLNUmResultCols error". I even tried the following: -
UCHAR SQLtemp[128] ="execute stored_procedure_FOOBAR 3, 'spam' ";
-
//if I now use this it works...but
-
UCHAR SQLstr[128];
-
for(i=0;i<128;i++){
-
SQLstr[i] = SQLtemp[i]
-
}
-
//if I now use SQLstr it doesn't!
-
If anyone can offer advice it would be greatly appreciated. Rich.
7 7843
One question is why the UCHAR? C-strings are CHAR.
Actually, since this is Windows, it should be TCHAR to support ASCSII/UNICODE mappings. -
string stringySQL = "execute stored_procedure_FOOBAR 3, 'spam' ";
-
char temp[128] ;
-
strcpy(temp, stringySQL.c_str());
-
TCHAR SQLstr[128];
-
strcpy(SQLstr, temp);
-
cout << SQLstr << endl;
-
One question is why the UCHAR? C-strings are CHAR.
Actually, since this is Windows, it should be TCHAR to support ASCSII/UNICODE mappings.
The function SQLPrepare takes UCHAR* as its input argument and did not work when given TCHAR. (even as a whole string, even using reinterpret_cast(UCHAR*)
On the bright side: I have just solved the problem to some, practical, extent. However I am intrigued as to why my solution works, If you want to tell me I would be happy to hear why it works. -
string stringySQL = "execute stored_procedure_FOOBAR 3, 'spam'";
-
char temp[128] ;
-
strcpy(temp, stringySQL.c_str());
-
UCHAR SQLstr[128] = "______any_length <= length(stringySQL)______";
-
strcpy(reinterpret_cast<char*>(SQLstr), temp);
-
Again, many thanks,
Rich
Is this your SQLPrepare()??
SQLRETURN SQLPrepare(
SQLHSTMT StatementHandle,
SQLCHAR * StatementText,
SQLINTEGER TextLength);
Maybe you could post the function prototype of your SQLPrepare becuse this one has no UCHAR arguiments.
Banfa 9,065
Expert Mod 8TB
Is this your SQLPrepare()??
Maybe you could post the function prototype of your SQLPrepare becuse this one has no UCHAR arguiments.
SQLCHAR is a unsigned type (which a suspect UCHAR is too from it's name)
Excert from SQLTYPES.H -
typedef unsigned char SQLCHAR;
-
However richardpeter the point I think weaknessforcats is trying to make is a valid one which is if you are going to call a function, since you are taking the trouble to change the datatype it is probably worth taking to trouble to change the datatype to the one the function is expecting.
SQLCHAR is a unsigned type (which a suspect UCHAR is too from it's name)
Excert from SQLTYPES.H -
typedef unsigned char SQLCHAR;
-
However richardpeter the point I think weaknessforcats is trying to make is a valid one which is if you are going to call a function, since you are taking the trouble to change the datatype it is probably worth taking to trouble to change the datatype to the one the function is expecting.
My apologies for the confusion, you are both correct: SQLCHAR is UCHAR which is unsigned char. like Banfa mentions, they both have typedefs located in the headder file sqext.h.
What weaknessforcats said is correct too, however in my program I have to concatenate strings to make my SQL statement and then convert these strings to SQLCHAR or equivalently UCHAR or equivalently unsigned char (they all work).
The code I posted above to do this does work, but I am now more intrigued as to why I have to initialise my UCHAR/SQLCHAR string to something of equal or lesser length than my desired SQL statement?
Many thanks
Rich
Banfa 9,065
Expert Mod 8TB
Sounds strange, I would suggest that you use the debugger to examine the exact contents of the sting you pass to SQLPrepare in both the working and non-working case.
Going back to Post #1:
//inside a sqlext.h file there is a typedef of UCHAR
typedef UCHAR unsigned char
//in my main() function
UCHAR SQLstr[128] = "execute stored_procedure_FOOBAR 3, 'spam' "
The typedef is backwards. It defines UCHAR as an unsigned char but does not define an unsigned char as UCHAR.
I changed the typedef below and looked at the array in the debugger and it is properly initialized.
There should be no need to use a string or a strcpy. -
typedef unsigned char UCHAR;
-
int main()
-
{
-
//in my main() function
-
UCHAR SQLstr[128] = "execute stored_procedure_FOOBAR 3, 'spam' ";
-
}
-
However, this code:
retcode = SQLPrepare (hStmt, SQLstr, sizeof SQLstr);
may have a bug in it. sizeof SQLStr is 128 which is the size of the buffer. The SQLPrepare() for ODBC requires the third argument to be the number of characters in the query , in this case 42, and not the size of the buffer.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jason Heyes |
last post by:
Is this the right idea?
char *ptr;
unsigned char *uptr = reinterpret_cast<unsigned char *>(ptr);
What about when I start with a char only?
char ch;
unsigned char *uptr =...
|
by: babak |
last post by:
Hi everyone
I want to format a string (using sprintf) and put it in a messagebox
but however I try to do it, it doesn't seem to work.
Here is an example sample of what i try to do:
char msg;...
|
by: ruffiano |
last post by:
Does the string class have a method for retrieving the address of a
string?
Thanks.
|
by: =?gb2312?B?wNbA1rTzzOzKpg==?= |
last post by:
i wrote:
-----------------------------------------------------------------------
----------------------------------------
unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
|
by: Zhang Liming |
last post by:
unsigned char rcd;
rcd contains 10 chars, such as '1', '2'.... etc.
string str;
the problem is how to pass the contents in rcd to str with minimal cost?
|
by: moumita |
last post by:
Hi All,
I need to convert 4 bytes to an unsigned long.
Suppose I have one array like unsigned char buf.I need to convert
these 4 bytes into a single
unsigned long. Is the following piece of code...
|
by: krishna81m |
last post by:
I am a newbie and have been trying to understand conversion from double to int and then back to int using the following code was posted on the c++ google group. Could someone help me out with...
|
by: john |
last post by:
Hi, at first the code doesn't seem to work. Any ideas?:
#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
|
by: Alex Vinokur |
last post by:
Hi,
Is it possible to do C++-casting from
const pair<const unsigned char*, size_t>*
to
const pair<unsigned char*, size_t>*
?
Alex Vinokur
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |