Revert "Remove mysql and oracle resources for anna-ericsson project"
[anna.git] / example / dbms.mysql / insert / main.cpp
diff --git a/example/dbms.mysql/insert/main.cpp b/example/dbms.mysql/insert/main.cpp
new file mode 100644 (file)
index 0000000..7ed298e
--- /dev/null
@@ -0,0 +1,141 @@
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+#include <mysql/mysql.h>
+
+#define STRING_SIZE 50
+
+#define INSERT_SAMPLE "insert into anna_db_test (xx, yy, zz, tt) values (?,?,?,?)"
+
+
+#define MAXCOLUMN 4
+
+MYSQL* mysql;
+MYSQL_STMT    *stmt;
+MYSQL_BIND    bind[MAXCOLUMN];
+MYSQL_TIME    ts;
+unsigned long length[MAXCOLUMN];
+int           param_count, column_count, row_count;
+float         float_data;
+int           int_data;
+char          str_data[STRING_SIZE];
+my_bool       is_null[MAXCOLUMN];
+
+/*
+ * From http://dev.mysql.com/doc/refman/4.1/en/mysql-stmt-fetch.html
+ */
+int main (int argc, const char** argv)
+{
+   if ((mysql = mysql_init (NULL)) == NULL)
+      exit(-12);
+   
+   if (mysql_real_connect (mysql, NULL, "sdp", "sdp", "test", 0, NULL, 0L) == NULL) {
+      fprintf(stderr, " mysql_stmt_prepare(), SELECT failed\n");
+      fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
+      exit(0);
+   }   
+   
+   /* Prepare a SELECT query to fetch data from test_table */
+   stmt = mysql_stmt_init(mysql);
+   if (!stmt)
+   {
+     fprintf(stderr, " mysql_stmt_init(), out of memory\n");
+     exit(0);
+   }
+   
+   if (mysql_stmt_prepare(stmt, INSERT_SAMPLE, strlen(INSERT_SAMPLE)))
+   {
+     fprintf(stderr, " mysql_stmt_prepare(), INSERT failed\n");
+     fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
+     exit(0);
+   }
+   fprintf(stdout, " prepare, INSERT successful\n");
+
+   /* Get the parameter count from the statement */
+   param_count= mysql_stmt_param_count(stmt);
+   fprintf(stdout, " total parameters in INSERT: %d\n", param_count);
+
+   if (param_count != MAXCOLUMN) /* validate parameter count */
+   {
+     fprintf(stderr, " invalid parameter count returned by MySQL\n");
+     exit(0);
+   }
+
+   /* Bind the result buffers for all 3 columns before fetching them */
+
+   memset(bind, 0, sizeof(bind));
+
+   /* INTEGER COLUMN */
+   bind[0].buffer_type= MYSQL_TYPE_LONG;
+   bind[0].buffer= (char *)&int_data;
+   bind[0].is_null= &is_null[0];
+   bind[0].length= &length[0];
+
+   /* STRING COLUMN */
+   bind[1].buffer_type= MYSQL_TYPE_STRING;
+   bind[1].buffer= (char *)str_data;
+   bind[1].buffer_length= STRING_SIZE;
+   bind[1].is_null= &is_null[1];
+   bind[1].length= &length [1];
+
+   /* FLOAT COLUMN */
+   bind[2].buffer_type= MYSQL_TYPE_FLOAT;
+   bind[2].buffer= (char *)&float_data;
+   bind[2].is_null= &is_null[2];
+   bind[2].length= &length[2];
+
+   /* TIME COLUMN */
+   bind[3].buffer_type= MYSQL_TYPE_DATETIME;
+   bind[3].buffer= (char *)&ts;
+   bind[3].is_null= &is_null[2];
+   bind[3].length= &length[2];
+   
+   /* Bind the result buffers */
+   if (mysql_stmt_bind_param (stmt, bind))
+   {
+     fprintf(stderr, " mysql_stmt_bind_param() failed\n");
+     fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
+     exit(0);
+   }
+   
+   int_data = 99;
+   strcpy (str_data, "insert: 99");
+   length [1] = strlen (str_data);
+   float_data = 0.99;
+   ts.year = 1996; ts.month = 2; ts.day = 11; 
+   ts.hour = 19; ts.minute = 22; ts.second = 0;
+   
+   /* Execute the INSERT query */
+   if (mysql_stmt_execute(stmt))
+   {
+     fprintf(stderr, " mysql_stmt_execute(), failed\n");
+     fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
+     exit(0);   
+   }
+
+   is_null [0] = 1;
+   strcpy (str_data, "insert: <null>");
+   length [1] = strlen (str_data);
+   float_data = 0.98;
+   ts.year = 1999; ts.month = 8; ts.day = 7; 
+   ts.hour = 19; ts.minute = 18; ts.second = 17;
+
+   /* Execute the INSERT query */
+   if (mysql_stmt_execute(stmt))
+   {
+     fprintf(stderr, " mysql_stmt_execute(), failed\n");
+     fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
+     exit(0);   
+   }
+
+   /* Close the statement */
+   if (mysql_stmt_close(stmt))
+   {
+     fprintf(stderr, " failed while closing the statement\n");
+     fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
+     exit(0);
+   }
+
+  return 0;
+}