Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <mysql/mysql.h>
- #include <stdbool.h>
- #define STRING_SIZE 50
- #define INSERT_SAMPLE "INSERT INTO analytics.live (dat_sent, machine_id, foreign_addr, con_state) VALUES (?,?,?,?)"
- /* Compile with:
- gcc db.connect.c `mysql_config --libs` -O1
- for the best results
- */
- int main(int argc, char ** argv)
- {
- MYSQL_STMT *stmt;
- MYSQL_BIND bind[4];
- unsigned long affected_rows;
- int param_count;
- short small_data;
- int int_data;
- char str_data[STRING_SIZE];
- unsigned long str_length;
- int is_null;
- /*
- * Prepare an INSERT query with 4 parameters
- */
- MYSQL *mysql = mysql_init(NULL);
- if (mysql_real_connect(mysql, "localhost", "user", "password}",
- NULL, 0, NULL, 0) == NULL){
- fprintf(stderr, "%s\n", mysql_error(mysql));
- mysql_close(mysql);
- exit(1);
- }
- stmt = mysql_stmt_init(mysql);
- if (!stmt){
- fprintf(stderr, " mysql_stmt_init(), out of memory\n");
- return 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));
- return 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 != 4) /* validate parameter count */
- {
- fprintf(stderr, " invalid parameter count returned by MySQL\n");
- return 0;
- }
- /* Bind data for all 4 parameters */
- memset(bind, 0, sizeof(bind));
- /* INTEGER PARAM */
- bind[0].buffer_type= MYSQL_TYPE_LONG;
- bind[0].buffer= (char *)&int_data;
- bind[0].is_null= 0;
- bind[0].length= 0;
- /* STRING PARAM */
- bind[1].buffer_type= MYSQL_TYPE_STRING;
- bind[1].buffer= (char *)str_data;
- bind[1].buffer_length= STRING_SIZE;
- bind[1].is_null= 0;
- bind[1].length= &str_length;
- /* STRING PARAM */
- bind[2].buffer_type= MYSQL_TYPE_STRING;
- bind[2].buffer= (char *)str_data;
- bind[2].buffer_length= STRING_SIZE;
- bind[2].is_null= 0;
- bind[2].length= &str_length;
- /* STRING PARAM */
- bind[3].buffer_type= MYSQL_TYPE_STRING;
- bind[3].buffer= (char *)str_data;
- bind[3].buffer_length= STRING_SIZE;
- bind[3].is_null= 0;
- bind[3].length= &str_length;
- if (mysql_stmt_bind_param(stmt, bind)){
- fprintf(stderr, " mysql_stmt_bind_param() failed\n");
- fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
- return 0;
- }
- /* Specify the data values for the first row */
- int_data= argv[1]; /* integer */
- strncpy(str_data, "989b3ff047184022b33bd395a97cde4c", STRING_SIZE); /* string */
- str_length= strlen(str_data);
- /* INSERT SMALLINT data as NULL */
- is_null= 1;
- /* Execute the INSERT statement - 1*/
- if (mysql_stmt_execute(stmt))
- {
- fprintf(stderr, " mysql_stmt_execute(), 1 failed\n");
- fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
- return 0;
- }
- /* Get the number of affected rows */
- affected_rows= mysql_stmt_affected_rows(stmt);
- fprintf(stdout, " total affected rows(insert 1): %lu\n",
- (unsigned long) affected_rows);
- /*validate affected rows*/
- if (affected_rows != 1) {
- fprintf(stderr, "Invalid affected rows by MySQL \n");
- return 0;
- }
- int_data = 33;
- strncpy(str_data, "machine_id_goes_in_here", STRING_SIZE);
- str_length = strlen(str_data);
- small_data = 1000;
- is_null = 0;
- /* Executre ithe insert statement*/
- if (mysql_stmt_execute(stmt)){
- fprintf(stderr, "mysql_stmt_execute, 2 failde \n" );
- fprintf(stderr, "%s\n", mysql_stmt_error(stmt));
- return 0;
- }
- affected_rows = mysql_stmt_affected_rows(stmt);
- fprintf(stdout, "total affected rows(insert 2): %lu\n",(unsigned long) affected_rows);
- if (affected_rows != 1){
- fprintf(stderr, "invalid rows affected by MySQL\n");
- return 0;
- }
- if (mysql_stmt_close(stmt)){
- fprintf(stderr, " failed while closing the statement\n");
- fprintf(stderr, " %s\n", mysql_error(mysql));
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement