db.c File Reference

#include <mysql.h>
#include <stdio.h>
Include dependency graph for db.c:

Go to the source code of this file.

Functions

 main ()

Function Documentation

main ( void   ) 

Definition at line 33 of file db.c.

References database, password, server, and user.

00033        {        /* Simple C program that connects to MySQL Database server*/
00034  
00035    MYSQL *conn;
00036    MYSQL_RES *res;
00037    MYSQL_ROW row;
00038 
00039    char *server = "localhost";
00040    char *user = "root";
00041    char *password = "root"; /* set me first */
00042    char *database = "test";
00043 
00044    conn = mysql_init(NULL);
00045 
00046    /* Connect to database */
00047    if (!mysql_real_connect(conn, server,
00048          user, password, database, 0, NULL, 0)) {
00049       fprintf(stderr, "%s\n", mysql_error(conn));
00050       exit(1);
00051    }
00052 
00053 char cmd[40];
00054 int i=2;
00055      sprintf(cmd, "select * from student");
00056    /* send SQL query */
00057    if (mysql_query(conn, cmd)) {
00058       fprintf(stderr, "%s\n", mysql_error(conn));
00059       exit(1);
00060    }
00061 
00062    res = mysql_use_result(conn);
00063 
00064    /* output table name */
00065    printf("Data in student:\n");
00066    while ((row = mysql_fetch_row(res)) != NULL)
00067       printf("%s \n", row[0]);
00068    /* close connection */
00069    mysql_free_result(res);
00070    mysql_close(conn);
00071 }

Generated on Tue Jun 7 13:40:45 2011 for iPDC-DBServer by  doxygen 1.6.3