home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2003 September / INTERNET107.ISO / pc / software / windows / building / mysql / data1.cab / Servers / Embedded / DLL / test_dll.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2003-05-17  |  3.2 KB  |  132 lines

  1. /*
  2.  * A simple example client, using the embedded MySQL server library
  3.  */
  4.  
  5. #include "stdafx.h"
  6. #include <winsock.h>
  7. #include <mysql.h>
  8. #include <stdarg.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. MYSQL *db_connect(const char *dbname);
  13. void db_disconnect(MYSQL *db);
  14. void db_do_query(MYSQL *db, const char *query);
  15.  
  16. const char *server_groups[] = {
  17.   "test_libmysqld_SERVER", "embedded", "server", NULL
  18. };
  19.  
  20. int main(int argc, char* argv[])
  21. {
  22.     MYSQL *one, *two;
  23.  
  24.   /* mysql_server_init() must be called before any other mysql
  25.    * functions.
  26.    *
  27.    * You can use mysql_server_init(0, NULL, NULL), and it will
  28.    * initialise the server using groups = {
  29.    *   "server", "embedded", NULL
  30.    *  }.
  31.    *
  32.    * In your $HOME/.my.cnf file, you probably want to put:
  33.  
  34.    [test_libmysqld_SERVER]
  35.    language = /path/to/source/of/mysql/sql/share/english
  36.  
  37.    * You could, of course, modify argc and argv before passing
  38.    * them to this function.  Or you could create new ones in any
  39.    * way you like.  But all of the arguments in argv (except for
  40.    * argv[0], which is the program name) should be valid options
  41.    * for the MySQL server.
  42.    *
  43.    * If you link this client against the normal mysqlclient
  44.    * library, this function is just a stub that does nothing.
  45.    */
  46.   mysql_server_init(argc, argv, (char **)server_groups);
  47.  
  48.   one = db_connect("test");
  49.   two = db_connect(NULL);
  50.   
  51.   db_do_query(one, "show table status");
  52.   db_do_query(two, "show databases");
  53.  
  54.   mysql_close(two);
  55.   mysql_close(one);
  56.  
  57.   /* This must be called after all other mysql functions */
  58.   mysql_server_end();
  59.     return 0;
  60. }
  61.  
  62. static void
  63. die(MYSQL *db, char *fmt, ...)
  64. {
  65.   va_list ap;
  66.   va_start(ap, fmt);
  67.   vfprintf(stderr, fmt, ap);
  68.   va_end(ap);
  69.   (void)putc('\n', stderr);
  70.   if (db)
  71.     db_disconnect(db);
  72.   exit(EXIT_FAILURE);
  73. }
  74.  
  75. MYSQL *
  76. db_connect(const char *dbname)
  77. {
  78.   MYSQL *db = mysql_init(NULL);
  79.   if (!db)
  80.     die(db, "mysql_init failed: no memory");
  81.   /*
  82.    * Notice that the client and server use separate group names.
  83.    * This is critical, because the server will not accept the
  84.    * client's options, and vice versa.
  85.    */
  86.   mysql_options(db, MYSQL_READ_DEFAULT_GROUP, "test_libmysqld_CLIENT");
  87.   if (!mysql_real_connect(db, NULL, NULL, NULL, dbname, 0, NULL, 0))
  88.     die(db, "mysql_real_connect failed: %s", mysql_error(db));
  89.  
  90.   return db;
  91. }
  92.  
  93. void
  94. db_disconnect(MYSQL *db)
  95. {
  96.   mysql_close(db);
  97. }
  98.  
  99. void
  100. db_do_query(MYSQL *db, const char *query)
  101. {
  102.   if (mysql_query(db, query) != 0)
  103.     goto err;
  104.  
  105.   if (mysql_field_count(db) > 0)
  106.   {
  107.     MYSQL_RES   *res;
  108.     MYSQL_ROW    row, end_row;
  109.     int num_fields;
  110.  
  111.     if (!(res = mysql_store_result(db)))
  112.       goto err;
  113.     num_fields = mysql_num_fields(res);
  114.     while ((row = mysql_fetch_row(res)))
  115.     {
  116.       (void)fputs(">> ", stdout);
  117.       for (end_row = row + num_fields; row < end_row; ++row)
  118.         (void)printf("%s\t", row ? (char*)*row : "NULL");
  119.       (void)fputc('\n', stdout);
  120.     }
  121.     (void)fputc('\n', stdout);
  122.   }
  123.   else
  124.     (void)printf("Affected rows: %lld\n", mysql_affected_rows(db));
  125.  
  126.   return;
  127.  
  128. err:
  129.   die(db, "db_do_query failed: %s [%s]", mysql_error(db), query);
  130. }
  131.  
  132.