Click here to Skip to main content
15,904,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a variable that is an IP address. It is saved as text in my Access 2010 database. I am trying to run this query with ipSrc and the query always fails. My guess is that its seeing ipSrc as ipSrc and not as the actual IP address. I tried it with 'ipSrc' and just plain ipSrc and both reurn fail. Also tried ""ipSrc"", failed as well. This failed to. '&ipSrc'. Here is the statement.

C++
SQLCHAR* query = (SQLCHAR*)"SELECT tblIP.[IPAddress], tblIP.[IPType], tblIP.[IPStatus], tblIP.[IPMax] FROM tblIP WHERE tblIP.[IPAddress]= ipSrc AND tblIP.[IPType]=3 AND tblIP.[IPStatus]=1 AND tblIP.[IPMax]=0;";


and here is the definition of ipSrc.
C++
translate_ip(ip_header->source_ip, ipSrc);


Using printf it prints out as an actual IP address.
C++
printf("\n   Source      IP: %s", ipSrc);
Posted
Updated 24-Oct-11 7:46am
v3

1 solution

[Updated]:
char buffer[1024] = {0,};
sprintf(buffer,"SELECT tblIP.[IPAddress], tblIP.[IPType], tblIP.[IPStatus], tblIP.[IPMax] FROM tblIP WHERE tblIP.[IPAddress]= %s AND tblIP.[IPType]=3 AND tblIP.[IPStatus]=1 AND tblIP.[IPMax]=0;",ipSrc);
SQLCHAR* query = (SQLCHAR*)buffer;


Use a single line for the sprintf call

Best regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Member 7766180 24-Oct-11 14:19pm    
Tried it and it doesn't work. I'm runnig ansi on all of this and it's been working fine. If I put an actual ip address in like this '173.201.2.2' ,the code will run as predicted. I don't think it's seeing the ip address. I think it's seeing just ipSrc. It works in Access with an actual IP address.
Chuck O'Toole 24-Oct-11 17:55pm    
The above solution is correct EXCEPT you need to wrap the %s with ' (single quote aka apostrophe) so it looks like '%s'. You really need to learn about formatting strings.
Member 7766180 24-Oct-11 19:08pm    
Yes I do Chuck. Thank you as well!
Espen Harlinn 25-Oct-11 4:01am    
A little glitch there on my part, so thanks for the note Chuck :)
Member 7766180 24-Oct-11 19:08pm    
Thank you Espen, works great!

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