#include <stdio.h>
#include <strings.h>
#include <mysql.h>
#include <assert.h>
#include <stdlib.h>

int main()
{
    MYSQL *dbh;
    MYSQL_RES *query_result;
    MYSQL_ROW row;
    int have_next;

    /* INIT */
    dbh = mysql_init(NULL);
    if (!mysql_real_connect
	(dbh, NULL, "root", NULL, "test", 3306, NULL, 0)) {
	printf("Cannot connect %s", mysql_error(dbh));
    };
    mysql_set_server_option(dbh, MYSQL_OPTION_MULTI_STATEMENTS_ON);
    assert(mysql_query(dbh, "select version()") == 0);
    query_result = mysql_store_result(dbh);
    assert((row = mysql_fetch_row(query_result)) != NULL);
    printf("mysql version is: %s\n", row[0]);
    mysql_free_result(query_result);

    if (mysql_query(dbh, "drop table if exists `b15943`; \
                    drop view if exists v15943; \
		    CREATE TABLE `b15943` (  `id`  int(11) not null \
		    auto_increment, primary key(`id`) ) ENGINE = MyISAM DEFAULT CHARSET = utf8; \
		    create view v15943  as select * from b15943; \
		    drop table b15943; \
		    show create view v15943") != 0) {
	printf("%s\n", mysql_error(dbh));
	exit(1);
    };

    do {
	puts("Handling..");
	query_result = mysql_store_result(dbh);
	if (query_result != NULL) {
	    while ((row = mysql_fetch_row(query_result)) != NULL)
		printf("%s: %s \n", row[0],row[1]);
	    mysql_free_result(query_result);
	};
	if ((have_next = mysql_next_result(dbh)) > 0) {
	    printf("%s\n", mysql_error(dbh));
	    exit(1);
	};
    }
    while (have_next == 0);
    mysql_close(dbh);
}
