#include #include #include #include #include "/PATH/include/mysql/mysql.h" int main(){ MYSQL mysql; MYSQL_RES *res; MYSQL_ROW rw; MYSQL_STMT *stmt; MYSQL_BIND bind[3]; unsigned long strl; char txt[10]; char *ins; int ret= -1; int pc= -1; int nr= 0; int strsz= 10; /* ******************************************************************************* */ /* DATABASE CONNECTION: */ /* ******************************************************************************* */ if(mysql_init(&mysql)==NULL){ fprintf(stderr, "Failed to initate MySQL connection\n"); exit (1); } if(mysql_real_connect(&mysql,"127.0.0.1","myuser","mypw","mydb",3351,NULL,0) == NULL) { fprintf(stderr, "Real connection failed\n"); exit (1); } printf("\nConnected\n"); /* ******************************************************************************* */ /* SELECT VERSION TO LOOK IF IT IS THE RIGHT DBMS: */ /* ******************************************************************************* */ ret= mysql_query(&mysql, "select version()"); if(ret != 0){ fprintf(stderr, "Failed to select version()\n"); exit (1); } res= mysql_store_result(&mysql); rw= mysql_fetch_row(res); printf("version: %s\n", rw[0]); /* ******************************************************************************* */ /* TABLE CREATION: */ /* ******************************************************************************* */ ret= mysql_query(&mysql, "create table if not exists t(id serial,num integer,t text,primary key(id))"); if(ret != 0){ fprintf(stderr, "Failed to create table t\n"); exit (1); } printf("Table t created\n"); /* ******************************************************************************* */ /* INSERT TEST DATA WITHOUT PS: */ /* ******************************************************************************* */ ret= mysql_query(&mysql, "insert into t(num,t) values (1,'abc')"); if(ret != 0){ fprintf(stderr, "Failed to insert data without using PS\n"); exit (1); } printf("Data inserted without using PS\n"); /* ******************************************************************************* */ /* INSERT TEST DATA WITH PS: */ /* ******************************************************************************* */ stmt= mysql_stmt_init(&mysql); if(!stmt){ fprintf(stderr, "mysql_stmt_init(), out of memory\n"); exit(1); } ins= "insert into t(num,t) values(?,?)"; if (mysql_stmt_prepare(stmt, ins, strlen(ins))){ fprintf(stderr, "mysql_stmt_prepare(), INSERT failed\n"); fprintf(stderr, "%s\n", mysql_stmt_error(stmt)); exit(1); } fprintf(stdout, "prepare, INSERT successful\n"); /* Get the parameter count from the statement */ pc= mysql_stmt_param_count(stmt); fprintf(stdout, "total parameters in INSERT: %d\n", pc); if (pc != 2){ fprintf(stderr, "invalid parameter count returned by MySQL\n"); exit(1); } /* Bind the data for all 2 parameters */ memset(bind, 0, sizeof(bind)); bind[0].buffer_type= MYSQL_TYPE_LONG; bind[0].buffer= (char *)&nr; bind[0].is_null= 0; bind[0].length= 0; bind[1].buffer_type= MYSQL_TYPE_STRING; bind[1].buffer= (char *)txt; bind[1].buffer_length= strsz; bind[1].is_null= 0; bind[1].length= &strl; /* Bind the 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(1); } /* Specify the data values for the first row */ nr=2; strncpy(txt, "def", strsz); strl= strlen(txt); /* 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)); exit(1); } printf("Data inserted with using PS\n"); /* ******************************************************************************* */ /* DROP TABLE: */ /* ******************************************************************************* */ ret= mysql_query(&mysql, "drop table if exists t"); if(ret != 0){ fprintf(stderr, "Failed to drop table t\n"); exit (1); } printf("Table t dropped\n"); return 0; }