好吧,我想知道是否可以在printf中使用计数计数的结果。
我有一个代码在mysql中进行计数。
void count_sql(void) {
if (SQL_ENTER == SQL_ROWS(mysql, "SELECT COUNT(*) FROM `login_client` WHERE `ID` < DATE(NOW() + INTERVAL 1 DAY)"))
sqldebug(mysql);
printf("Counted users\n");
return;
}
我想通过留下如下信息来返回计数值,例如:
printf(&#34;找到150个用户\ n&#34;);
答案 0 :(得分:2)
我无法找到与SQL_ENTER
类似的数据类型,所以我不确定这是否是您的代码中的变量,这是我正在查看的文档
26.8.7 C API Function Descriptions
下面我添加了int main
,但您应该只使用void count_sql
,同样值得注意的是我将MYSQL
对象传递给了您的问题,这可能也是您问题的一部分。< / p>
还值得注意的是我使用MinGW编译器和mysql-connector-c-6.1.9-win32库在Windows环境中构建并测试了此代码,因此它可能无法在您的环境中运行
#include<winsock2.h>
#include<stdio.h>
#include<mysql.h>
int main(){
/* initialize MYSQL connection */
MYSQL *mysql = mysql_init(NULL);
if(mysql == NULL){
sqldebug(mysql);
}
/* pass credentials here */
if(mysql_real_connect(mysql, "localhost", "user", "password", "database", 0, NULL, 0) == NULL){
sqldebug(mysql);
}
/* run query and count results */
count_sql(mysql);
/*close connection and exit void*/
mysql_close(mysql);
exit(0);
}
void sqldebug(MYSQL *mysql)
{
fprintf(stderr, "%s\n", mysql_error(mysql));
mysql_close(mysql);
exit(1);
}
void count_sql(MYSQL *mysql){
/* run query */
if(mysql_query(mysql, "SELECT COUNT(*) FROM `login_client` WHERE `ID` < DATE(NOW() + INTERVAL 1 DAY)")){
sqldebug(mysql);
}
/* store the results */
MYSQL_RES *result = mysql_store_result(mysql);
if(result == NULL){
finish_with_error(mysql);
}
/* check the result has records ** don't really need this part */
int num_fields = mysql_num_fields(result);
if(num_fields<1){
printf("no fields returned");
exit(1);
}
/* row record */
MYSQL_ROW row;
if(row=mysql_fetch_row(result)){
printf("Counted users %s\n", row[0]);
}
return;
}