Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a console app and I need to print the result of this query to screen.
How is this done?

"SELECT COUNT(IP) AS row_count FROM urlock WHERE IP='173.201.216.2'";
Posted

Use printf() or cout to display your results formatted according to your requirements.
 
Share this answer
 
Comments
Member 7766180 9-May-11 15:57pm    
Tried this, it displayed to query...
int printf("SELECT COUNT(IP) AS row_count FROM urlock WHERE IP='173.201.216.2'");
Richard MacCutchan 10-May-11 3:50am    
I think it's time to go back to your programming guides.
Member 7766180 10-May-11 11:34am    
In the meantime, how do you get a returned value?
Richard MacCutchan 10-May-11 12:23pm    
I don't really know SQL but "SELECT COUNT" looks like it returns the count of items that you are querying. Try a query that gets the value instead.
Member 7766180 10-May-11 12:37pm    
I think I need a variable before the query, but whatI tried doesn't debug. I think it's the syntax...

int RED;
RED = SELECT COUNT(*) AS row_count FROM tblURL WHERE IP = '192.168.1.1';
cout << "RED"
If you want to show the result of query than first you get this value in string and than with the help of printf and cout you can display your results.
Can you please share your database part with us ??
 
Share this answer
 
Comments
Member 7766180 10-May-11 11:28am    
Database is one table, one table only. I'm querying it to see if an IP address is in it. If it is then I get a "1" returned else it returns a "0".
Thanks
DS
Here's an explanation on printf() and how to use it:
http://www.cplusplus.com/reference/clibrary/cstdio/printf/[^]
 
Share this answer
 
Comments
Member 7766180 10-May-11 11:26am    
Thanks Albert, the printing part I have, Its the getting the returned value that seems to be my problem. How do I get that?
Thanks
DS
Albert Holguin 10-May-11 11:36am    
The return from the query? ...that query is not C++... you would have to put up more code, you may be trying to mix different languages.
Member 7766180 10-May-11 11:47am    
It's MySQL, I have an online MySQL database that I am connecting to. I can make the connection, but I need to save the answer to a variable, then print to screen.
DS
Member 7766180 10-May-11 12:02pm    
I thought maybe this...
int RED;
RED = SELECT COUNT(*) AS row_count FROM tblURL WHERE IP = '192.168.1.1';
cout << "RED"

Doesn't return or display anything.
Albert Holguin 10-May-11 13:22pm    
that's because that statement is completely invalid, like I said, its not even C++... how are you passing that query to your SQL server (or database)? there has to be other code to interface the two, or use a library like SQLAPI++ (http://www.sqlapi.com/)
First of all you should get a "connection" to the database... something you could use as an interface... once you've got that interface, then you will be able to query SQL strings like the one you've written here...

Then, typically you'll have to store the result of your SQL statement into a variable or so and then you'll be able to show it to the user by using any of the methods to show data... printf, cout...

if you use printf("SELECT COUNT(IP) AS row_count FROM urlock WHERE IP='173.201.216.2'"); of course this will show the text itself...

printf or cout only are useful to show information. Before showing the information you need to get it...

Give us more details on the database part and we will help you...

Good luck!
 
Share this answer
 
Comments
Member 7766180 10-May-11 11:33am    
The connection I have. The query I have. The printf part I understand. The problem seems to be, how to get the returned value.
Thanks
DS

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