Get ready:
System: ubuntu12.04
Tool: Gcc,mysql,libmysqlclient-dev
Test code (file name: CON_MYSQL.C):
#include <mysql/mysql.h> #include <stdio.h>main () { MYSQL *conn; mysql_res *res; mysql_ row row; char *server = "localhost"; char *user = "root"; char *password = "123456"; /* set me first */ char *database = "MySQL"; char *query = "Select host,user,password from user"; int t, r; conn = mysql_init (NULL); if (!mysql_real_connect (conn, server, user, password, database, 0, null, 0)) { printf ("Error connecting to dataBase: %s\n ", &NBSP;MYSQL_ERROR (conn)); } else { printf ("connected....\n"); } t = mysql_query (Conn, query); if (t) { printf ("Error making Query: %s\n ", &NBSP;MYSQL_ERROR (conn)); } else { printf ("query made...\n"); res = mysql_use_result (conn); if (RES) { for (R=0; r<=mysql_field_count (conn); r++) &NBSP;&NBSP;&NBsp; { row = mysql_fetch_row (RES); if (row<0) break; for (T=0; t<mysql_num_fields (res); t++) printf (" %s ", row[t]); printf ("\ n"); } } mysql_free_result (res); } mysql_close (conn); return 0;}
Compile:
GCC con_mysql.c-l/usr/lib/mysql/-lmysqlclient-o con_mysql
Perform:
./con_mysql
Results:
Connected .... Query Made...localhostroot*6bb4837eb74329105ee4568dda7dc67ed2ca2ad9ubunturoot127.0.0.1root::1root
This article is from the "9645582" blog, please be sure to keep this source http://9655582.blog.51cto.com/9645582/1794151
ubuntu12.04 using the C API to manipulate the database