Mysql++安装步骤

作者: Caiaolun | 来源:发表于2018-12-07 09:56 被阅读0次

原文地址:https://blog.csdn.net/laoyang360/article/details/52335669

\color{red}{1、Mysql++作用}
Mysql++是官方发布的、为MySQL设计的C++语言的API,这个API的作用是使工作更加简单且容易。
Mysql++为Mysql的C-Api的再次封装,它用STL开发并编写,并为C++开发程序员提供象操作STL容器一样方便的操作数据库的一套机制。
经常使用STL、OTL的朋友,使用起来会非常方便。

下载地址:https://tangentsoft.net/mysql++/

\color{red}{2、Mysql++安装步骤(CentOS release 6.8 (Final))}
第1步:安装 libmysqlclient

[root@ollen Downloads]# yum install mysql-devel

libmysqlclient.so安装位置查看:

[root@ollen Downloads]# rpm -ql mysql-devel
*****************
/usr/lib64/mysql/libmysqlclient.so

如上,从结果中可以看到libmysqlclient.so在/usr/lib64/mysql/目录下
第2步: 安装mysql3.2.2

  1. 解压:
[root@ollen Downloads]# tar -zxvf mysql3.2.2 .tar.gz
  1. 配置
[root@ollen mysql++]# ./configure –prefix="your specified directory" –enable-thread-check 
  1. 编译
[root@ollen mysql++]# make
  1. 安装
[root@ollen mysql++]# make install

如果一切全程没有\color{red}{Error},\color{blue}{include} \color{blue}{lib}就应该在你指定的目录下了

\color{red}{3、Mysql++操作}
3.1 核心功能点:

  1. 通过Mysql++类库,连接Mysql。
  2. 实现对Mysql的增、删、改、查操作。

3.2 用户手册:

API的详细介绍及使用Demo
http://tangentsoft.net/mysql++/doc/html/userman/

3.3 核心接口

MySql++支持三种查询: Query::execute(), Query::store(), Query::use()

  1. execute( )接口
    用于不返回数据的查询,该函数返回一个SimpleResult对象。

  2. exec( )接口
    它返回一个bool值,标示执行成功与否;如果只要成功与否的标识,可以使用该接口。

  3. store() 接口
    用于用服务器获取数据,该函数返回一个StoreQueryResult对象。对象包含了整个查询结果,使用stl::map方式从里面取数据即可。

  4. use()接口
    同样用于从服务器获取数据,不过该函数返回UseQueryResult对象。相比store()而言更节省内存,该对象类似StoreQueryResult,但是不提供随机访问的特性。use查询会让服务器一次返回结果集的一行。
    Query对象的errnum()返回上次执行对应的错误代码,error()返回错误信息,affected_rows()返回受影响的行数。

3.4 实战源码实现:
1)test.cpp内容如下:

#include <mysql++.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
using namespace mysqlpp;

//insert into cc(id, name, status) values(22, "laoyang", "ok");
const char* g_szInsertFormat = "insert into cc(id, name, status) values(%d, \"%s\", \"%s\");";
const char* g_szUpdateFormat = "update cc set name = \"%s\" where id = %d;";
const char* g_szDeleteFormat = "delete from cc where id = %d;";
const char* g_szSearchFormat = "select * from cc;";

#define DATEBASE_NAME "test"
#define DATEBASE_IP "192.168.1.1"
#define DATEBASE_USERNAME "admin"
#define DATEBASE_PWD "**********"

#define DATA_BUF_SIZE 2048

//增
void insertFun(Query* pQuery)
{
  cout << "Inserting test" << endl;
  char szInsert[DATA_BUF_SIZE] = {0};
  memset(szInsert, 0, DATA_BUF_SIZE);
  int iId = 66;
  const char* pszName = "Miss Zhangx";
  const char* pszStatus = "OK";

  sprintf((char*)szInsert, g_szInsertFormat, iId, pszName, pszStatus);
  cout << "szInsert = " << szInsert << endl;

  *pQuery << szInsert;
  SimpleResult res = pQuery->execute();
  // Report successful insertion
  cout << "Inserted into cc table, ID =" << res.insert_id() << endl;
  cout << endl;
}

//删
void deleteFun(Query* pQuery)
{
  cout << "deleting test" << endl;
  char szDelete[DATA_BUF_SIZE] = {0};
  int iId = 44;
  memset(szDelete, 0, DATA_BUF_SIZE);
  sprintf((char*)szDelete, g_szDeleteFormat, iId);
  cout << "szDelete = " << szDelete << endl;

  *pQuery << szDelete;
  if (pQuery->exec())
  {
  cout << "deleted success!" << endl;
  }
  cout << endl;
}

//改
void updateFun(Query* pQuery)
{
  cout << "updating test" << endl;
  char szUpdate[DATA_BUF_SIZE] = {0};
  memset(szUpdate, 0, DATA_BUF_SIZE);

  int iId = 2;
  const char* pszNewName = "new line 2 revise";

  sprintf((char*)szUpdate, g_szUpdateFormat, pszNewName, iId);
  cout << "szUpdate = " << szUpdate << endl;

  *pQuery << szUpdate;
  if (pQuery->exec())
  {
  cout << "updated success!" << endl;
  }
  cout << endl;
}

//查
void searchFun(Query* pQuery)
{
  /* Now SELECT */
  cout << "selecting test:" << endl;
  *pQuery << g_szSearchFormat;
  StoreQueryResult ares = pQuery->store();
  cout << "ares.num_rows() = " << ares.num_rows() << endl;
  for (size_t i = 0; i < ares.num_rows(); i++)
  {
  cout << "id: " << ares[i]["id"] << "\t - Name: " << ares[i]["name"] \
  << "\t - Status: " << ares[i]["status"] << "\t - Modified_at" << ares[i]["modified_at"] << endl;
  }

  /* Let's get a count of something */
  *pQuery << "SELECT COUNT(*) AS row_count FROM cc";
  StoreQueryResult bres = pQuery->store();
  cout << "Total rows: " << bres[0]["row_count"] << endl;
  cout << endl;
}

int main()
{
  try
  {
  Connection conn(false);
  conn.connect(DATEBASE_NAME, DATEBASE_IP, DATEBASE_USERNAME, DATEBASE_PWD);
  Query query = conn.query();

  /*insert , delete , update, search testing */
  (void)insertFun(&query);
  (void)deleteFun(&query);
  (void)updateFun(&query);
  (void)searchFun(&query);

  }
  catch (BadQuery er)
  { // handle any connection or
  // query errors that may come up
  cerr << "Error: " << er.what() << endl;
  return -1;
  }
  catch (const BadConversion& er)
  {
  // Handle bad conversions
  cerr << "Conversion error: " << er.what() << endl <<
  "\tretrieved data size: " << er.retrieved <<
  ", actual size: " << er.actual_size << endl;
  return -1;
  }
  catch (const Exception& er)
  {
  // Catch-all for any other MySQL++ exceptions
  cerr << "Error: " << er.what() << endl;
  return -1;
  }

  return (EXIT_SUCCESS);
}
  1. makefile文件:
[root@ollen testMysqlConn]# cat Makefile
CXX := g++
CXXFLAGS := -I/usr/include/mysql -I/usr/local/include/mysql++
LDFLAGS := -L/usr/lib64/mysql -lmysqlpp -lmysqlclient -lnsl -lz -lm
EXECUTABLE := main

all: test

clean:
  rm -f $(EXECUTABLE) *.o

3) 执行结果如下:

[root@ollen testMysqlConn]# ./test
Inserting test
szInsert = insert into cc(id, name, status) values(66, "Miss Zhangx", "OK");
Inserted into cc table, ID =66

deleting test
szDelete = delete from cc where id = 44;
deleted success!

updating test
szUpdate = update cc set name = "new line 2 revise" where id = 2;
updated success!

selecting test:
ares.num_rows() = 11
id: 1 - Name: laoyang360 - Status: ok - Modified_at0000-00-00 00:00:00
id: 2 - Name: new line 2 revise - Status: ok - Modified_at2016-06-23 06:16:42
id: 11 - Name: test11 - Status: ok - Modified_at2016-06-24 02:09:15
id: 5 - Name: jdbc_test_update08 - Status: ok - Modified_at0000-00-00 00:00:00
id: 7 - Name: test7 - Status: ok - Modified_at0000-00-00 00:00:00
id: 8 - Name: test008 - Status: ok - Modified_at0000-00-00 00:00:00
id: 9 - Name: test009 - Status: ok - Modified_at0000-00-00 00:00:00
id: 10 - Name: test10 - Status: ok - Modified_at2016-06-24 02:08:14
id: 22 - Name: laoyang - Status: ok - Modified_at2016-08-27 06:24:05
id: 55 - Name: Miss Zhang - Status: OK - Modified_at2016-08-27 07:40:38
id: 66 - Name: Miss Zhangx - Status: OK - Modified_at2016-08-27 08:41:51

\color{red}{问题1}
在的时候配置的时候:error: Didn't find mysqlclient_r library

[root@ollen mysql]# ln -s libmysqlclient.so libmysqlclient_r.so

相关文章

网友评论

    本文标题:Mysql++安装步骤

    本文链接:https://www.haomeiwen.com/subject/femzcqtx.html