Bug #46290 SELECT COUNT(*) FROM ... does'nt work with libmysql client library
Submitted: 18 Jul 2009 22:24 Modified: 10 Feb 2018 17:44
Reporter: Roberto Farioli Email Updates:
Status: Can't repeat Impact on me:
None 
Category:MySQL Server: DML Severity:S2 (Serious)
Version:MySQL 5.1.34 comunitiy edition OS:Windows
Assigned to: CPU Architecture:Any
Tags: count(*), libmysql

[18 Jul 2009 22:24] Roberto Farioli
Description:
I have a Windows C++ program using libmysql.dll with libmysql.lib importing library. When I run this query "SELECT COUNT(*) FROM 'table'",  the return isn't the correct rows number. I'm using MySql OleDB Provider 3.9.6.

How to repeat:
	long lCount		= 0;	
			
	CString csSqlSelect(_T("SELECT COUNT(*) FROM TableName"));

	CCommand<CManualAccessor> rs;
	MYBIND * pBind = NULL;
	pBind = new MYBIND [1];
	rs.CreateAccessor (1, pBind, sizeof (MYBIND));				
	rs.AddBindEntry (1,
					 DBTYPE_R8,
					 sizeof (double),
					 &pBind [0].dblDate,
					 NULL,
					 &pBind [0].dwStatus);
	HRESULT hr = rs.Open (*pSession, csSqlSelect);
	if (hr == S_OK)
	{

		CRowset * pRS = (CRowset *) &rs;
		if (pRS->MoveFirst () == S_OK)
		{
			if (pBind [0].dwStatus == DBSTATUS_S_ISNULL)
				pBind [0].dblDate = 0;							

			lCount = pBind [0].dblDate;								
		}				
	}

	delete [] pBind;

	return lCount;
[19 Jul 2009 7:05] Valeriy Kravchuk
Thank you for the problem report. Please, send the results of:

select coun(*) from tablename;
show create table tablename\G
show table status like 'tablename'\G

from mysql command line client. Send also the result you get in your application using OleDB.
[19 Jul 2009 13:42] Roberto Farioli
SELECT COUNT(*) FROM `test`.`datalogger`

Returns 5310

show create table `test`.`datalogger`

Returns

"Table","Create Table datalogger"

"CREATE TABLE `datalogger` (
  `DataOra` datetime NOT NULL,
  `increment.R4` double DEFAULT '0',
  `increment.R8` double DEFAULT '0',
  `increment.UI1` double DEFAULT '0',
  `increment.UI2` double DEFAULT '0',
  `increment.UI4` double DEFAULT '0',
  PRIMARY KEY (`DataOra`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1"

show table status

Returns

"Name","Engine","Version","Row_format","Rows","Avg_row_length","Data_length","Max_data_length","Index_length","Data_free","Auto_increment","Create_time","Update_time","Check_time","Collation","Checksum","Create_options","Comment"
"datalogger","InnoDB",10,"Compact",5626,75,425984,0,0,8388608,,"2009-07-19 00:44:16","","","latin1_swedish_ci",,"",""
"datalogger1","InnoDB",10,"Compact",5272,80,425984,0,0,8388608,,"2009-07-19 00:44:17","","","latin1_swedish_ci",,"",""
"datalogger2","InnoDB",10,"Compact",5088,83,425984,0,0,8388608,,"2009-07-19 00:44:17","","","latin1_swedish_ci",,"",""
"datalogger3","InnoDB",10,"Compact",5626,75,425984,0,0,8388608,,"2009-07-19 00:44:18","","","latin1_swedish_ci",,"",""
"datalogger4","InnoDB",10,"Compact",5272,80,425984,0,0,8388608,,"2009-07-19 00:44:18","","","latin1_swedish_ci",,"",""
"datalogger5","InnoDB",10,"Compact",5091,83,425984,0,0,8388608,,"2009-07-19 00:44:19","","","latin1_swedish_ci",,"",""
"datalogger6","InnoDB",10,"Compact",5626,75,425984,0,0,8388608,,"2009-07-19 00:44:19","","","latin1_swedish_ci",,"",""
"datalogger7","InnoDB",10,"Compact",5272,80,425984,0,0,8388608,,"2009-07-19 00:44:20","","","latin1_swedish_ci",,"",""
"datalogger8","InnoDB",10,"Compact",5094,83,425984,0,0,8388608,,"2009-07-19 00:44:20","","","latin1_swedish_ci",,"",""
"datalogger9","InnoDB",10,"Compact",5626,75,425984,0,0,8388608,,"2009-07-19 00:44:21","","","latin1_swedish_ci",,"",""

My variable pBind [0].dblDate is not update with the Count(*) result, others DB work fine with same code.
[19 Jul 2009 15:12] Valeriy Kravchuk
In your code I see this:

CString csSqlSelect(_T("SELECT COUNT(*) FROM TableName"));

Now, what is TableName in the above? It is your function's parameter? How do you pass real table name there? I also see this:

	HRESULT hr = rs.Open (*pSession, csSqlSelect);
	if (hr == S_OK)
	{
...

So, your variable will not be updated if hs is not equal to S_OK, that is, in case of any error. Maybe you should print out hr value and/or check for possible error to find the real reason of the problem...
[19 Jul 2009 15:38] Roberto Farioli
My code for 'DataLogger' table is
CString csSqlSelect(_T("SELECT COUNT(*) FROM Datalogger"));

HRESULT hr = rs.Open (*pSession, csSqlSelect);
if (hr == S_OK)
{

Command is opened OK, so hr is S_OK, 

  if (pRS->MoveFirst () == S_OK)

Rowset MoveFirst is S_OK

I searched in libmysql sources (debugged with my project) and I think there is an error in what libmysql send to Mysql Service (TCp/IP) when the request is COUNT(*)
[20 Jul 2009 7:04] Tonci Grgin
Hi Roberto.

I truly don't know what "MySql OleDB Provider 3.9.6." and I doubt we made it... As for return result of *any* function in MySQL, it is output as sequence of bytes (binary) and I guess that's where your code breaks. Please try reading the result with "GetBytes" or some similar function at your disposal and inform us of result.
[20 Aug 2009 23:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
[10 Feb 2018 17:44] Roy Lyseng
Posted by developer:
 
Closing since it has been suspended for more than 8 years.