当前位置: 移动技术网 > IT编程>开发语言>C/C++ > 采用C++封装MySQL提供的常用库函数,实现对MySQL数据库的访问

采用C++封装MySQL提供的常用库函数,实现对MySQL数据库的访问

2018年09月14日  | 移动技术网IT编程  | 我要评论

采用c++封装mysql提供的常用库函数,实现对mysql的访问。

版本历史

freemysqlv1.0.0

1、创建表的示例程序createtable.cpp

//

//本程序演示创建一个表,用于存放商品信息。

//

#include"freemysql.h"

intmain(intargc,char*argv[])

{

//数据库连接池

connectionconn;

//sql语言操作类

sqlstatementstmt;

//连接数据库,返回值0-成功,其它-失败

//失败代码在conn.m_cda.rc中,失败描述在conn.m_cda.message中。

if(conn.connecttodb("120.77.115.3,szidc,szmb1601,lxqx,3306")!=0)

{

printf("connectdatabasefailed.\n%s\n",conn.m_cda.message);

exit(-1);

}

//设置字符集为'gbk',与my的字符集要相同,否则中文会出现乱码

conn.character("gbk");

//为sqlstatement指定数据库连接池,不需要判断返回值

stmt.connect(&conn);

//为sqlstatement指定数据库连接池,不需要判断返回值

stmt.connect(&conn);

//准备创建表的sql,商品表:商品编号id,商品名称name,价格sal

//入库时间btime,商品说明memo,商品图片pic

//prepare方法不需要判断返回值

stmt.prepare("\

createtablegoods(idbigint(10),\

namevarchar(30),\

saldecimal(8,2),\

btimedatetime,\

memolongtext,\

piclongblob,\

primarykey(id))");

//执行sql语句,一定要判断返回值,0-成功,其它-失败。

if(stmt.execute()!=0)

{

printf("stmt.execute()failed.\n%s\n%s\n",stmt.m_sql,stmt.m_cda.message);

exit(-1);

}

printf("createtablegoodsok.\n");

exit(0);

}

2、向表中插入记录的示例程序inserttable.cpp

//

//本程序演示向商品表中插入10条记录。

//

#include"freemysql.h"

//定义用于操作数据的结构,与表中的字段对应

structst_goods

{

longid;//商品编号

charname[31];//商品名称

doublesal;//商品价格

charbtime[20];//入库时间

}stgoods;

intmain(intargc,char*argv[])

{

//数据库连接池

connectionconn;

//sql语言操作类

sqlstatementstmt;

//连接数据库,返回值0-成功,其它-失败

//失败代码在conn.m_cda.rc中,失败描述在conn.m_cda.message中。

if(conn.connecttodb("120.77.115.3,szidc,szmb1601,lxqx,3306")!=0)

{

printf("connectdatabasefailed.\n%s\n",conn.m_cda.message);

exit(-1);

}

//设置字符集为'gbk',与mysql数据库的字符集要相同,否则中文会出现乱码

conn.character("gbk");

//为sqlstatement指定数据库连接池,不需要判断返回值

stmt.connect(&conn);

//准备插入数据的sql,不需要判断返回值

stmt.prepare("\

insertintogoods(id,name,sal,btime)\

values(,,,str_to_date(,'%%y-%%m-%%d%%h:%%i:%%s'))");

//为sql语句绑定输入变量的地址

stmt.bindin(1,&stgoods.id);

stmt.bindin(2,stgoods.name,30);

stmt.bindin(3,&stgoods.sal);

stmt.bindin(4,stgoods.btime,19);

//模拟商品数据,向表中插入10条测试信息

for(intii=1;ii<=10;ii++)

{

//结构体变量初始化

memset(&stgoods,0,sizeof(stgoods));

//为结构体的变量指定值

stgoods.id=ii;

sprintf(stgoods.name,"商品名称%02d",ii);

stgoods.sal=ii*2.11;

strcpy(stgoods.btime,"2018-03-0112:25:31");

//每次指定变量的值后,执行sql语句,一定要判断返回值,0-成功,其它-失败。

if(stmt.execute()!=0)

{

printf("stmt.execute()failed.\n%s\n%s\n",stmt.m_sql,stmt.m_cda.message);

exit(-1);

}

printf("insertok(id=%d).\n",ii);

}

printf("inserttablegoodsok.\n");

//提交数据库事务

conn.commit();

exit(0);

}

3、查询表中记录的示例程序selecttable.cpp

//

//本程序演示从商品表中查询数据

//

#include"freemysql.h"

//定义用于查询数据的结构,与表中的字段对应

structst_goods

{

longid;//商品编号

charname[31];//商品名称

doublesal;//商品价格

charbtime[20];//入库时间

}stgoods;

intmain(intargc,char*argv[])

{

//数据库连接池

connectionconn;

//sql语言操作类

sqlstatementstmt;

//连接数据库,返回值0-成功,其它-失败

//失败代码在conn.m_cda.rc中,失败描述在conn.m_cda.message中。

if(conn.connecttodb((char*)"120.77.115.3,szidc,szmb1601,lxqx,3306")!=0)

{

printf("connectdatabasefailed.\n%s\n",conn.m_cda.message);

exit(-1);

}

//设置字符集为'gbk',与mysql数据库的字符集要相同,否则中文会出现乱码

conn.character((char*)"gbk");

//为sqlstatement指定数据库连接池,不需要判断返回值

stmt.connect(&conn);

intiminid,imaxid;

//准备查询数据的sql,不需要判断返回值

stmt.prepare("\

selectid,name,sal,date_format(btime,'%%y-%%m-%%d%%h:%%i:%%s')\

fromgoodswhereid>:1andid<:2");

//为sql语句绑定输入变量的地址

stmt.bindin(1,&iminid);

stmt.bindin(2,&imaxid);

//为sql语句绑定输出变量的地址

stmt.bindout(1,&stgoods.id);

stmt.bindout(2,stgoods.name,30);

stmt.bindout(3,&stgoods.sal);

stmt.bindout(4,stgoods.btime,19);

//手工指定id的范围为1到8,执行一次查询

iminid=1;

imaxid=8;

//执行sql语句,一定要判断返回值,0-成功,其它-失败。

if(stmt.execute()!=0)

{

printf("stmt.execute()failed.\n%s\n%s\n",stmt.m_sql,stmt.m_cda.message);

exit(-1);

}

while(1)

{

//先把结构体变量初始化,然后才获取记录

memset(&stgoods,0,sizeof(stgoods));

//获取一条记录,一定要判断返回值,0-成功,其它-无记录

if(stmt.next()!=0)break;

//把获取到的记录的值打印出来

printf("id=%ld,name=%s,sal=%.02f,btime=%s\n",stgoods.id,stgoods.name,stgoods.sal,stgoods.btime);

}

//请注意,stmt.m_cda.rpc变量非常重要,它保存了sql被执行后影响的记录数。

printf("本次查询了goods表%ld条记录。\n",stmt.m_cda.rpc);

exit(0);

}

4、更新表中记录的示例程序updatetable.cpp

//

//本程序演示更新商品表中数据

//

#include"freemysql.h"

intmain(intargc,char*argv[])

{

//数据库连接池

connectionconn;

//sql语言操作类

sqlstatementstmt;

//连接数据库,返回值0-成功,其它-失败

//失败代码在conn.m_cda.rc中,失败描述在conn.m_cda.message中。

if(conn.connecttodb("120.77.115.3,szidc,szmb1601,lxqx,3306")!=0)

{

printf("connectdatabasefailed.\n%s\n",conn.m_cda.message);exit(-1);

}

//设置字符集为'gbk',与mysql数据库的字符集要相同,否则中文会出现乱码

conn.character("gbk");

//为sqlstatement指定数据库连接池,不需要判断返回值

stmt.connect(&conn);

intiminid,imaxid;

charstrbtime[20];

//准备更新数据的sql,不需要判断返回值

stmt.prepare("\

updategoodssetbtime=str_to_date(,'%%y-%%m-%%d%%h:%%i:%%s')whereid>andid//为sql语句绑定输入变量的地址

stmt.bindin(1,strbtime,19);

stmt.bindin(2,&iminid);

stmt.bindin(3,&imaxid);

//手工指定id的范围为1到5,btime为2017-12-2009:45:30,执行一次更新

iminid=1;

imaxid=5;

memset(strbtime,0,sizeof(strbtime));

strcpy(strbtime,"2017-12-2009:45:30");

//执行sql语句,一定要判断返回值,0-成功,其它-失败。

if(stmt.execute()!=0)

{

printf("stmt.execute()failed.\n%s\n%s\n",stmt.m_sql,stmt.m_cda.message);

exit(-1);

}

//请注意,stmt.m_cda.rpc变量非常重要,它保存了sql被执行后影响的记录数。

printf("本次更新了goods表%ld条记录。\n",stmt.m_cda.rpc);

//提交事务

conn.commit();

exit(0);

}

5、删除表中记录的示例程序deletetable.cpp

//

//本程序演示删除商品表中数据

//

#include"freemysql.h"

intmain(intargc,char*argv[])

{

//数据库连接池

connectionconn;

//sql语言操作类

sqlstatementstmt;

//连接数据库,返回值0-成功,其它-失败

//失败代码在conn.m_cda.rc中,失败描述在conn.m_cda.message中。

if(conn.connecttodb("120.77.115.3,szidc,szmb1601,lxqx,3306")!=0)

{

printf("connectdatabasefailed.\n%s\n",conn.m_cda.message);exit(-1);

}

//设置字符集为'gbk',与mysql数据库的字符集要相同,否则中文会出现乱码

conn.character("gbk");

//为sqlstatement指定数据库连接池,不需要判断返回值

stmt.connect(&conn);

intiminid,imaxid;

//准备删除数据的sql,不需要判断返回值

stmt.prepare("deletefromgoodswhereid>andid//为sql语句绑定输入变量的地址

stmt.bindin(1,&iminid);

stmt.bindin(2,&imaxid);

//手工指定id的范围为1到5

iminid=1;

imaxid=5;

//执行sql语句,一定要判断返回值,0-成功,其它-失败。

if(stmt.execute()!=0)

{

printf("stmt.execute()failed.\n%s\n%s\n",stmt.m_sql,stmt.m_cda.message);

exit(-1);

}

//请注意,stmt.m_cda.rpc变量非常重要,它保存了sql被执行后影响的记录数。

printf("本次从goods表中删除了%ld条记录。\n",stmt.m_cda.rpc);

//提交事务

conn.commit();

exit(0);

}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网