在Linux下通過C語言訪問MySQL資料庫的方法

來源:互聯網
上載者:User

錯誤提示: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 $@ $<

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.