home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: InfoMgt / InfoMgt.zip / mysql-c.zip / mysqldll.txt < prev   
Text File  |  1999-12-18  |  4KB  |  134 lines

  1. Using mysql/2 3.22.26a from C and C++ programs. (rev 2)           1999-12-18
  2. =============================================================================
  3.  
  4. Requirements:
  5.     mysql/2 3.22.26a installed and working
  6.     mysql.h (modified version included in this package)
  7.     mysqldll.lib (included in this package)
  8.     An OS/2 C/C++ compiler
  9.  
  10.     I use IBM C/C++ 3.6 but it's probably very easy to make 
  11.     it work with other compilers. 
  12.  
  13.  
  14. How:
  15.     The header file (mysql.h) had to be modified to support the calling
  16.     convention of OS/2. The mysqldll.lib was created with implib.exe.
  17.  
  18.  
  19. Usage:
  20.     Ensure that the following header files are availible for your compiler:
  21.       mysql_com.h
  22.       mysql_version.h
  23.       mysql.h (modified version included in this package)
  24.  
  25.     Link with 
  26.       mysqldll.lib
  27.  
  28.  
  29. Tips:
  30.  
  31.     All needed documentation for the C api is in the MySql Manual (mysql.inf) but it took me 
  32.     a while to find out a few things. The API had a nasty habit of hanging the machine when
  33.     I called it with uninitlialized structures. So take care.
  34.  
  35.     You cannot call the mysql api inside another thread. It made me crazy before i found it
  36.     out :-((. I assume that each thread need a separate connection but in my case I just
  37.     rewrote the code without threading.
  38.  
  39.     Text is normally saved in the ANSI character set in mysql. If you plan to use it only
  40.     in your application it does not matter but if you want it to be useful on the net and
  41.     possible with phpMySqlAdmin (http://www.phpwizard.net/phpMyAdmin/) you should
  42.     assure that all NLS characters uses the ANSI coding. I have included a set of simple
  43.     conversion functions for the swedish NLS.
  44.  
  45.  
  46.  
  47. Example code:
  48.  
  49.     These are just snippets and will not compile by themselves but I hope that they
  50.     will help you.
  51.  
  52.      #include <mysql.h>
  53.  
  54.      MYSQL mysql;
  55.  
  56.      memset(&mysql, 0, sizeof(mysql));
  57.  
  58.      mysql_init(&mysql);
  59.      /* host and sockets and such should probably be put in a ini file... */
  60.      if (!mysql_real_connect(&mysql, "localhost", "root", NULL, "jma", 0, "\\socket\\mysql.sock", 0))
  61.         {
  62.         printf(szMessage, "Error: %s", mysql_error(&mysql));
  63.         return (FALSE);
  64.         }
  65.  
  66.      MYSQL_RES            *rs;
  67.      MYSQL_ROW          row;
  68.  
  69.      sprintf(szSql, "SELECT F.*, R.* FROM tFaktura F, tFakturaRad R WHERE F.year = %d AND F.fakturaid = R.fakturaid ORDER BY F.fakturaid, R.fakturaid", iCurYear);
  70.      mysql_real_query(&mysql, szSql, strlen(szSql));
  71.      rs = mysql_store_result(&mysql);
  72.  
  73.      while ( (row = mysql_fetch_row(rs)) )
  74.            {
  75.            /* The row contains the fields in a array row[0] to row[x] where x is the 
  76.                last field in the result set */
  77.            /* Since each field is returned in a string you must convert it to 
  78.                whatever you need (int, float) using atof(), atoi() before using it */
  79.            printf("%d", atoi(row[0]));
  80.            }
  81.  
  82.      mysql_free_result(rs);
  83.  
  84.      /* UPDATEs and DELETEs needs no resultsets */
  85.  
  86.      sprintf(szSql, "DELETE FROM tKund WHERE kundid = %d", iRecordNo);
  87.      if (mysql_real_query(&mysql, szSql, strlen(szSql)) != 0)
  88.         return (FALSE);
  89.  
  90.      mysql_close(&mysql);
  91.  
  92. /*-----------------------------------------------------------------------------
  93.    GetLastCustomerId: Find last customer id.
  94. -----------------------------------------------------------------------------*/
  95. int  GetLastCustomerId(void)
  96.      {
  97.      MYSQL mysql;
  98.      char               szSql[64];
  99.      MYSQL_RES          *rs;
  100.      MYSQL_ROW          row;
  101.      int                iNumber;
  102.  
  103.  
  104.     sprintf(szSql, "SELECT MAX(kundid) AS kundmin FROM tKund");
  105.  
  106.     if (mysql_real_query(&mysql, szSql, strlen(szSql)) != 0)
  107.        return(0);
  108.  
  109.     rs = mysql_store_result(&mysql);
  110.     row = mysql_fetch_row(rs);       
  111.  
  112.     if (row != NULL)
  113.        iNumber = atoi(row[0]);
  114.  
  115.     mysql_free_result(rs);
  116.  
  117.     return(iNumber);
  118.     }
  119.  
  120.  
  121.  
  122. Thanks to:
  123.  
  124.     Antony T Curtis <antony.curtis@olcs.net>
  125.     for porting mysql to OS/2
  126.  
  127.     Brian Havard <bjh@apache.org>
  128.     for porting Apache and PHP to OS/2 
  129.  
  130.  
  131.  
  132. How to contact me:
  133.     email: jma@jmast.se
  134.