程序在网上一搜一大摞,我也是借鉴别人写的。
不过要注意的是,如果mysql/mysql.h这个引用出现问题时,说明lib没有安装上,那么要安装上
在终端执行指令:sudo apt-get install libmysql15*
编译时使用的指令:gcc -l mysqlclient -o HelloTest HelloTest.cpp
使用makefile文件进行编译的话(在eclipse开发环境下)
CXXFLAGS = -O2 -g -Wall -fmessage-length=0 -l
OBJS = HelloTest.o
LIBS = mysqlclient
TARGET = HelloTest
$(TARGET): $(OBJS)
$(CXX) -o $(TARGET) $(OBJS) $(LIBS)
all: $(TARGET)
clean:
rm -f $(OBJS) $(TARGET)
//============================================================================
// Name : HelloTest.cpp
// Author : longkun.wyb
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C, Ansi-style
//============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
#include <string.h>
int main(void) {
int t,r;
char *query;
MYSQL *conn_ptr;
MYSQL_RES *res;
MYSQL_ROW row;
//puts("Hello World!!!");
conn_ptr = mysql_init(NULL);
mysql_real_connect(conn_ptr,"","root","root","test",0,NULL,0);
query = "select * from logo";
t = mysql_real_query(conn_ptr,query,(unsigned int)strlen(query));
printf("%d\n",t);
res = mysql_use_result(conn_ptr);
for(r=0;r<=mysql_field_count(conn_ptr);++r){
row=mysql_fetch_row(res);
if(row<0){
break;
}
for(t=0;t<mysql_num_fields(res);++t){
printf("%s\n",row[t]);
}
printf("\n");
}
mysql_free_result(res);
mysql_close(conn_ptr);
return EXIT_SUCCESS;
}