How to access MySQL database through C language in Linux

Source: Internet
Author: User
Error message: Can

Error message: Can

Error message: Can't connect to local MySQL server through socket '/var/run/mysqld. sock'

Solution: Create a mysqld directory under/var/run/, and create a link pointing to:/opt/lampp/var/mysql. sock

Input command: ln-s/opt/lampp/var/mysql. sock/var/run/mysqld. sock

.

**************************************** **************************************** ***********************************

The main. c file program code is as follows:

// ================================================ ======================================

# Include
# Include
# Include

Main ()
{
Char dbhost [32], dbuser [16], dbpasswd [16], dbname [16];
Char query [256];
Int count;
MYSQL * mysql;/* indicates the handle to a database connection */
MYSQL_RES * mysql_res;/* indicates the result of a query in the returned row */
MYSQL_ROW mysql_row;/* string array */
MYSQL_FIELD * field;
My_ulonglong rows;
Int I, num_fields;
/* This type is used for row numbers, mysql_affected_rows (), mysql_num_rows (), and mysql_insert_id ()
**/

Sprintf (dbhost, "localhost ");
Sprintf (dbuser, "root ");
Sprintf (dbpasswd, "123456 ");
Sprintf (dbname, "system ");


If (! (Mysql = mysql_init (NULL)/* obtain or initialize a MYSQL structure */
{
Printf ("mysql_init failed! ");
Mysql_close (mysql );
Exit (0 );
}

If (! Mysql_real_connect (mysql, dbhost, dbuser, dbpasswd, dbname, 3306, NULL, 0 ))
{/* Connect to a MySQL Server */
Printf ("% s", mysql_error (mysql ));
Printf ("\ n failed to connect to the server. Please contact the system administrator! \ N ");
Mysql_close (mysql );
Exit (0 );
}

Strcpy (query, "select * from ns_materiel_import where status = 0 order by itemid asc limit 1000 ");
If (mysql_query (mysql, query)/* execute an SQL query specified as an empty string */
{
Printf ("% s", mysql_error (mysql ));
Printf ("\ nmysql_query error! \ N ");
Mysql_close (mysql );
Exit (0 );
}
Mysql_res = mysql_store_result (mysql);/* retrieve a complete set of results for the customer */
Rows = mysql_num_rows (mysql_res);/* returns the number of duplicate columns in a result set */
If (rows = 0)/* This Management Code does not exist and returns */
{
Printf ("\ n return value is blank \ n ");
Mysql_free_result (mysql_res);/* release the memory used by a result set */
Mysql_close (mysql);/* close a server connection */
Exit (0 );
}
Else
{
Num_fields = mysql_num_fields (mysql_res );
For (I = 0; I <num_fields; I ++)/* print the field name */
{
Field = mysql_fetch_field_direct (mysql_res, I );
Printf ("% s \ t", field-> name );
}


For (count = 0; count {
Mysql_row = mysql_fetch_row (mysql_res);/* obtain the next row from the result set */
For (I = 0; I <num_fields; I ++)
{
Printf ("% s \ t", mysql_row [I]);
}
Printf ("\ n ");
}

}

# If 0
For (count = 0; count {
Mysql_row = mysql_fetch_row (mysql_res);/* obtain the next row from the result set */
Printf ("User ID: % s \ t", mysql_row [0]);
Printf ("username: % s \ t", mysql_row [2]);
Printf ("Real name: % s \ t", mysql_row [4]);
Printf ("nickname: % s \ t", mysql_row [5]);
Printf ("title: % s \ t", mysql_row [7]);
Printf ("Tel: % s \ n", mysql_row [8]);
}
# Endif

Mysql_free_result (mysql_res );
Mysql_close (mysql );
Printf ("\ nApp exit !!! \ N ");
}

**************************************** **************************************** ***********************************

The contents of Makefile are as follows:

#===================================================== ======================================
# Makefile template write by albert. shi
#
# Shanghai
#===================================================== ======================================

# Standard defines:
CC = gcc

WRES = windres

Hopem =/usr/src/linux-2.6.32.21
VPATH = $ (hopem)/include
ODir =.
Bin =.
Src =.
LibDirs =
IncDirs =

LIBS =-s-rdynamic-L/usr/lib/mysql-lmysqlclient
C_FLAGS + =-I/usr/include/mysql/-DBIG_JOINS = 1-fno-strict-aliasing-DUNIV_LINUX-Wl,-Bsymbolic-functions

# C_FLAGS =-O-I/usr/include/mysql-DBIG_JOINS = 1-fno-strict-aliasing-DUNIV_LINUX-Wl, -Bsymbolic-functions-rdynamic-L/usr/lib/mysql-lmysqlclient

SRCS = \
$ (Src)/main. c \
# $ (Src)/getfileargv. c \
# $ (Src)/bdidll. c \
# $ (Src)/bdisetup. c

EXOBJS = \
$ (ODir)/main. o \
# $ (ODir)/getfileargv. o \
# $ (ODir)/bdidll. o \
# $ (ODir)/bdisetup. o

ALLOBJS = $ (EXOBJS)
ALLBIN = $ (Bin)/app
ALLTGT = $ (Bin)/app

# User defines:

### Targets follow ---------------------------------

All: $ (ALLTGT)

Objs: $ (ALLOBJS)

Cleanobjs:
Rm-f $ (ALLOBJS)

Cleanbin:
Rm-f $ (ALLBIN)

Cleantgt:
Rm-f $ (ALLTGT)

Clean: cleanobjs cleanbin

Cleanall: cleanobjs cleanbin cleantgt

### User Targets follow ---------------------------------


### Dependency rules follow -----------------------------
# --- List all bin file ----------------------------------
$ (ALLTGT): $ (EXOBJS)
$ (CC)-o $ (ALLBIN) $ (EXOBJS) $ (incDirs) $ (libDirs) $ (LIBS)

#--------------------------------------------------------
# ---- Compile all *. c to *. c ------
%. O: %. c
$ (CC) $ (C_FLAGS) $ (incDirs)-c-o $ @ $ <

# --- Another way to compile *. c to *. o ------
# $ (ODir)/main. o: main. c
# $ (CC) $ (C_FLAGS) $ (incDirs)-c-o $ @ $ <

#--------------------------------------------------------

,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.