[MySQL++] Get Total Records in Table?

I've found function "COUNT" but result is returning 1 even if I have 3 records there.. code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
	try
	{
		mysqlpp::Connection connection(DATABASE, HOST, USER, PASSWORD, PORT);
		string queryy;
		queryy += "SELECT COUNT(PC) FROM PCs";
		mysqlpp::Query query = connection.query(queryy.c_str());
		mysqlpp::StoreQueryResult result = query.store();
		MessageBox(0, test.c_str(), test.c_str(), NULL);
	}
	catch(mysqlpp::Exception e){
		return -1;
	}
}
¿How do you see the returned value?
Maybe, NULL elimination. (use count(*) instead)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
	try
	{
		mysqlpp::Connection connection(DATABASE, HOST, USER, PASSWORD, PORT);
		string queryy;
		queryy += "SELECT COUNT(*) FROM PCs";
		mysqlpp::Query query = connection.query(queryy.c_str());
		mysqlpp::StoreQueryResult result = query.store();
		string test;
		stringstream ss;
		ss << result[0]["PC"];
		ss >> test;
		MessageBox(0, test.c_str(), test.c_str(), NULL);
	}
	catch(mysqlpp::Exception e){
		return -1;
	}
}


Unknown field name: PC

edit: well nvm, just figured out that
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
	try
	{
		mysqlpp::Connection connection(DATABASE, HOST, USER, PASSWORD, PORT);
		string queryy;
		queryy += "SELECT PC FROM PCs";
		mysqlpp::Query query = connection.query(queryy.c_str());
		mysqlpp::StoreQueryResult result = query.store();
		string test;
		stringstream ss;
		ss << result.size();
		ss >> test;
		MessageBox(0, test.c_str(), test.c_str(), NULL);
	}
	catch(mysqlpp::Exception e){
		return -1;
	}
}


is working well!
Last edited on
count() will return just 1 number.*
The header will be the same as the consult "count(*)", but you may want to use an alias.
Althought your other method give you the correct answer*, is quite innefficient. You are loading the entire column just to know the number of rows.

* There is the clause group by that can be used with the aggregate functions. So you will get that function applied to every group, then [ttcount()[/tt] will give you as many results as groups.
1
2
select manufacturer,count(*) as quantity from PCs
  group by manufacturer;


* You are not eliminating the NULL values. And you are loosing power there.
Considerer select count(distinct manufacturer) from PCs
to be honest, I have no idea what u said O-o
Test your queries in an interpreter.
select count(*) from PCs; will give you as result
count(*)
----
3
So to access it you will need to do result[0]["count(*)"]; (actually I'm guessing, check your documentation).
But that is awful, so you can use an alias
select count(*) as quantity from PCs;
quantity
---
3
Now the header is quantity, so result[0]["quantity"];

With SELECT PC FROM PCs you will get a long list with the name of every PC. It's overkill.
ah, thanks ! :P I'll try it tomorrow
Topic archived. No new replies allowed.