http://curran.blog.51cto.com/2788306/533169
今天给大家演示在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服务状态:
启动服务:
连接数据库:
2:安装gcc工具包
rpm-ivhgcc*--force--nodeps强制安装
3:进入mysql
createtablet1(idint,namevarchar(30));
t1表插入数据:
1.cpp
#include
#include
#include
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
#include
#include
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//
>createtriggertr1afterinsertont1foreachrow
>begin
>insertintot2(id,name)values(new.id,new.name);
>end;
>//
>deletefromt1whereid=66;
>//
>delimeter;
vi3.cpp
#include
#include
#include
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
#include
#include
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<
mysql_close(&mysql);
}
g++-o4.out4.cpp-lmysqlclient-I/usr/include/mysql/-L/usr/lib/mysql
./4.out
查处字段的总数:
vi5.cpp
#include
#include
#include
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<
intfieldcount=mysql_num_fields(result);
cout<
mysql_close(&mysql);
}
g++-o5.out5.cpp-lmysqlclient-I/usr/include/mysql/-L/usr/lib/mysql
./5.out
列出具体字段名:
vi6.cpp
#include
#include
#include
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<
intfieldcount=mysql_num_fields(result);
cout<
for(inti=0;i
{
field=mysql_fetch_field_direct(result,i);
cout<name<<"\t\t";
}
cout<
mysql_close(&mysql);
}
g++-o6.out6.cpp-lmysqlclient-I/usr/include/mysql/-L/usr/lib/mysql
./6.out
显示表中所有数据:
vi7.cpp
#include
#include
#include
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<
intfieldcount=mysql_num_fields(result);
cout<
for(inti=0;i
{
field=mysql_fetch_field_direct(result,i);
cout<name<<"\t\t";
}
cout<
MYSQL_ROWrow=NULL;
row=mysql_fetch_row(result);
while(NULL!=row)
{
for(inti=1;i
{
cout<"\t\t";
}
cout<
row=mysql_fetch_row(result);
}
mysql_close(&mysql);
}
g++-o7.out7.cpp-lmysqlclient-I/usr/include/mysql/-L/usr/lib/mysql
./out
本文出自“Art_Hero”博客,请务必保留此出处http://curran.blog.51cto.com/2788306/533169
阅读(2126) | 评论(0) | 转发(0) |