今天给大家演示在linux下如果用C++操作mysql
1:安装MySQL
挂载光盘:
mkdir/cdrom
mount/dev/hdc/cdrom
cd/cdrom/Server
rpm-ivhperl-DBI-1.52-2.el5.i386.rpm
rpm-ivhmysql-5.0.45-7.el5.i386.rpmmysql-bench-5.0.45-7.el5.i386.rpmmysql-devel-5.0.45-7.el5.i386.rpm
rpm-ivhperl-DBD-MySQL-3.0007-2.el5.i386.rpm
rpm-ivhmysql-server-5.0.45-7.el5.i386.rpmmysql-test-5.0.45-7.el5.i386.rpm
查看mysql服务状态:
servicemysqldstatus
启动服务:
servicemysqldstart
连接数据库:
mysql
2:安装gcc工具包
rpm-ivhgcc*--force--nodeps强制安装
3:进入mysql
createtablet1(id
int,namevarchar(30));
t1表插入数据:
1.cpp
#include<iostream>
#include<mysql/mysql.h>
#include<string>
usingnamespacestd;
main()
{
MYSQLmysql;
mysql_init(&mysql);
mysql_real_connect(&mysql,
"localhost",
"root",
"root",
"test",3306,NULL,0);
stringsql=
"insertintot1(id,name)values(1,'java1');";
mysql_query(&mysql,sql.c_str());
mysql_close(&mysql);
}
g++-o1.out1.cpp-lmysqlclient-I/usr/include/mysql/-L/usr/lib/mysql
更新mysql数据;
1.cpp
#include<iostream>
#include<mysql/mysql.h>
#include<string>
usingnamespacestd;
main()
{
MYSQLmysql;
mysql_init(&mysql);
mysql_real_connect(&mysql,
"localhost",
"root",
"root",
"test",3306,NULL,0);
stringsql=
"insertintot1(id,name)values(2,'java2'),(3,'java3');";
mysql_query(&mysql,sql.c_str());
sql=
"updatet1setname='java33'whereid=3;";
mysql_query(&mysql,sql.c_str());
mysql_close(&mysql);
}
g++-o1.out1.cpp-lmysqlclient-I/usr/include/mysql/-L/usr/lib/mysql
./1.out
mysql存储过程:
mysql>delimiter
//
>createprocedurep01()
>begin
>insertintot1(id,name)values(66,'java66');
>end;
>
//
应用存储过程:
mysql>delimiter
//
>createprocedurep01()
>begin
>insertintot1(id,name)values(66,'java66');
>end;
>
//
触发器:
新建t2表:
mysql>delimiter
//
>createtriggertr1afterinsertont1
foreachrow
>begin
>insertintot2(id,name)values(
new.id,
new.name);
>end;
>
//
>deletefromt1whereid=66;
>
//
>delimeter;
vi3.cpp
#include<iostream>
#include<string>
#include<mysql/mysql.h>
usingnamespacestd;
main()
{
MYSQLmysql;
mysql_init(&mysql);
mysql_real_connect(&mysql,
"localhost",
"root",
"root",3306,NULL,0);
stringsql=
"callp01();";
mysql_query(&mysql,sql.c_str());
mysql_close(&mysql);
}
g++-o3.out3.cpp-lmysqlclient-I/usr/include/mysql/-L/usr/lib/mysql
./3.out
mysql>delimeter;
>select*fromt1;
>select*fromt2;
>存在数据(66,'java66')
查看表中数据的总数:
vi4.cpp
#include<iostream>
#include<string>
#include<mysql/mysql.h>
usingnamespacestd;
main()
{
MYSQLmysql;
MYSQL_RES*result=NULL;
mysql_init(&mysql);
mysql_real_connect(&mysql,
"localhost",
"root",
"root",
"test",3306,NULL,0);
stringsql=
"selectid,namefromt1;";
mysql_query(&mysql,sql.c_str());
result=mysql_store_result(&mysql);
introwcount=mysql_num_rows(result);
cout<<rowcount<<endl;
mysql_close(&mysql);
}
g++-o4.out4.cpp-lmysqlclient-I/usr/include/mysql/-L/usr/lib/mysql
./4.out
查处字段的总数:
vi5.cpp
#include<iostream>
#include<string>
#include<mysql/mysql.h>
usingnamespacestd;
main()
{
MYSQLmysql;
MYSQL_RES*result=NULL;
mysql_init(&mysql);
mysql_real_connect(&mysql,
"localhost",
"root",
"root",3306,NULL,0);
stringstr=
"selectid,namefromt1;";
mysql_query(&mysql,sql.c_str());
result=mysql_store_result(&mysql);
introwcount=mysql_num_rows(result);
cout<<rowcount<<endl;
intfieldcount=mysql_num_fields(result);
cout<<fieldcount<<endl;
mysql_close(&mysql);
}
g++-o5.out5.cpp-lmysqlclient-I/usr/include/mysql/-L/usr/lib/mysql
./5.out
列出具体字段名:
vi6.cpp
#include<iostream>
#include<string>
#include<mysql/mysql.h>
usingnamespacestd;
main()
{
MYSQLmysql;
MYSQL_RES*result=NULL;
MYSQL_FIELD*field=NULL;
mysql_init(&mysql);
mysql_real_connect(&mysql,
"localhost",
"root",
"root",
"test",3306,NULL,0);
stringstr=
"selectid,namefromt1;";
mysql_query(&mysql,sql.c_str());
result=mysql_store_result(&mysql);
introwcount=mysql_num_rows(result);
cout<<rowcount<<endl;
intfieldcount=mysql_num_fields(result);
cout<<fieldcount<<endl;
for(
inti=0;i<fieldcount;i++)
{
field=mysql_fetch_field_direct(result,i);
cout<<field->name<<
"\t\t";
}
cout<<endl;
mysql_close(&mysql);
}
g++-o6.out6.cpp-lmysqlclient-I/usr/include/mysql/-L/usr/lib/mysql
./6.out
显示表中所有数据:
vi7.cpp
#include<iostream>
#include<string>
#include<mysql/mysql.h>
usingnamespacestd;
main()
{
MYSQLmysql;
MYSQL_RES*result=NULL;
MYSQL_FIELD*field=NULL;
mysql_init(&mysql);
mysql_real_connect(&mysql,
"localhost",
"root",
"root",3306,NULL,0);
stringstr=
"selectid,namefromt1;";
mysql_query(&mysql,sql.c_str());
result=mysql_store_result(&mysql);
introwcount=mysql_num_rows(result);
cout<<rowcount<<endl;
intfieldcount=mysql_num_fields(result);
cout<<fieldcount<<endl;
for(
inti=0;i<fieldcount;i++)
{
field=mysql_fetch_field_direct(result,i);
cout<<field->name<<
"\t\t";
}
cout<<endl;
MYSQL_ROWrow=NULL;
row=mysql_fetch_row(result);
while(NULL!=row)
{
for(
inti=1;i<fieldcount;i++)
{
cout<<row[i]<<
"\t\t";
}
cout<<endl;
row=mysql_fetch_row(result);
}
mysql_close(&mysql);
}
g++-o7.out7.cpp-lmysqlclient-I/usr/include/mysql/-L/usr/lib/mysql
./out

本文出自“”博客,请务必保留此出处