/* mult_update.c: test multi-statement update * * This program demonstrates two things: * - If mysql_next_result() isn't called after a multi-statement query, there * will be a protocol error (feature request) * - But if sleep(1) is called between the two mysql_query() calls, then the * protocol error goes away (bug - why does this happen?) * * Create a table in the 'test' database as follows: drop table if exists test.mult_update; create table test.mult_update (f1 int, f2 varchar(100), f3 varchar(100)); insert into test.mult_update (f1, f2, f3) values (0, "f2 0", "f3 0"), (1, "f2 1", "f3 1"); * Then run this program to test. */ /* If mysql_next_result() is called, the program works fine */ #define CALL_MYSQL_NEXT_RESULT 0 /* If sleep(1) is called, the program seems to work fine */ #define SLEEP_HACK 0 #include #include #include #include #include MYSQL *dbh; MYSQL_RES *results; MYSQL_ROW record; void _die (int line, const char *msg) { fprintf(stderr, "Program terminated at line %d: %s\n", line, msg); exit(EXIT_FAILURE); } #define die(msg) _die(__LINE__, (msg)) int main(void) { int i; int rc; /* Connect to server */ dbh = mysql_init(NULL); if (!dbh) die("mysql_init() failed"); mysql_options(dbh, MYSQL_READ_DEFAULT_GROUP, "client"); if (mysql_real_connect(dbh, NULL, NULL, NULL, "test", 0, NULL, CLIENT_MULTI_STATEMENTS) == NULL) die(mysql_error(dbh)); /* Do multi-statement updates, in a loop */ for (i = 0; i < 2; ++i) { char buf[300]; snprintf(buf, sizeof(buf), "update mult_update set f2 = 'test f2 %d' where f1 = '%d'; update mult_update set f3 = 'test f3 %d' where f1 = %d", i, i, i, i); printf("iter %d: buf: '%s'\n", i, buf); if (mysql_query(dbh, buf) != 0) die(mysql_error(dbh)); do { printf("\t- A statement affected %llu row(s)\n", mysql_affected_rows(dbh)); #if SLEEP_HACK /* For some reason, this makes things "work" ok */ fprintf(stderr, "Sleeping for 1 second...\n"); sleep(1); #endif } #if CALL_MYSQL_NEXT_RESULT while ((rc = mysql_next_result(dbh)) == 0); if (rc > 0) die(mysql_error(dbh)); #else while (0); #endif } mysql_close(dbh); exit(EXIT_SUCCESS); }