"Fossies" - the Fresh Open Source Software Archive

Member "mariadb-connector-c-3.0.8-src/examples/mysql_affected_rows.c" (18 Dec 2018, 2713 Bytes) of package /linux/misc/mariadb-connector-c-3.0.8-src.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "mysql_affected_rows.c": 3.0.3-src_vs_3.0.4-src.

    1 #include <mysql.h>
    2 #include <stdio.h>
    3 #include <string.h>
    4 #include <stdlib.h>
    5 
    6 void show_error(MYSQL *mysql)
    7 {
    8   printf("Error(%d) [%s] \"%s\"", mysql_errno(mysql),
    9                                   mysql_sqlstate(mysql),
   10                                   mysql_error(mysql));
   11   mysql_close(mysql);
   12   exit(-1);
   13 }
   14 
   15 int main(int argc, char *argv[])
   16 {
   17   MYSQL *mysql;
   18   const char *query;
   19   MYSQL_RES *result;
   20 
   21   mysql= mysql_init(NULL);
   22   if (!mysql_real_connect(mysql, "localhost", "example", "example_pw", 
   23                           "example_db", 0, "/tmp/mysql.sock", 0))
   24     show_error(mysql);
   25 
   26   query= "DROP TABLE IF EXISTS affected_rows";
   27   if (mysql_real_query(mysql, query, strlen(query)))
   28     show_error(mysql);
   29  
   30   query= "CREATE TABLE affected_rows (id int not null, my_name varchar(50),"
   31          "PRIMARY KEY(id))";
   32   if (mysql_real_query(mysql, query, strlen(query)))
   33     show_error(mysql);
   34 
   35   /* Affected rows with INSERT statement */
   36   query= "INSERT INTO affected_rows VALUES (1, \"First value\"),"
   37          "(2, \"Second value\")";
   38   if (mysql_real_query(mysql, query, strlen(query)))
   39     show_error(mysql);
   40   printf("Affected_rows after INSERT: %lu\n",
   41          (unsigned long) mysql_affected_rows(mysql));
   42 
   43   /* Affected rows with REPLACE statement */
   44   query= "REPLACE INTO affected_rows VALUES (1, \"First value\"),"
   45          "(2, \"Second value\")";
   46   if (mysql_real_query(mysql, query, strlen(query)))
   47     show_error(mysql);
   48   printf("Affected_rows after REPLACE: %lu\n",
   49          (unsigned long) mysql_affected_rows(mysql));
   50 
   51   /* Affected rows with UPDATE statement */
   52   query= "UPDATE affected_rows SET id=1 WHERE id=1";
   53   if (mysql_real_query(mysql, query, strlen(query)))
   54     show_error(mysql);
   55   printf("Affected_rows after UPDATE: %lu\n",
   56          (unsigned long) mysql_affected_rows(mysql));
   57 
   58   query= "UPDATE affected_rows SET my_name=\"Monty\" WHERE id=1";
   59   if (mysql_real_query(mysql, query, strlen(query)))
   60     show_error(mysql);
   61   printf("Affected_rows after UPDATE: %lu\n",
   62          (unsigned long) mysql_affected_rows(mysql));
   63 
   64   /* Affected rows after select */
   65   query= "SELECT id, my_name FROM affected_rows";
   66   if (mysql_real_query(mysql, query, strlen(query)))
   67     show_error(mysql);
   68   result= mysql_store_result(mysql);
   69   printf("Affected_rows after SELECT and storing result set: %lu\n",
   70          (unsigned long) mysql_affected_rows(mysql));
   71   mysql_free_result(result);
   72 
   73   /* Affected rows with DELETE statement */
   74   query= "DELETE FROM affected_rows";
   75   if (mysql_real_query(mysql, query, strlen(query)))
   76     show_error(mysql);
   77   printf("Affected_rows after DELETE: %lu\n",
   78          (unsigned long) mysql_affected_rows(mysql));
   79 
   80   mysql_close(mysql);
   81   return 0;
   82 }