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选项标志的影响。
新闻热点
疑难解答