Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an if statement with two SQL types that I am comparing with each other
The statement....

C++
if ((int*)rowCount3 >= (int*)chval1)


rowCount3 is an SQLINTEGER
chval1 is an SQLCHAR chval1[128]

I'm trying to force the cast so that I am comparing apples to apples. I imagine that they both have to be real comparable numbers. Any help appreciated, Thank you.
Posted
Comments
Member 7766180 20-Oct-11 0:39am    
Tried This and it didn't work.

if (rowCount3 >= (SQLINTEGER)chval1)
Member 7766180 20-Oct-11 0:41am    
And this...also did not work.

if (rowCount3 >= chval1[128])
Member 7766180 20-Oct-11 0:44am    
This is what I see when I do a printf

printf("Total of Downloads: %d\n",rowCount3);
printf("Quota of IPAddress: %s\n", chval1);

Total of Downloads: 122
Quota of IP Address: 116
Member 7766180 20-Oct-11 0:50am    
Also didn't work!

if ((SQLINTEGER)rowCount3 >= (SQLINTEGER)chval1)
Member 7766180 20-Oct-11 0:54am    
This seems to work...correct me if I'm wrong, please!

if ((SQLCHAR)rowCount3 >= (SQLCHAR)chval1)

The Big Hammer (tm) does not solve every problem. You cannot typecast your way to making things work. Typecasting only works if the datatypes are already compatible. SQLINTEGER and SQLCHAR are incompatible.

That said, if you have a string of arbitrary characters (SQLCHAR) that are stored in a database and you, in your mind, know that these characters are just numbers, you can apply the "_atoi()" function on the string to convert the characters found to their numeric interpretation.

Then you will have two compatible types and you can compare them mathematically.

On a side note, it seems that the database record is itself badly designed. Your comments above seem to say that the SQLCHAR field is the quota of addresses, a numeric value yet the field is defined as a character string instead of a simple intger (SQLINTEGER). While another field is the number of downloads and is stored properly as an integer (SQLINTEGER).

The database is capable of storing binary values (SQLINTEGER) so that should be the preferred way of storing these numbers. That way, you do not have to do "Character to Integer" (_atoi())conversions when reading the field nor "Integer to Character" (sprintf() / CString.Format()) conversions when updating / writing the field.
 
Share this answer
 
Comments
Stefan_Lang 20-Oct-11 5:10am    
My 5 for 'The Big Hammer (tm)' :D (and the rest of course ;)
Member 7766180 20-Oct-11 11:16am    
Thank you Chuck. Good explanation. I tried this and I'm sure in fact I know I have it wrong. What else should I add?

int i;
char chval1 [128];
i = atoi (chval1);
if (rowCount3 >= i)
Stefan_Lang 20-Oct-11 12:11pm    
Apart from the fact that in this code snippet your variable chval1 is not initialized, and therefore the conversion will not provide any meaningful value, it should work.

P.S.: how big can your table be? 128 chars seems an awful lot to represent the number of rows in a table!
Member 7766180 20-Oct-11 12:23pm    
This is way I can't initialize it....SQLCHAR chval1[128]; It seems that I need this to get the value to begin with. I tried changing the SQL statement to SQLINTEGER so that I don't have to convert but the SQLPrepare statemet keeps calling for an SQLCHAR, that is why I was asking about te typecast.
Member 7766180 20-Oct-11 12:26pm    
SQLCHAR* query4 = (SQLCHAR*)"SELECT tblIP.[IPQuota] FROM tblIP WHERE tblIP.[IPAddress] ='173.201.216.2';";

SQLSMALLINT iConnStrLength2Ptr;
SQLCHAR szConnStrOut[255];
SQLCHAR chval1[128];

QUOTA = SQLAllocEnv(&hEnv4);
QUOTA = SQLAllocConnect(hEnv4, &hDbc4);
QUOTA = SQLDriverConnect(hDbc4, NULL, szDSN, _countof(szDSN), szConnStrOut, 255, &iConnStrLength2Ptr, SQL_DRIVER_NOPROMPT);
QUOTA = SQLAllocStmt(hDbc4,&hStmt4);
QUOTA = SQLPrepare(hStmt4, query4, SQL_NTS);
QUOTA = SQLBindCol(hStmt4, 1, SQL_C_CHAR, chval1, 128, &ret1);
QUOTA = SQLExecute(hStmt4);
QUOTA = SQLFetch(hStmt4);
QUOTA = SQLFreeStmt(hStmt4, SQL_DROP);
printf("Quota of IP Address: %d\n", chval1);
SQLDisconnect(hDbc4);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc4);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv4);
use strcmp function to compare

My answer is completely wrong.

if op expect chvall is holding numeric data then op can use atoi function to convert char array of number to integer.
 
Share this answer
 
v2
Comments
Member 7766180 20-Oct-11 1:12am    
Ok made a mess of things! I think I need further guidance!

int strcmp(int * rowCount3 >= int * chval1)
Stefan_Lang 20-Oct-11 5:11am    
Seems like you fell in the same trap as the original poster. See Solution 2.
Mohibur Rashid 20-Oct-11 5:26am    
I didn't fall any trap like OP,

I fell in other trap, I didn't read the whole problem. besides I didn't expect someone to compare sting and integer who is working on database related application with c/c++
Stefan_Lang 20-Oct-11 9:05am    
Hehe, I can totally understand that. Sometimes people have an... unexpected ... understanding of programming ;)

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