Click here to Skip to main content
15,904,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to change the type defination in SQLPrepare(). It's the query4 part. It's defined as a SQLCHAR and I need it to be an SQLINTEGER. How can I do this?

C++
QUOTA = SQLPrepare(hStmt4, query4, SQL_NTS);


I tried this.
C++
QUOTA = SQLPrepare(hStmt4, reinterpret_cast<sqlinteger>(query4), SQL_NTS);</sqlinteger>
Posted

You cannot cast a variable from one type to another if the two types are incompatible! You didn' understand the comments in your other posting, did you?

Again: Do not use type casts! If you have incompatible types (such as a string type and an integral type), you must use special conversion functions! If you have types that should be compatible but still issue compiler warnings, then in 101 out of 100 cases the type of one or both variables hasn't been chosen appropriately - in that case, fix the variable types!

There are very, very, very, very, very, very few occasions where type casts are actually required, and they involve either some obscure low-level stuff best left to the gurus, or interaction with some badly designed interfaces from the stone age (such as the MFC). If neither is the case for you, assume that any perceived neccessity for a type cast is a misperception, and in truth your code needs reworking!

I don't know what the types SQLINTEGER and SQLCHAR are defined to be, but if they are compatible to int and char respectively, then a simple call to the function atoi(const char*) should work just fine.
 
Share this answer
 
Comments
Member 7766180 20-Oct-11 12:19pm    
Thank you Stefan. The problem I am having with atoi is that SQLCHAR is not compatable with const char. This is what is messing me up on the conversion. SQLCHAR is an unsigned char and SQLINTEGER is an int, you are correct.
Member 7766180 20-Oct-11 12:43pm    
int var = atoi(chval1);
if (rowCount3 >= var)
returns the not compatible error.......
Chuck O'Toole 20-Oct-11 20:52pm    
And this is the ONE place where typecasting is useful, it's for telling the compiler to SHUT UP.

int var = atoi((const char *)chval1);

Doing typecasting everywhere is BAD and only shows that you are desparately trying to wedge things to work. It will rarely work.
Mehdi Gholam 20-Oct-11 12:47pm    
My 5!
Chuck O'Toole 20-Oct-11 20:49pm    
Stefan, this is all my fault. I showed him typecasting in this note : http://www.codeproject.com/Questions/269947/Not-Getting-a-Number-Returned : and now he's typecasting everything in sight. I showed him the Big Hammer (tm) to get the compiler to shut up about the difference between 'unsigned char *' and 'char *'. I forgot to tell him that typecasting is mostly just for telling the compiler to "SHUT UP, I KNOW WHAT I'M DOING". But in the wrong hands.......
There really was a simple solution to this.....

C++
SQLHDBC     hDbc4 = NULL;
SQLHSTMT    hStmt4 = NULL;
long        lIPQuota;
SQLINTEGER  ret1;


C++
QUOTA = SQLBindCol(hStmt4, 1, SQL_C_LONG, (SQLPOINTER) &lIPQuota, sizeof(long),&ret1);


All is good.
 
Share this answer
 
Comments
Stefan_Lang 21-Oct-11 8:06am    
It's good to see it finally works for you. However, this would have been a lot easier if you hadn't spread your information over multiple questions! Please, the next time you ask a question, put all the relevant information in one place only! If I had seen this posting it would have saved me a lot of time in interpreting and posting an answer to the information you posted elsewhere!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900