錯(cuò)誤提示:Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
解決方法:在/var/run/ 下面創(chuàng)建一個(gè) mysqld目錄,然后建立一個(gè)鏈接指向:/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; /*表示對一個(gè)數(shù)據(jù)庫連接的句柄*/
MYSQL_RES *mysql_res; /*代表返回行的一個(gè)查詢的結(jié)果*/
MYSQL_ROW mysql_row; /*字符串?dāng)?shù)組*/
MYSQL_FIELD *field;
my_ulonglong rows;
int i,num_fields;
/*該類型用于行編號(hào)和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))) /*獲得或初始化一個(gè)MYSQL結(jié)構(gòu)*/
{
printf("mysql_init失敗!");
mysql_close(mysql);
exit(0);
}
if(!mysql_real_connect(mysql,dbhost,dbuser,dbpasswd,dbname,3306,NULL,0))
{ /*連接一個(gè)MySQL服務(wù)器*/
printf("%s",mysql_error(mysql));
printf("\n連接服務(wù)器失敗,請聯(lián)系系統(tǒng)管理人員!\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)) /*執(zhí)行指定為一個(gè)空結(jié)尾的字符串的SQL查詢*/
{
printf("%s",mysql_error(mysql));
printf("\nmysql_query出錯(cuò)!\n");
mysql_close(mysql);
exit(0);
}
mysql_res=mysql_store_result(mysql); /*檢索一個(gè)完整的結(jié)果集合給客戶*/
rows=mysql_num_rows(mysql_res); /*返回一個(gè)結(jié)果集合重的列的數(shù)量*/
if(rows==0) /*此管理碼不存在 返回*/
{
printf("\n返回值為空\n");
mysql_free_result(mysql_res); /*釋放一個(gè)結(jié)果集合使用的內(nèi)存*/
mysql_close(mysql); /*關(guān)閉一個(gè)服務(wù)器連接*/
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); /*從結(jié)果集合中取得下一行*/
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); /*從結(jié)果集合中取得下一行*/
printf("用戶ID:%s\t",mysql_row[0]);
printf("用戶名:%s\t",mysql_row[2]);
printf("真實(shí)姓名:%s\t",mysql_row[4]);
printf("昵稱:%s\t",mysql_row[5]);
printf("職務(wù):%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 文件內(nèi)容如下:
#=======================================================================
# 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 $@ $<
#--------------------------------------------------------
本文出自:億恩科技【mszdt.com】
服務(wù)器租用/服務(wù)器托管中國五強(qiáng)!虛擬主機(jī)域名注冊頂級提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM]
|