日期:2014-05-16  浏览次数:20615 次

linux c/c++ 操作数据库 mysql 和 oracle
如题,想在linux用c/c++ 操作 mysql,寻求建议和指导

------解决方案--------------------
搜一下嘛

http://blog.chinaunix.net/u3/93926/showart_1872453.html
http://blog.sina.com.cn/s/blog_606c49090100frat.html
------解决方案--------------------
哦,没做过!!!
------解决方案--------------------
我昨天写了1100多行C操作Mysql的代码,在我的Ubuntu Linux下可以运行,但是代码很烂 goto语句太多实在是不好意思拿出来。你要的话,跟我联系一下。我的邮箱地址:zxywd@yahoo.com,zxy__11@163.com。我还是写一些简单的代码给你看一下:
connect.c 连接数据库

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <mysql/mysql.h>
#include <syslog.h>

MYSQL mysql;
int main()
{
char *host="localhost";
char *user=malloc(30);
char *password=malloc(30);
char *db=malloc(30);
db=NULL;
printf("请输入用户名:\n");
scanf("%s",user);
printf("请输入密码:\n");
scanf("%s",password);
if(mysql_init(&mysql)==NULL)
{
printf("Init mysql error!\n");
return -1;
}
if(mysql_real_connect(&mysql,host,user,password,db,0,NULL,0)==NULL)
{
printf("connect to mysql Error:%s!",mysql_error(&mysql));
return -1;
}
printf("登录成功!\n");
return 0;
}

编译:cc -o connect $(mysql_config --cflags) connect.c $(mysql_config --libs)

连接数据库之前一定要用mysql_init(&mysql)将数据库初始化。编译的时候一定要注意空格。