当前位置: 技术问答>linux和unix
linux下用c如何连接mysql
来源: 互联网 发布时间:2016-04-08
本文导语: 我在linux下连接mysql但是编译不过去。 代码如下: #include #include #include #include MYSQL mysql; void doQuery() { MYSQL_ROW m_row; MYSQL_RES *m_res; char sql[1024]; sprintf(sql,"select count(*) from my_ta...
我在linux下连接mysql但是编译不过去。
代码如下:
#include
#include
#include
#include
MYSQL mysql;
void doQuery()
{
MYSQL_ROW m_row;
MYSQL_RES *m_res;
char sql[1024];
sprintf(sql,"select count(*) from my_table");
if(mysql_query(&mysql,sql) != 0)
{
fprintf(stderr, "mysql_query err: %s",mysql_error(&mysql));
}
m_res = mysql_store_result(&mysql);
if(m_res==NULL)
{
fprintf(stderr, "get result err: %s",mysql_error(&mysql));
}
if(m_row = mysql_fetch_row(m_res))
{
printf("count(*) is %d!n",atoi(m_row[0]));
}
mysql_free_result(m_res);
}
int main()
{
char host[32] = "127.0.0.1";
char user[32] = "2008";
char passwd[32] = "2008";
char db[32] = "tra";
if( mysql_init(&mysql) == NULL )
{
fprintf(stderr,"Init mysql err!");
return -1;
}
if (mysql_real_connect(&mysql,host,user,passwd,db,0,NULL,0) == NULL)
{
fprintf(stderr,"Connect to mysql Error:%s!",mysql_error(&mysql));
return -1;
}
else
{
puts("Connect to mysql success!");
}
doQuery();
mysql_close(&mysql);
return 0;
}
但是我用命令 gcc -o test test.c 编译时有如下错误:
test.c:4:25: error: mysql/mysql.h: No such file or directory
test.c:7: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'mysql'
test.c: In function 'doQuery':
test.c:11: error: 'MYSQL_ROW' undeclared (first use in this function)
test.c:11: error: (Each undeclared identifier is reported only once
test.c:11: error: for each function it appears in.)
test.c:11: error: expected ';' before 'm_row'
test.c:12: error: 'MYSQL_RES' undeclared (first use in this function)
test.c:12: error: 'm_res' undeclared (first use in this function)
test.c:16: error: 'mysql' undeclared (first use in this function)
test.c:25: error: 'm_row' undeclared (first use in this function)
test.c: In function 'main':
test.c:39: error: 'mysql' undeclared (first use in this function)
test.c:39: warning: comparison between pointer and integer
test.c:44: warning: comparison between pointer and integer
test.c:57:2: warning: no newline at end of file
这个是说找不到mysql.h引起的错误吧?
为什么会有这个错误?如何解决?
刚刚接触到Linux,请大家多帮忙,谢谢了!
代码如下:
#include
#include
#include
#include
MYSQL mysql;
void doQuery()
{
MYSQL_ROW m_row;
MYSQL_RES *m_res;
char sql[1024];
sprintf(sql,"select count(*) from my_table");
if(mysql_query(&mysql,sql) != 0)
{
fprintf(stderr, "mysql_query err: %s",mysql_error(&mysql));
}
m_res = mysql_store_result(&mysql);
if(m_res==NULL)
{
fprintf(stderr, "get result err: %s",mysql_error(&mysql));
}
if(m_row = mysql_fetch_row(m_res))
{
printf("count(*) is %d!n",atoi(m_row[0]));
}
mysql_free_result(m_res);
}
int main()
{
char host[32] = "127.0.0.1";
char user[32] = "2008";
char passwd[32] = "2008";
char db[32] = "tra";
if( mysql_init(&mysql) == NULL )
{
fprintf(stderr,"Init mysql err!");
return -1;
}
if (mysql_real_connect(&mysql,host,user,passwd,db,0,NULL,0) == NULL)
{
fprintf(stderr,"Connect to mysql Error:%s!",mysql_error(&mysql));
return -1;
}
else
{
puts("Connect to mysql success!");
}
doQuery();
mysql_close(&mysql);
return 0;
}
但是我用命令 gcc -o test test.c 编译时有如下错误:
test.c:4:25: error: mysql/mysql.h: No such file or directory
test.c:7: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'mysql'
test.c: In function 'doQuery':
test.c:11: error: 'MYSQL_ROW' undeclared (first use in this function)
test.c:11: error: (Each undeclared identifier is reported only once
test.c:11: error: for each function it appears in.)
test.c:11: error: expected ';' before 'm_row'
test.c:12: error: 'MYSQL_RES' undeclared (first use in this function)
test.c:12: error: 'm_res' undeclared (first use in this function)
test.c:16: error: 'mysql' undeclared (first use in this function)
test.c:25: error: 'm_row' undeclared (first use in this function)
test.c: In function 'main':
test.c:39: error: 'mysql' undeclared (first use in this function)
test.c:39: warning: comparison between pointer and integer
test.c:44: warning: comparison between pointer and integer
test.c:57:2: warning: no newline at end of file
这个是说找不到mysql.h引起的错误吧?
为什么会有这个错误?如何解决?
刚刚接触到Linux,请大家多帮忙,谢谢了!
|
添加mysql链接库
gcc -o test test.c -L/usr/include/mysql -lmysqlclient
如果没记错的话应该是这样写的。
gcc -o test test.c -L/usr/include/mysql -lmysqlclient
如果没记错的话应该是这样写的。
|
最好写个Makefile来维护,参考如下:
C= gcc
CFLAGS= -c -g -pthread -D_REENTRANT -D`uname -s` -Wall
LDFLAGS= -g -lpthread
CDBFLAGS= `mysql_config --cflags`
LDDBFLAGS = `mysql_config --libs_r`
target = ex_8_6
all: $(target)
target_objects=$(target).o
$(target) :$(target_objects)
$(CC) $(LDFLAGS) $(LDDBFLAGS) -o $@ $^
@echo $@ Build OK.
.SUFFIXES:.cpp .o
.c.o :
$(CC) $(CFLAGS) $(CDBFLAGS) -o $@ $<
clean:
@rm -rf $(target) $(target_objects)
把target 换成你要生成的文件。
要在多线程的环境下安全使用mysql C 客户端,动态SQL,可以关注我的书.
班门弄斧之作已经发行,各大书店有售(http://www.khp.com.cn/books/detail.asp?bookid=1707)
C= gcc
CFLAGS= -c -g -pthread -D_REENTRANT -D`uname -s` -Wall
LDFLAGS= -g -lpthread
CDBFLAGS= `mysql_config --cflags`
LDDBFLAGS = `mysql_config --libs_r`
target = ex_8_6
all: $(target)
target_objects=$(target).o
$(target) :$(target_objects)
$(CC) $(LDFLAGS) $(LDDBFLAGS) -o $@ $^
@echo $@ Build OK.
.SUFFIXES:.cpp .o
.c.o :
$(CC) $(CFLAGS) $(CDBFLAGS) -o $@ $<
clean:
@rm -rf $(target) $(target_objects)
把target 换成你要生成的文件。
要在多线程的环境下安全使用mysql C 客户端,动态SQL,可以关注我的书.
班门弄斧之作已经发行,各大书店有售(http://www.khp.com.cn/books/detail.asp?bookid=1707)