00001 /* ----------------------------------------------------------------------------- 00002 * db.c 00003 * 00004 * iPDC - Phasor Data Concentrator 00005 * 00006 * Copyright (C) 2011 Nitesh Pandit 00007 * Copyright (C) 2011 Kedar V. Khandeparkar 00008 * 00009 * This program is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU General Public License 00011 * as published by the Free Software Foundation; either version 2 00012 * of the License, or (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software 00021 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00022 * 00023 * Authors: 00024 * Kedar V. Khandeparkar <kedar.khandeparkar@gmail.com> 00025 * Nitesh Pandit <panditnitesh@gmail.com> 00026 * 00027 * ----------------------------------------------------------------------------- */ 00028 00029 00030 #include <mysql.h> 00031 #include <stdio.h> 00032 00033 main() { /* 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 } 00072
1.6.3