C++操作mysql数据库

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

技术交流QQ:157086771

版本历史

freemysql v1.0.0           点击下载


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

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

#include "freemysql.h"

int main(int argc,char *argv[])
{
  // 数据库连接池
  connection conn;

  // SQL语言操作类
  sqlstatement stmt;

  // 连接数据库,返回值0-成功,其它-失败
  // 失败代码在conn.m_cda.rc中,失败描述在conn.m_cda.message中。
  if (conn.connecttodb("120.77.115.3,szidc,SZmb1601,lxqx,3306") != 0)
  {
    printf("connect database failed.\n%s\n",conn.m_cda.message);
    exit(-1);
  }

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

  // 为sqlstatement指定数据库连接池,不需要判断返回值
  stmt.connect(&conn);

 // 为sqlstatement指定数据库连接池,不需要判断返回值
  stmt.connect(&conn);

  // 准备创建表的SQL,商品表:商品编号id,商品名称name,价格sal
  // 入库时间btime,商品说明memo,商品图片pic
  // prepare方法不需要判断返回值
  stmt.prepare("\
    create table goods(id    bigint(10),\
                       name  varchar(30),\
                       sal   decimal(8,2),\
                       btime datetime,\
                       memo  longtext,\
                       pic   longblob,\
                       primary key (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("create table goods ok.\n");

  exit(0);
}


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

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

#include "freemysql.h"

// 定义用于操作数据的结构,与表中的字段对应
struct st_GOODS
{
  long id;          // 商品编号
  char name[31];    // 商品名称
  double sal;       // 商品价格
  char btime[20];   // 入库时间
} stgoods;

int main(int argc,char *argv[])
{
  // 数据库连接池
  connection conn;

  // SQL语言操作类
  sqlstatement stmt;

  // 连接数据库,返回值0-成功,其它-失败
  // 失败代码在conn.m_cda.rc中,失败描述在conn.m_cda.message中。
  if (conn.connecttodb("120.77.115.3,szidc,SZmb1601,lxqx,3306") != 0)
  {
    printf("connect database failed.\n%s\n",conn.m_cda.message); 
exit(-1);
  }


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


  // 为sqlstatement指定数据库连接池,不需要判断返回值
  stmt.connect(&conn);

  // 准备插入数据的SQL,不需要判断返回值
  stmt.prepare("\
    insert into goods(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 (int ii=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-01 12: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("insert ok(id=%d).\n",ii);
  }

  printf("insert table goods ok.\n");

  // 提交数据库事务
  conn.commit();

  exit(0);
}



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

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

#include "freemysql.h"

// 定义用于查询数据的结构,与表中的字段对应
struct st_GOODS
{
  long id;          // 商品编号
  char name[31];    // 商品名称
  double sal;       // 商品价格
  char btime[20];   // 入库时间
} stgoods;

int main(int argc,char *argv[])
{
  // 数据库连接池
  connection conn;

  // SQL语言操作类
  sqlstatement stmt;

  // 连接数据库,返回值0-成功,其它-失败
  // 失败代码在conn.m_cda.rc中,失败描述在conn.m_cda.message中。
  if (conn.connecttodb((char *)"120.77.115.3,szidc,SZmb1601,lxqx,3306") != 0)
  {
    printf("connect database failed.\n%s\n",conn.m_cda.message);
    exit(-1);
  }

  // 设置字符集为'gbk',与mysql数据库的字符集要相同,否则中文会出现乱码
  conn.character((char*)"gbk");

  // 为sqlstatement指定数据库连接池,不需要判断返回值
  stmt.connect(&conn);

  int iminid,imaxid;

  // 准备查询数据的SQL,不需要判断返回值
  stmt.prepare("\
    select id,name,sal,date_format(btime,'%%Y-%%m-%%d %%h:%%i:%%s')\
      from goods where id>:1 and id<: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"

int main(int argc,char *argv[])
{
  // 数据库连接池
  connection conn;

  // SQL语言操作类
  sqlstatement stmt;

  // 连接数据库,返回值0-成功,其它-失败
  // 失败代码在conn.m_cda.rc中,失败描述在conn.m_cda.message中。
  if (conn.connecttodb("120.77.115.3,szidc,SZmb1601,lxqx,3306") != 0)
  {
    printf("connect database failed.\n%s\n",conn.m_cda.message); exit(-1);
  }

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

  // 为sqlstatement指定数据库连接池,不需要判断返回值
  stmt.connect(&conn);

  int iminid,imaxid;
  char strbtime[20];
  
  // 准备更新数据的SQL,不需要判断返回值
  stmt.prepare("\
    update goods set btime=str_to_date(?,'%%Y-%%m-%%d %%h:%%i:%%s') where id>? and id<?");
  // 为SQL语句绑定输入变量的地址
  stmt.bindin(1, strbtime,19);
  stmt.bindin(2,&iminid);
  stmt.bindin(3,&imaxid);

  // 手工指定id的范围为1到5,btime为2017-12-20 09:45:30,执行一次更新
  iminid=1;
  imaxid=5;
  memset(strbtime,0,sizeof(strbtime));
  strcpy(strbtime,"2017-12-20 09: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"

int main(int argc,char *argv[])
{
  // 数据库连接池
  connection conn;

  // SQL语言操作类
  sqlstatement stmt;

  // 连接数据库,返回值0-成功,其它-失败
  // 失败代码在conn.m_cda.rc中,失败描述在conn.m_cda.message中。
  if (conn.connecttodb("120.77.115.3,szidc,SZmb1601,lxqx,3306") != 0)
  {
    printf("connect database failed.\n%s\n",conn.m_cda.message); exit(-1);
  }

  // 设置字符集为'gbk',与mysql数据库的字符集要相同,否则中文会出现乱码
  conn.character("gbk");
  
  // 为sqlstatement指定数据库连接池,不需要判断返回值
  stmt.connect(&conn);
  
  int iminid,imaxid; 
  
  // 准备删除数据的SQL,不需要判断返回值
  stmt.prepare("delete from goods where id>? and id<?");
  // 为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);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千百炼软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值