#include #include #include #include int main(void) { #define STRING_SIZE 50 #define SELECT_SAMPLE "SELECT a, concat(?,b) FROM test_table" MYSQL *mysql; MYSQL_STMT *stmt; MYSQL_BIND bind[2]; MYSQL_RES *prepare_meta_result; /* set default options */ static char *opt_db="mysql"; static char *opt_user="root"; static char *opt_password=0; static char *opt_host=0; static char *opt_unix_socket="/GORDON/MySQL/data_new/mysql.sock"; static unsigned int opt_port; unsigned long length[2]; int param_count, column_count, row_count; int int_data; char str_data[STRING_SIZE]; unsigned long str_length[2]; my_bool is_null[2]; fprintf(stdout, " Establishing a connection to the database ..."); /* Create our mysql structure */ if (!(mysql = mysql_init(NULL))) { fprintf(stderr, " mysql_init() failed\n"); exit(8); } /* We load any defaults from the my.cnf file for this application */ if (mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "manual")) { fprintf(stderr, " mysql_options() failed\n"); fprintf(stderr, " %s\n", mysql_error(mysql)); mysql_close(mysql); exit(8); } /* Make the actual connection to the database */ if (!(mysql_real_connect(mysql, opt_host, opt_user, opt_password, opt_db ? opt_db:"test", opt_port, opt_unix_socket, 0))) { fprintf(stderr, " connection failed\n"); fprintf(stderr, " %s\n", mysql_error(mysql)); mysql_close(mysql); exit(8); } fprintf(stdout, " OK\n"); fprintf(stdout, " DROP TABLE IF EXISTS test_table ..."); if (mysql_query(mysql, "DROP TABLE IF EXISTS test_table")) { fprintf(stderr, " failed\n"); fprintf(stderr, " %s\n", mysql_error(mysql)); exit(8); } fprintf(stdout, " OK\n"); fprintf(stdout, " CREATE TABLE test_table(a INT, b VARCHAR(30)) ..."); if (mysql_query(mysql, "CREATE TABLE test_table(a INT, b VARCHAR(30))")) { fprintf(stderr, " CREATE TABLE test_table failed\n"); fprintf(stderr, " %s\n", mysql_error(mysql)); exit(8); } fprintf(stdout, " OK\n"); fprintf(stdout, " INSERT INTO test_table VALUES(1,'ONE') ..."); if (mysql_query(mysql, "INSERT INTO test_table VALUES(1,'ONE')")) { fprintf(stderr, " INSERT INTO test_table failed\n"); fprintf(stderr, " %s\n", mysql_error(mysql)); exit(0); } fprintf(stdout, " OK\n"); fprintf(stdout, " INSERT INTO test_table VALUES(2,'TWO') ..."); if (mysql_query(mysql, "INSERT INTO test_table VALUES(2,'TWO')")) { fprintf(stderr, " INSERT INTO test_table failed\n"); fprintf(stderr, " %s\n", mysql_error(mysql)); exit(0); } fprintf(stdout, " OK\n"); /* Prepare a SELECT query to fetch data from test_table */ stmt = mysql_stmt_init(mysql); if (!stmt) { fprintf(stderr, " mysql_stmt_init(), out of memory\n"); exit(0); } if (mysql_stmt_prepare(stmt, SELECT_SAMPLE, strlen(SELECT_SAMPLE))) { fprintf(stderr, " mysql_stmt_prepare(), %s failed\n", SELECT_SAMPLE ); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } fprintf(stdout, " prepare, %s successful\n", SELECT_SAMPLE ); /* Get the parameter count from the statement */ param_count= mysql_stmt_param_count(stmt); fprintf(stdout, " total parameters in SELECT: %d\n", param_count); if (param_count != 1) /* validate parameter count */ { fprintf(stderr, " invalid parameter count returned by MySQL\n"); exit(0); } /* Fetch result set meta information */ prepare_meta_result = mysql_stmt_result_metadata(stmt); if (!prepare_meta_result) { fprintf(stderr, " mysql_stmt_result_metadata(), returned no meta information\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } /* Get total columns in the query */ column_count= mysql_num_fields(prepare_meta_result); fprintf(stdout, " total columns in SELECT statement: %d\n", column_count); if (column_count != 2) /* validate column count */ { fprintf(stderr, " invalid column count returned by MySQL\n"); exit(0); } /* STRING PARAM */ bind[0].buffer_type= MYSQL_TYPE_VAR_STRING; bind[0].buffer= (char *)str_data; bind[0].buffer_length= STRING_SIZE; bind[0].is_null= 0; bind[0].length= &str_length[0]; /* INTEGER PARAM */ /* This is a number type, so there is no need to specify buffer_length */ bind[1].buffer_type= MYSQL_TYPE_LONG; bind[1].buffer= (char *)&int_data; bind[1].is_null= 0; bind[1].length= 0; /* Bind the buffers */ if (mysql_stmt_bind_param(stmt, bind)) { fprintf(stderr, " mysql_stmt_bind_param() failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } /* Specify the parameter values */ strncpy(str_data, "duplicate", STRING_SIZE); /* string */ str_length[0]= strlen(str_data); /* Execute the select statement */ if (mysql_stmt_execute(stmt)) { fprintf(stderr, " mysql_stmt_execute(), 1 failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } /* INTEGER COLUMN */ bind[0].buffer_type= MYSQL_TYPE_LONG; bind[0].buffer= (char *)&int_data; bind[0].is_null= &is_null[0]; bind[0].length= &length[0]; /* STRING COLUMN */ bind[1].buffer_type= MYSQL_TYPE_VAR_STRING; bind[1].buffer= (char *)str_data; bind[1].buffer_length= STRING_SIZE; bind[1].is_null= &is_null[1]; bind[1].length= &length[1]; if (mysql_stmt_bind_result(stmt, bind)) { fprintf(stderr, " mysql_bind_result() failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } /* Initialize the result buffers */ is_null[0]= 0; int_data= 1111111111; /* column b VARCHAR -- MYSQL_TYPE_VAR_STRING */ is_null[1]= 0; strncpy(str_data, "INITIALIZED-INITIALIZED row", STRING_SIZE); length[1]= strlen(str_data); /* Now buffer all results to client */ if (mysql_stmt_store_result(stmt)) { fprintf(stderr, " mysql_stmt_store_result() failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } /* Fetch all rows */ row_count= 0; fprintf(stdout, "Fetching results ...\n"); while (!mysql_fetch(stmt)) { row_count++; fprintf(stdout, " row %d\n", row_count); /* column a */ fprintf(stdout, " column1 (integer) : "); if (is_null[0]) fprintf(stdout, " NULL\n"); else fprintf(stdout, " %d (length %ld)\n", int_data, length[0]); /* column b */ fprintf(stdout, " column1 (varchar) : "); if (is_null[0]) fprintf(stdout, " NULL\n"); else fprintf(stdout, " '%s' (length %ld)\n", str_data, length[1]); fprintf(stdout, "\n"); } /* Free the prepared result metadata */ mysql_free_result(prepare_meta_result); if (mysql_stmt_close(stmt)) { fprintf(stderr, " failed while closing the statement\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } fprintf(stdout, " DROP TABLE IF EXISTS test_table ..."); if (mysql_query(mysql, "DROP TABLE IF EXISTS test_table")) { fprintf(stderr, " failed\n"); fprintf(stderr, " %s\n", mysql_error(mysql)); exit(8); } fprintf(stdout, " OK\n"); /* close the connection */ fprintf(stdout, " closing the connection ..."); mysql_close(mysql); fprintf(stdout, " OK\n"); fprintf(stdout, "\n\n success !!! \n"); return 0; }