X-Git-Url: https://git.teslayout.com/public/public/public/?a=blobdiff_plain;f=example%2Fdbms.mysql%2Finsert%2Fmain.cpp;fp=example%2Fdbms.mysql%2Finsert%2Fmain.cpp;h=7ed298eac694d2fdc174fc3c38d4e2e983b83075;hb=78be86969d2f26a9084b0c4af6ce43d5fa4ed3fd;hp=0000000000000000000000000000000000000000;hpb=a3b95648bd76140ef55e0b5941d423eee6c3856f;p=anna.git diff --git a/example/dbms.mysql/insert/main.cpp b/example/dbms.mysql/insert/main.cpp new file mode 100644 index 0000000..7ed298e --- /dev/null +++ b/example/dbms.mysql/insert/main.cpp @@ -0,0 +1,141 @@ +#include +#include +#include + +#include + +#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: "); + 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; +}