#include #include #include #include /* Static SQL */ #define DROP_SAMPLE_TABLE "DROP TABLE IF EXISTS t1" #define CREATE_SAMPLE_TABLE "CREATE TABLE t1(a INT, \ b char(10))" /* 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; static void create_sample_table( MYSQL *mysql ) { if (mysql_query(mysql, DROP_SAMPLE_TABLE)) { fprintf(stderr, " DROP TABLE failed\n"); fprintf(stderr, " %s\n", mysql_error(mysql)); exit(0); } if (mysql_query(mysql, CREATE_SAMPLE_TABLE)) { fprintf(stderr, " CREATE TABLE failed\n"); fprintf(stderr, " %s\n", mysql_error(mysql)); exit(0); } } /******************************************************** * my_prepare * *********************************************************/ static MYSQL_STMT *my_prepare(MYSQL *mysql, const char *stmt_buff, unsigned int exp_errno) { /* stmt_buff contains the statement to be prepared */ /* exp_errno contains the errno expected */ MYSQL_STMT *stmt; fprintf(stdout, "my_prepare: %s ", stmt_buff); /* Prepare an statement */ stmt = mysql_prepare(mysql, stmt_buff, strlen(stmt_buff)); if ( (NULL == stmt) || ( 0 != exp_errno )) { if ( exp_errno == mysql_errno(mysql) ) { fprintf(stdout, " OK, got expected error\n"); return (NULL); } else { fprintf(stdout, " FAILURE\n"); fprintf(stderr, " mysql_prepare(), %s failed\n", stmt_buff); fprintf(stderr, " expected: %d \n", exp_errno); fprintf(stderr, " got: %d %s\n",mysql_errno(mysql),mysql_error(mysql)); exit(0); } } fprintf(stdout, " OK\n"); return (stmt); } int main(void) { MYSQL *mysql; MYSQL_STMT *stmt; fprintf(stdout, " Establishing a connection to the database ...\n"); /* Create our mysql structure */ if (!(mysql = mysql_init(NULL))) { fprintf(stderr, " mysql_init() failed\n"); exit(0); } /* 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(0); } /* 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(0); } fprintf(stdout, " OK\n"); create_sample_table(mysql); stmt=my_prepare(mysql, "select sum(a) + 200, 1 from t1 \ union distinct \ select sum(a) + 200, 1 from t1 \ group by b ", 0) ; stmt=my_prepare(mysql, "select sum(a) + 200, ? from t1 \ group by b \ union distinct \ select sum(a) + 200, 1 from t1 \ group by b ", 0) ; stmt=my_prepare(mysql, "select sum(a) + 200, ? from t1 \ union distinct \ select sum(a) + 200, 1 from t1 \ group by b ", 0) ; /* close the connection */ fprintf(stdout, " closing the connection ..."); mysql_close(mysql); fprintf(stdout, " OK\n"); fprintf(stdout, "\n\n success !!! \n"); return 0; }