5 #include <mysql/mysql.h>
9 #define INSERT_SAMPLE "insert into anna_db_test (xx, yy, zz, tt) values (?,?,?,?)"
16 MYSQL_BIND bind[MAXCOLUMN];
18 unsigned long length[MAXCOLUMN];
19 int param_count, column_count, row_count;
22 char str_data[STRING_SIZE];
23 my_bool is_null[MAXCOLUMN];
26 * From http://dev.mysql.com/doc/refman/4.1/en/mysql-stmt-fetch.html
28 int main (int argc, const char** argv)
30 if ((mysql = mysql_init (NULL)) == NULL)
33 if (mysql_real_connect (mysql, NULL, "sdp", "sdp", "test", 0, NULL, 0L) == NULL) {
34 fprintf(stderr, " mysql_stmt_prepare(), SELECT failed\n");
35 fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
39 /* Prepare a SELECT query to fetch data from test_table */
40 stmt = mysql_stmt_init(mysql);
43 fprintf(stderr, " mysql_stmt_init(), out of memory\n");
47 if (mysql_stmt_prepare(stmt, INSERT_SAMPLE, strlen(INSERT_SAMPLE)))
49 fprintf(stderr, " mysql_stmt_prepare(), INSERT failed\n");
50 fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
53 fprintf(stdout, " prepare, INSERT successful\n");
55 /* Get the parameter count from the statement */
56 param_count= mysql_stmt_param_count(stmt);
57 fprintf(stdout, " total parameters in INSERT: %d\n", param_count);
59 if (param_count != MAXCOLUMN) /* validate parameter count */
61 fprintf(stderr, " invalid parameter count returned by MySQL\n");
65 /* Bind the result buffers for all 3 columns before fetching them */
67 memset(bind, 0, sizeof(bind));
70 bind[0].buffer_type= MYSQL_TYPE_LONG;
71 bind[0].buffer= (char *)&int_data;
72 bind[0].is_null= &is_null[0];
73 bind[0].length= &length[0];
76 bind[1].buffer_type= MYSQL_TYPE_STRING;
77 bind[1].buffer= (char *)str_data;
78 bind[1].buffer_length= STRING_SIZE;
79 bind[1].is_null= &is_null[1];
80 bind[1].length= &length [1];
83 bind[2].buffer_type= MYSQL_TYPE_FLOAT;
84 bind[2].buffer= (char *)&float_data;
85 bind[2].is_null= &is_null[2];
86 bind[2].length= &length[2];
89 bind[3].buffer_type= MYSQL_TYPE_DATETIME;
90 bind[3].buffer= (char *)&ts;
91 bind[3].is_null= &is_null[2];
92 bind[3].length= &length[2];
94 /* Bind the result buffers */
95 if (mysql_stmt_bind_param (stmt, bind))
97 fprintf(stderr, " mysql_stmt_bind_param() failed\n");
98 fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
103 strcpy (str_data, "insert: 99");
104 length [1] = strlen (str_data);
106 ts.year = 1996; ts.month = 2; ts.day = 11;
107 ts.hour = 19; ts.minute = 22; ts.second = 0;
109 /* Execute the INSERT query */
110 if (mysql_stmt_execute(stmt))
112 fprintf(stderr, " mysql_stmt_execute(), failed\n");
113 fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
118 strcpy (str_data, "insert: <null>");
119 length [1] = strlen (str_data);
121 ts.year = 1999; ts.month = 8; ts.day = 7;
122 ts.hour = 19; ts.minute = 18; ts.second = 17;
124 /* Execute the INSERT query */
125 if (mysql_stmt_execute(stmt))
127 fprintf(stderr, " mysql_stmt_execute(), failed\n");
128 fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
132 /* Close the statement */
133 if (mysql_stmt_close(stmt))
135 fprintf(stderr, " failed while closing the statement\n");
136 fprintf(stderr, " %s\n", mysql_stmt_error(stmt));