BMS-HOSxP Community

HOSxP => Development => ข้อความที่เริ่มโดย: doramon ที่ กรกฎาคม 19, 2008, 22:49:07 PM

หัวข้อ: ฝากภาษาซี mysql-c-api.c.txt
เริ่มหัวข้อโดย: doramon ที่ กรกฎาคม 19, 2008, 22:49:07 PM
โค๊ด: C
  1.  
  2. /*
  3. See url for more info:
  4. http://www.cyberciti.biz/tips/linux-unix-connect-mysql-c-api-program.html
  5.  
  6. */
  7. #include <mysql.h>
  8. #include <stdio.h>
  9.  
  10. int main(void) {
  11.    MYSQL *conn;
  12.    MYSQL_RES *res;
  13.    MYSQL_ROW row;
  14.   /* Change me */
  15.    char *server = "localhost";
  16.    char *user = "root";
  17.    char *password = "PASSWORD";
  18.    char *database = "mysql";
  19.    
  20.    conn = mysql_init(NULL);
  21.    
  22.    /* Connect to database */
  23.    if (!mysql_real_connect(conn, server,
  24.          user, password, database, 0, NULL, 0)) {
  25.       fprintf(stderr, "%s\n", mysql_error(conn));
  26.       exit(1);
  27.    }
  28.  
  29.    /* send SQL query */
  30.    if (mysql_query(conn, "show tables")) {
  31.       fprintf(stderr, "%s\n", mysql_error(conn));
  32.       exit(1);
  33.    }
  34.  
  35.    res = mysql_use_result(conn);
  36.    
  37.    /* output table name */
  38.    printf("MySQL Tables in mysql database:\n");
  39.    while ((row = mysql_fetch_row(res)) != NULL)
  40.       printf("%s \n", row[0]);
  41.  
  42.    /* close connection */
  43.    mysql_free_result(res);
  44.    mysql_close(conn);
  45.  
  46.   return 0;
  47. }
  48.  
  49.  
  50.  
  51.  
หัวข้อ: Re: ฝากภาษาซี mysql-c-api.c.txt
เริ่มหัวข้อโดย: เข้ม ที่ กรกฎาคม 20, 2008, 12:33:22 PM
Howto: Connect MySQL server using C program API under Linux or UNIX

ขอบคุณ อ. อ๊อด ครับ แหล่ม...จริง ๆ  ;D
หัวข้อ: Re: ฝากภาษาซี mysql-c-api.c.txt
เริ่มหัวข้อโดย: doramon ที่ กรกฎาคม 20, 2008, 14:22:43 PM
http://dev.mysql.com/doc/refman/5.1/en/c.html