Click here to Skip to main content
16,004,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can't seem to get this query to count the number of instances from the database. I think my syntax is wrong.

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
#include <windows.h>
#include <winsock.h>
#include <c:\\mysql\\include\\mysql.h>

using namespace std;

int main()
   {
   MYSQL* conn;
   
   conn = mysql_init(NULL);
     if (mysql_real_connect(conn,"urlock.db.5513143.hostedresource.com","VendorCheck","Reader1234","urlock",0,NULL,0) !=0)
     {
		cout << "Connected!" <<endl;
		query << "SELECT COUNT(173.201.216.2) AS row_count FROM urlock";
        StoreQueryResult bres = query.store();
        cout << "Total rows: " << bres[0]["row_count"];

	 }
	 else
	 {
        cout << "Failed!" <<endl;
	 }
	 mysql_close(conn);
     system("PAUSE"); 
     return 0;
   }</winsock.h></windows.h></fstream></conio.h></string></iostream></stdio.h>
Posted

Perhaps, if you gave it a column reference, or '*', rather than an IP address, it might help...
SQL
SELECT COUNT(myColumn) AS row_count FROM urlock

Or are you trying to count how many rows there are with that IP address?
SQL
SELECT COUNT(myColumn) AS row_count FROM urlock WHERE myColumnWithIPAddress='173.201.216.2'
 
Share this answer
 
Comments
Member 7766180 9-May-11 15:07pm    
Yes the second one is exactly right! Except I'm getting a few errors...
Error 1 error C2065: 'SELECT' : undeclared identifier
Error 2 error C2146: syntax error : missing ';' before identifier 'COUNT'
OriginalGriff 9-May-11 15:11pm    
Perhaps if you put it inside the double quotes... :laugh:
CString sSql;
sSql.Format("SELECT COUNT(*) as mycount FROM Series WHERE Name = 'jim'") ;

recordset.Open(AFX_DAO_USE_DEFAULT_TYPE, sSql, NULL);

long count = recordset.GetFieldValue("mycount").lVal;


Now the variable count stores how many matches were made with the criteria you supply as an integer(how many records have jim in the field "name").


The above query works perfectly fine with MS Access. I am not sure what database you have. I hope the above example helps you to move forward in your program. You can use this code and improvise it to work with your database and code.

I found the following query while i was searching on google. Hope this helps you: -

SQL / MySQL » Select Clause » Count	 	
	
COUNT command with condition

/*
mysql>  SELECT species, sex, COUNT(*) FROM Bird
    ->         WHERE species = 'dog' OR species = 'cat'
    ->         GROUP BY species, sex;
Empty set (0.00 sec)


*/  
Drop table Bird;

CREATE TABLE Bird (
    name VARCHAR(20), 
    owner VARCHAR(20),
    species VARCHAR(20), 
    sex CHAR(1), 
    birth DATE, 
    death DATE
);
  
INSERT INTO  Bird VALUES ('BlueBird','Joe','Car','f','1999-03-30',NULL);
INSERT INTO  Bird VALUES ('RedBird','Yin','Bus','m','1979-04-30',1998-01-30);
INSERT INTO  Bird VALUES ('RedBird','Yin','Bus','m','1998-01-30',NULL);

/* You need not retrieve an entire table when you use COUNT(). 
For example, the previous query, when performed just on dogs and 
cats, looks like this:
*/
 SELECT species, sex, COUNT(*) FROM Bird
        WHERE species = 'dog' OR species = 'cat'
        GROUP BY species, sex;


This code should really help you. If you don't need the group by just remove it and instead of having two conditions, just keep on condition and I am sure this will help you in your code.

Let me know if any of the codes help you.

--
AJ
 
Share this answer
 
Comments
Member 7766180 9-May-11 22:31pm    
Thank you, unfortunately neither work. I love MS Access, that is what I am use to, this C++ is a killer compared to MS Access! I'm still working on this problem 10 hours later!!!!!
ankitjoshi24 10-May-11 10:32am    
The second one should work. I did try it myself. Well I hope I could have been of a help. Good luck with your work. And if you find a solution, reply to this so that even we can know what is the solution for your problem. And it will also help others facing similar problems.
Member 7766180 10-May-11 11:36am    
The query is good, but I need to get the returned value.
ankitjoshi24 10-May-11 11:47am    
you can use an Integer variable and assign the SELECT query to that integer. I haven't tried it but may be you can do something like: -

<pre>
long val;

val = SELECT ................... ;

cout<< val;

</pre>
ankitjoshi24 10-May-11 11:49am    
or may be you can just do the following: -

printf("%s\n",mysql_store_result("SELECT COUNT(IP) AS row_count FROM urlock WHERE IP='173.201.216.2'"));

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