Linux 连接数据库 mysql.h出错
2011-10-09 10:34
330 查看
程序在网上一搜一大摞,我也是借鉴别人写的。
不过要注意的是,如果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;
}
不过要注意的是,如果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;
}
相关文章推荐
- MySQL 数据库远程连接 数据加密 Linux
- 阿里云服务器Linux环境下设置mysql支持远程连接数据库
- Linux下安装了MySQL之后,无法用navicat连接数据库?
- 在Linux下面使用Mysql的客户端工具WorkBench创建数据库和用户并连接
- mac QT5连接mysql出错 Qt配制数据库驱动问题
- Linux/CentOS下开启MySQL远程连接,远程管理数据库
- Linux 下 C 语言连接 MySQL 数据库 / 西安嵌入式培训
- mysql_real_connect()数据库连接出错
- Linux/CentOS下开启MySQL远程连接,远程管理数据库
- linux 下开启Mysql服务,连接数据库
- Mysql远程连接失败原因(数据库在linux;持续更新中。。。)
- linux c++编写访问mysql程序,访问数据库出错,解决方法
- linux下用tsql连接mssql数据库出错
- MySQL连接数据库:C API代码, Linux,Windows
- 在万网虚拟主机上连接数据库出错mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication
- MySQLdb 连接Mysql 数据库出错解决
- linux安装mysql和使用c语言操作数据库的方法 c语言连接mysql
- linux 中JDK环境变量不一致问题导致mysql无法连接,总是告诉权限出错
- Linux环境下设置mysql支持远程连接数据库(转载)
- linux下 java 通过jdbc 连接数据库mysql