c语言提供了连接mysql的一些接口,可以实现增查改删等操作,主要有以下:
头文件 <mysql.h>
MYSQL //句柄
MYSQL * mysql_init(MYSQL *mysql); //初始化一个MYSQL连接的实例对象,如果mysql不为空,返回该对象地址。
void mysql_close(MYSQL *sock); //释放一个MYSQL连接实例
MYSQL *mysql_real_connect (
MYSQL *mysql,
const char *host,
const char *user,
const char *passwd,
const char *db,
unsigned int port,
const char *unix_socket,
unsigned long client_flag); // 连接请求
int mysql_query(MYSQL *mysql, const char *stmt_str); //命令执行
MYSQL_RES *mysql_store_result(MYSQL *mysql); //储存结果集
MYSQL_ROW mysql_fetch_row(MYSQL_RES *result); //检索一个结果集合的下一行。当在mysql_store_result()之后使用时,如果没有更多的行可检索时,mysql_fetch_row()返回NULL。
int mysql_num_fields(MYSQL_RES *result); //函数返回结果集中字段的数。
void mysql_free_result(MYSQL_RES *result); //释放结果集内存。
const char* mysql_error(MYSQL* mysql); //返回上一个 MySQL 操作产生的文本错误信息。
执行流程:
下面写一个例程,展示mysql的select和insert操作,delete和update方法类似,就不放了:
#include <iostream>
#include <mysql.h>
#include <stdlib.h>
using namespace std;
class stu_system{
public:
stu_system(int id):Getid(id),flag(0),row(0),result(NULL) {
i_query = new char[100];
this->init();
}
~stu_system(){
//别忘了释放result,关闭sock
mysql_free_result(result);
mysql_close(sock);
delete []i_query;
}
public:
void init() {
if (mysql_init(&mysql) == NULL) { //初始化
cout<<"初始化失败。"<<endl;
exit(0);
}
cout<<"初始化成功!"<<endl;
if ((sock = mysql_real_connect(&mysql, //连接mysql
host,
user,
passwd,
db,
port,
unix_socket,
client_flag)) == NULL)
{
cout<<"连接失败,原因是: "<<mysql_error(&mysql)<<endl;
exit(0);
}
else {
cout<<"连接数据库成功......"<<endl<<endl;
}
}
bool cmd_executive(){
if ((mysql_query(&mysql,i_query)) != 0){ //进行语句执行
cout<<"语句执行失败!"<<endl;
return 0;
}
return 1;
}
bool Get_Result(){
if ((result = mysql_store_result(&mysql)) == NULL){ //储存结果集,成功就返回一个结果集
cout<<"保存结果集合失败!"<<endl;
return 0;
}
return 1;
}
bool Get_Row(){
if((row = mysql_fetch_row(result)) == NULL){
cout<<"查询结果为空!"<<endl;
return 0;
}
else{
cout<<"查询结果有"<<mysql_num_fields(result)<<"条,";
// while((row = mysql_fetch_row(result)) != NULL){
cout<<"个人信息结果如下:"<<endl;
cout<< "姓名: "<<row[0]<<endl
<< "性别: "<<row[1]<<endl
<< "班级: "<<row[2]<<endl
<< "得分: "<<row[3]<<endl
<< "奖牌: "<<row[4]<<endl;
// }
return 1;
}
}
//查
void search(){
sprintf(i_query,"select t4_basic.name,t4_basic.sex,t4_basic.class,t4_other.score,t4_other.medals from t4_basic left join t4_other on t4_basic.id=t4_other.id where t4_other.id=%d",Getid);
if (this->cmd_executive()){
if (this->Get_Result()){
this->Get_Row();
}
}
}
//增
void insert(){
char * name = new char[10];
int id;
char sex;
int Class;
int score;
int medals;
cout<<"请输入id号,姓名,性别,班级,得分,奖牌: "<<endl;
cin>>id>>name>>sex>>Class>>score>>medals;
sprintf(i_query,"insert into t4_basic values('%s',%d,'%c',%d)",name,id,sex,Class);
if (this->cmd_executive() == 0){
return;
}
sprintf(i_query,"insert into t4_other values(%d,%d,%d)",score,medals,id);
if (this->cmd_executive()){
cout<<"信息录入成功!"<<endl;
}
delete []name;
}
private:
MYSQL mysql,*sock;
const char *host = "localhost"; //待连接的IP地址,如果在本地可以写localhost
const char *user = "jimmy"; //待连接的用户名,也可以用root
const char *passwd = "ilkb15766648564"; //用户密码,登录mysql的密码
const char *db = "test04"; //待连接的数据库,注意写对
unsigned int port = 3306; //固定端口
const char *unix_socket = NULL; //默认参数
unsigned long client_flag = 0; //默认参数设置为0
MYSQL_ROW row; //结果集合中的 “行”
MYSQL_RES *result; //保存查询结果集合
char *i_query;
int flag;
int Getid;
};
int main(){
int doWhat;
int Getid;
cout<<"请输入你的id:";
cin>>Getid;
stu_system a(Getid);
cout<<"输入: 1.查询个人信息; 2.修改个人信息; 3.删除个人信息; 4.增加学生信息。"<<endl;
cin>>doWhat;
switch(doWhat) {
case 1: a.search(); break;
case 2: a.update(); break;
case 3: a.delet(); break;
case 4: a.insert(); break;
default: cout<<"输入有误,请重新输入: ";
cin>>doWhat;
}
return 0;
}
makefile
all:demo.out
demo.out:mysqldemo.cpp
g++ -I/usr/include/mysql mysqldemo.cpp -L/usr/lib/mysql -L/usr/lib64/mysql -lmysqlclient -o demo.out
clean:
rm -rf demo.out
查:
增:
mysql