4 #include <mysql/mysql.h>
8 #define INSERT_SAMPLE "insert into nemesis_db_test (xx, yy, zz, tt) values (?,?,?,?)"
15 MYSQL_BIND bind[MAXCOLUMN];
17 unsigned long length[MAXCOLUMN];
18 int param_count, column_count, row_count;
21 char str_data[STRING_SIZE];
22 my_bool is_null[MAXCOLUMN];
25 * From http://dev.mysql.com/doc/refman/4.1/en/mysql-stmt-fetch.html
29 if ((mysql = mysql_init (NULL)) == NULL)
32 if (mysql_real_connect (mysql, NULL, "sdp", "sdp", "test", 0, NULL, 0L) == NULL) {
33 fprintf(stderr, " mysql_stmt_prepare(), SELECT failed\n");
34 fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
38 /* Prepare a SELECT query to fetch data from test_table */
39 stmt = mysql_stmt_init(mysql);
42 fprintf(stderr, " mysql_stmt_init(), out of memory\n");
46 if (mysql_stmt_prepare(stmt, INSERT_SAMPLE, strlen(INSERT_SAMPLE)))
48 fprintf(stderr, " mysql_stmt_prepare(), INSERT failed\n");
49 fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
52 fprintf(stdout, " prepare, INSERT successful\n");
54 /* Get the parameter count from the statement */
55 param_count= mysql_stmt_param_count(stmt);
56 fprintf(stdout, " total parameters in INSERT: %d\n", param_count);
58 if (param_count != MAXCOLUMN) /* validate parameter count */
60 fprintf(stderr, " invalid parameter count returned by MySQL\n");
64 /* Bind the result buffers for all 3 columns before fetching them */
66 memset(bind, 0, sizeof(bind));
69 bind[0].buffer_type= MYSQL_TYPE_LONG;
70 bind[0].buffer= (char *)&int_data;
71 bind[0].is_null= &is_null[0];
72 bind[0].length= &length[0];
75 bind[1].buffer_type= MYSQL_TYPE_STRING;
76 bind[1].buffer= (char *)str_data;
77 bind[1].buffer_length= STRING_SIZE;
78 bind[1].is_null= &is_null[1];
79 bind[1].length= &length [1];
82 bind[2].buffer_type= MYSQL_TYPE_FLOAT;
83 bind[2].buffer= (char *)&float_data;
84 bind[2].is_null= &is_null[2];
85 bind[2].length= &length[2];
88 bind[3].buffer_type= MYSQL_TYPE_DATETIME;
89 bind[3].buffer= (char *)&ts;
90 bind[3].is_null= &is_null[2];
91 bind[3].length= &length[2];
93 /* Bind the result buffers */
94 if (mysql_stmt_bind_param (stmt, bind))
96 fprintf(stderr, " mysql_stmt_bind_param() failed\n");
97 fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
102 strcpy (str_data, "insert: 99");
103 length [1] = strlen (str_data);
105 ts.year = 1996; ts.month = 2; ts.day = 11;
106 ts.hour = 19; ts.minute = 22; ts.second = 0;
108 /* Execute the INSERT query */
109 if (mysql_stmt_execute(stmt))
111 fprintf(stderr, " mysql_stmt_execute(), failed\n");
112 fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
117 strcpy (str_data, "insert: <null>");
118 length [1] = strlen (str_data);
120 ts.year = 1999; ts.month = 8; ts.day = 7;
121 ts.hour = 19; ts.minute = 18; ts.second = 17;
123 /* Execute the INSERT query */
124 if (mysql_stmt_execute(stmt))
126 fprintf(stderr, " mysql_stmt_execute(), failed\n");
127 fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
131 /* Close the statement */
132 if (mysql_stmt_close(stmt))
134 fprintf(stderr, " failed while closing the statement\n");
135 fprintf(stderr, " %s\n", mysql_stmt_error(stmt));