int mysql_query(MYSQL *connection, const char *query)
my_ulonglong mysql_affected_rows(MYSQL *connection);
#include
#include
#include "mysql.h"
int main(int argc, char *argv[]) {
MYSQL my_connection;
int res;
mysql_init(&my_connection);
if (mysql_real_connect(&my_connection, "localhost",
"rick", "bar", "rick", 0, NULL, 0)) {
printf("Connection success\n");
res = mysql_query(&my_connection,
"INSERT INTO children(fname,age),
VALUES('Ann',3)");
if (!res) {
printf("Inserted %lu rows\n",
(unsigned long)mysql_affected_rows(&my_connection));
} else {
fprintf(stderr, "Insert error %d: s\n",mysql_errno ,
(&my_connection),
mysql_error(&my_connection));
}
mysql_close(&my_connection);
} else {
fprintf(stderr, "Connection failed\n");
if (mysql_errno(&my_connection)) {
fprintf(stderr, "Connection error %d: %s\n",
mysql_errno(&my_connection),
mysql_error(&my_connection));
}
}
return EXIT_SUCCESS;
}
mysql_errno(&my_connection), mysql_error(&my_connection));
}
}
res = mysql_query(&my_connection, "UPDATE children SET AGE = 4
WHERE fname = 'Ann'");
if (!res) {
printf("Updated %lu rows\n",
(unsigned long)mysql_affected_rows(&my_connection));
} else {
fprintf(stderr, "Update error %d: %s\n",
mysql_errno(&my_connection),
mysql_error(&my_connection));
}
2
3
4
5
6
7
8
9
10
11
JennyAndrew
Gavin
Duncan
Emma
Alex
Adrian
Ann
Ann
Ann
Ann
1410
4
2
0
11
5
3
4
3
4
if (mysql_real_connect(&my_connection, "localhost",
"rick", "bar", "rick", 0, NULL, CLIENT_FOUND_ROWS)) {
如果我们在数据库中复位数据,然后运行带有这种修改的程序,则它报告的行数为4。
函数mysql_affected_rows还有最后一个奇怪之处,它发生在从数据库中删除数据时。如果使用WHERE子句,则mysql_affected_rows将按预期返回删除行数。但是,如果没有WHERE子句,则删除所有行,报告受影响的行数却为0。这是因为由于效率原因优化删除整个表。这种行为不受CLIENT_FOUND_ROWS选项标志的影响。
[1] [2] 下一页