#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[3]; 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[0]; bind[0].is_null= 0; bind[0].length= 0; bind[1].buffer_type= MYSQL_TYPE_TIME; bind[1].buffer= (char*)&ts[1]; bind[1].is_null= 0; bind[1].length= 0; bind[2].buffer_type= MYSQL_TYPE_TIMESTAMP; bind[2].buffer= (char*)&ts[2]; 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[0].neg = ts[1].neg = ts[2].neg = 0; ts[0].second_part = ts[1].second_part = ts[2].second_part = 0; ts[0].year = ts[1].year = ts[2].year = 2003; ts[0].month = ts[1].month = ts[2].month = 04; ts[0].day = ts[1].day = ts[2].day = 05; ts[0].hour = ts[1].hour = ts[2].hour = 11; ts[0].minute = ts[1].minute = ts[2].minute = 21; ts[0].second = ts[1].second = ts[2].second = 31; printf("Parameters before execute:\n"); for (i=0; i<3; i++) { printf("ts[%d] = %04d-%02d-%02d %02d:%02d:%02d.%06d\n", i, ts[i].year, ts[i].month, ts[i].day, ts[i].hour, ts[i].minute, ts[i].second, ts[i].second_part); } 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"); printf("Parameters after execute:\n"); for (i=0; i<3; i++) { printf("ts[%d] = %04d-%02d-%02d %02d:%02d:%02d.%06d\n", i, ts[i].year, ts[i].month, ts[i].day, ts[i].hour, ts[i].minute, ts[i].second, ts[i].second_part); } }