#include #include /* CREATE TABLE test.t1 ( id INT PRIMARY KEY AUTO_INCREMENT, date_field DATE, time_field TIME, timestamp_field TIMESTAMP ) */ #define query "INSERT INTO t1 (date_field, time_field, timestamp_field) VALUES (?,?,?)" int main (void) { MYSQL *mysql; MYSQL_TIME ts; MYSQL_BIND bind[3]; MYSQL_STMT *stmt; int i; mysql= mysql_init(NULL); mysql_real_connect(mysql, "127.0.0.1", NULL, NULL, "test", 0, NULL, 0); if (!(stmt= mysql_stmt_init(mysql))) { fprintf(stderr, "mysql_stmt_init() failed\n"); exit(0); } if (mysql_stmt_prepare(stmt, query, strlen(query))){ fprintf(stderr, "mysql_stmt_prepare() failed\n"); fprintf(stderr, "%s \n", mysql_stmt_error(stmt)); exit(0); } /* set up input buffers for all 3 parameters */ bind[0].buffer_type= MYSQL_TYPE_DATE; bind[0].buffer= (char*)&ts; bind[0].is_null= 0; bind[0].length= 0; bind[1].buffer_type= MYSQL_TYPE_TIME; bind[1].buffer= (char*)&ts; bind[1].is_null= 0; bind[1].length= 0; bind[2].buffer_type= MYSQL_TYPE_TIMESTAMP; bind[2].buffer= (char*)&ts; bind[2].is_null= 0; bind[2].length= 0; mysql_stmt_bind_param(stmt, bind); /* supply the data to be sent in the ts structure */ ts.neg= ts.second_part= 0; ts.year = 2003; ts.month = 04; ts.day = 05; ts.hour = 11; ts.minute = 21; ts.second = 31; /* Now, execute the query */ if (mysql_stmt_execute(stmt)){ fprintf(stderr, "mysql_stmt_execute failed\n"); fprintf(stderr, "%s\n", mysql_stmt_error(stmt)); exit(0); } printf("insert complete\n"); return 0; }