Add no-deprecated to warnings due to dynamic exceptions.
[anna.git] / example / dbms.mysql / insert / main.cpp
1 #include <stdlib.h>
2 #include <string.h>
3 #include <stdio.h>
4
5 #include <mysql/mysql.h>
6
7 #define STRING_SIZE 50
8
9 #define INSERT_SAMPLE "insert into anna_db_test (xx, yy, zz, tt) values (?,?,?,?)"
10
11
12 #define MAXCOLUMN 4
13
14 MYSQL* mysql;
15 MYSQL_STMT    *stmt;
16 MYSQL_BIND    bind[MAXCOLUMN];
17 MYSQL_TIME    ts;
18 unsigned long length[MAXCOLUMN];
19 int           param_count, column_count, row_count;
20 float         float_data;
21 int           int_data;
22 char          str_data[STRING_SIZE];
23 my_bool       is_null[MAXCOLUMN];
24
25 /*
26  * From http://dev.mysql.com/doc/refman/4.1/en/mysql-stmt-fetch.html
27  */
28 int main (int argc, const char** argv)
29 {
30    if ((mysql = mysql_init (NULL)) == NULL)
31       exit(-12);
32    
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));
36       exit(0);
37    }   
38    
39    /* Prepare a SELECT query to fetch data from test_table */
40    stmt = mysql_stmt_init(mysql);
41    if (!stmt)
42    {
43      fprintf(stderr, " mysql_stmt_init(), out of memory\n");
44      exit(0);
45    }
46    
47    if (mysql_stmt_prepare(stmt, INSERT_SAMPLE, strlen(INSERT_SAMPLE)))
48    {
49      fprintf(stderr, " mysql_stmt_prepare(), INSERT failed\n");
50      fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
51      exit(0);
52    }
53    fprintf(stdout, " prepare, INSERT successful\n");
54
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);
58
59    if (param_count != MAXCOLUMN) /* validate parameter count */
60    {
61      fprintf(stderr, " invalid parameter count returned by MySQL\n");
62      exit(0);
63    }
64
65    /* Bind the result buffers for all 3 columns before fetching them */
66
67    memset(bind, 0, sizeof(bind));
68
69    /* INTEGER COLUMN */
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];
74
75    /* STRING COLUMN */
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];
81
82    /* FLOAT COLUMN */
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];
87
88    /* TIME COLUMN */
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];
93    
94    /* Bind the result buffers */
95    if (mysql_stmt_bind_param (stmt, bind))
96    {
97      fprintf(stderr, " mysql_stmt_bind_param() failed\n");
98      fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
99      exit(0);
100    }
101    
102    int_data = 99;
103    strcpy (str_data, "insert: 99");
104    length [1] = strlen (str_data);
105    float_data = 0.99;
106    ts.year = 1996; ts.month = 2; ts.day = 11; 
107    ts.hour = 19; ts.minute = 22; ts.second = 0;
108    
109    /* Execute the INSERT query */
110    if (mysql_stmt_execute(stmt))
111    {
112      fprintf(stderr, " mysql_stmt_execute(), failed\n");
113      fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
114      exit(0);   
115    }
116
117    is_null [0] = 1;
118    strcpy (str_data, "insert: <null>");
119    length [1] = strlen (str_data);
120    float_data = 0.98;
121    ts.year = 1999; ts.month = 8; ts.day = 7; 
122    ts.hour = 19; ts.minute = 18; ts.second = 17;
123
124    /* Execute the INSERT query */
125    if (mysql_stmt_execute(stmt))
126    {
127      fprintf(stderr, " mysql_stmt_execute(), failed\n");
128      fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
129      exit(0);   
130    }
131
132    /* Close the statement */
133    if (mysql_stmt_close(stmt))
134    {
135      fprintf(stderr, " failed while closing the statement\n");
136      fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
137      exit(0);
138    }
139
140   return 0;
141 }