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