C + + database development How to get the number of records in a table that satisfies a condition __ios

Source: Internet
Author: User

This morning search how to get the number of records that meet a condition in a table and assign an int variable to take a long time, try a variety of methods are useless, but also found that most of Baidu is to give a number of records to meet a condition of the SQL statement, but there is no one told us to execute the statement after the use, This is a key issue, after their own groping, finally found a relatively simple method, for everyone's reference.


Strcommand.format ("SELECT count (*) as Countnum from Hsample where id=%d", i_id);//This is the number of all records in the query id=i_id. And the quantity value as the value of the new field Countnum, the * number in the COUNT (*) bracket can also be changed to a specific field name,
//Because I have qualified the condition (ID number must be i_id), so here the * number or the specific field name results are the same
M_precordset=m_pconnection->execute (_bstr_t (strcommand), Null,adcmdtext)//EXECUTE query Statement
int Count=atoi (_com_ Util::convertbstrtostring (_bstr_t (M_precordset->getcollect ("Countnum")));//Then get the value of Countnum (string) First, Then use the Atoi function to convert the quantity value in the form of a string to an int and assign it to the count variable


Also, you need to add that to get a field value, which is a string, you can refer to the following code:

CString mv_idset=_com_util::convertbstrtostring ((_bstr_t) m_precordset->getcollect ("Mv_idset"));//Field here Mv_ IDSet is a String type

If a field value is an integer, the same number of fetch records as above, get the string form, and then the int type, for example, where the field ID is the integer type:

int Id=atoi (_com_util::convertbstrtostring (_bstr_t m_precordset->getcollect ("id")));

And so on, if you want to get the maximum value for a field, you can also use that code, for example:

Strcommand.format ("Select Max (Hflag) as Maxhflag from Hcollar where (Mv_idset like%s) or (Mv_idset like%s) or" (Mv_idset Like%s)) "," '%, ' +id+ ', ' ', ' ' ' +id+ ',% ', ' '% ', ' +id+ ',% ' '; First query the maximum value of the Hflag field in the Hcollar table, because the data exported here is the most recently modified tuple
m_ Precordset=m_pconnection->execute (_bstr_t (strcommand), null,adcmdtext);//execute the above statement
int Maxhflag=atoi (_com_ Util::convertbstrtostring ((_bstr_t) m_precordset->getcollect ("Maxhflag")); Gets the Maxhflag field value in the Hcollar table to assign to the integer variable Maxhflag


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.