在Linux下通过C语言访问MySQL数据库的方法


错误提示:Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

解决方法:在/var/run/ 下面创建一个 mysqld目录,然后建立一个链接指向:/opt/lampp/var/mysql/mysql.sock

输入指令: ln -s /opt/lampp/var/mysql/mysql.sock /var/run/mysqld/mysqld.sock

即可解决。

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

main.c文件 程序代码如下:

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

#include <stdio.h>
#include <strings.h>
#include <mysql.h>

main()
{
    char dbhost[32],dbuser[16],dbpasswd[16],dbname[16];
    char query[256];
    int count;
    MYSQL * mysql; /*表示对一个数据库连接的句柄*/
    MYSQL_RES *mysql_res; /*代表返回行的一个查询的结果*/
    MYSQL_ROW mysql_row; /*字符串数组*/   
    MYSQL_FIELD *field;
    my_ulonglong rows;
    int i,num_fields;
    /*该类型用于行编号和mysql_affected_rows()、mysql_num_rows()和mysql_insert_id()
     * */

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


    if(!(mysql=mysql_init(NULL))) /*获得或初始化一个MYSQL结构*/
    {
        printf("mysql_init失败!");
        mysql_close(mysql);
        exit(0);
    }
  
    if(!mysql_real_connect(mysql,dbhost,dbuser,dbpasswd,dbname,3306,NULL,0))
    { /*连接一个MySQL服务器*/
        printf("%s",mysql_error(mysql));
        printf("\n连接服务器失败,请联系系统管理人员!\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)) /*执行指定为一个空结尾的字符串的SQL查询*/
    {
        printf("%s",mysql_error(mysql));
        printf("\nmysql_query出错!\n");
        mysql_close(mysql);
        exit(0);
    }
    mysql_res=mysql_store_result(mysql); /*检索一个完整的结果集合给客户*/
    rows=mysql_num_rows(mysql_res); /*返回一个结果集合重的列的数量*/
    if(rows==0) /*此管理码不存在 返回*/
    {
        printf("\n返回值为空\n");
        mysql_free_result(mysql_res); /*释放一个结果集合使用的内存*/
        mysql_close(mysql); /*关闭一个服务器连接*/
        exit(0);
    }
    else
    {
        num_fields = mysql_num_fields(mysql_res);
        for(i = 0; i < num_fields; i++)/* 打印字段名称 */
        {
            field = mysql_fetch_field_direct(mysql_res, i);          
            printf("%s\t", field->name);
        }
       
       
        for(count=0; count<rows; count++)/* 打印字段的值 */   
        {   
            mysql_row=mysql_fetch_row(mysql_res); /*从结果集合中取得下一行*/                   
            for(i = 0; i < num_fields; i++)
            {
                printf("%s\t",mysql_row[i]);
            }
            printf("\n");
        }
       
    }

#if 0
    for(count=0; count<rows; count++)
    {
        mysql_row=mysql_fetch_row(mysql_res); /*从结果集合中取得下一行*/
        printf("用户ID:%s\t",mysql_row[0]);
        printf("用户名:%s\t",mysql_row[2]);
        printf("真实姓名:%s\t",mysql_row[4]);
        printf("昵称:%s\t",mysql_row[5]);
        printf("职务:%s\t",mysql_row[7]);
        printf("电话:%s\n",mysql_row[8]);
    }
#endif
   
    mysql_free_result(mysql_res);
    mysql_close(mysql);
    printf("\nApp exit!!!\n");
}

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

Makefile 文件内容如下:

#=======================================================================
# Makefile template write by albert.shi
# 2011-06-03 21:09
# Shanghai
#=======================================================================

# Standard defines:
CC      =    gcc

WRES    =    windres

HOMEV    =    /usr/src/linux-2.6.32.21
VPATH    =    $(HOMEV)/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 -DUNIV_LINUX -Wl,-Bsymbolic-functions

#C_FLAGS    = -O -I/usr/include/mysql -DBIG_JOINS=1 -fno-strict-aliasing -DUNIV_LINUX -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 $@ $<

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

相关内容