Linux下编译MySQL++及简单使用

下载

MySQL++的官网地址为https://tangentsoft.com/mysqlpp/wiki?name=MySQL%2B%2B&p&nsm
当前推荐的版本为3.3.0为例,下载地址为mysql+±3.3.0.tar.gz

编译

使用命令tar -zxvf mysql++-3.3.0.tar.gz进行解压,解压完成后文件夹mysql+±3.3.0中目录结构如下:

[root@VM-12-15-centos mysql++-3.3.0]# ls
abi.xml          config.h.in      install.hta           mysql++.bkl-merge     README-Solaris.txt
abi.xml.in       config.sub       install.hta.in        mysql++.bkl-original  README-Unix.txt
aclocal.m4       configure        install-sh            mysql++.ebuild        README-Visual-C++.txt
Bakefiles.bkgen  configure~       INSTALL.txt           mysql++.spec          rebake.bat
bk-deps          configure.ac     lib                   mysql++.spec.in       RELEASE-CHECKLIST.txt
bmark.txt        CONTRIBUTING.md  libtool               mysql++.xcodeproj     shared-ld-sh
bootstrap        COPYING.txt      LICENSE.txt           osver                 ssx
bootstrap.bat    CREDITS.txt      ltmain.sh             README-Cygwin.txt     test
ChangeLog.md     doc              Makefile.in           README-examples.txt   vc2003
cleanmf          dtest            Makefile.mingw        README-Linux.txt      vc2005
config           examples         Makefile.simple       README-Mac-OS-X.txt   vc2008
config.guess     exrun            mysql++.bkl           README.md             version
config.h         exrun.bat        mysql++.bkl-baseline  README-MinGW.txt      version.in

运行./configure进行配置,生成MakeFile文件。

./configure

注意,当前操作系统为CentOS7.6,系统自带的gcc版本为4.8.5。运行./configure无报错。如果将gcc升级至8.5.0(其他版本未试过),运行./configure将报错。

另外,编译MySQL++需要先安装MySQL,如果是默认安装的话,编译MySQL++就无须另外设置,如果MySQL相关的头文件和库文件(mysqlclient.so)不在默认安装目录, 编译MySQL++就需要设置相关目录(为了方便,建议安装MySQL是进行默认安装)。

再使用命令make进行编译,

[root@instance-1apocjsh build]# make
...省略

最后使用命令make install DESTDIR=目标目录进行安装。

[root@instance-1apocjsh build]# make install DESDIR=xxx
...省略

安装完成后,将目标目录中的include目录lib目录放入到新建的MySQL++目录,最终供外部使用的**MySQL++**目录结构如下(去掉其中无用的文件、文件夹):

[root@VM-12-15-centos local]# tree MySQL++/
MySQL++/
|-- include
|   `-- mysql++
|       |-- autoflag.h
|       |-- beemutex.h
|       |-- cmdline.h
|       |-- common.h
|       |-- comparable.h
|       |-- connection.h
|       |-- cpool.h
|       |-- datetime.h
|       |-- dbdriver.h
|       |-- exceptions.h
|       |-- field.h
|       |-- field_names.h
|       |-- field_types.h
|       |-- insertpolicy.h
|       |-- manip.h
|       |-- myset.h
|       |-- mysql++.h
|       |-- mystring.h
|       |-- noexceptions.h
|       |-- null.h
|       |-- options.h
|       |-- qparms.h
|       |-- querydef.h
|       |-- query.h
|       |-- refcounted.h
|       |-- result.h
|       |-- row.h
|       |-- scopedconnection.h
|       |-- sql_buffer.h
|       |-- sqlstream.h
|       |-- sql_types.h
|       |-- ssqls2.h
|       |-- ssqls.h
|       |-- stadapter.h
|       |-- stream2string.h
|       |-- tcp_connection.h
|       |-- tiny_int.h
|       |-- transaction.h
|       |-- type_info.h
|       |-- uds_connection.h
|       |-- utility.h
|       |-- vallist.h
|       `-- wnp_connection.h
`-- lib
    |-- libmysqlpp.so -> libmysqlpp.so.3
    |-- libmysqlpp.so.3 -> libmysqlpp.so.3.3.0
    `-- libmysqlpp.so.3.3.0

3 directories, 46 files

使用

测试项目为test_mysql++,目录结构为:

[root@VM-12-15-centos test_mysql++]# ls -al
total 20
drwxr-xr-x 3 root root 4096 Aug 12 15:52 .
drwxr-xr-x 5 root root 4096 Aug 12 13:34 ..
-rwxr-xr-x 1 root root  364 Aug 12 15:49 build.sh
-rw-r--r-- 1 root root 1015 Aug 11 20:50 main.cpp
drwxr-xr-x 4 root root 4096 Aug 12 13:34 MySQL++

main.cpp内容如下:

#include <mysql++/mysql++.h>
#include <iostream>
#include <iomanip>

using namespace std;

int main(int argc, char *argv[])
{
	mysqlpp::Connection conn(false);
	mysqlpp::SetCharsetNameOption *opt = new  mysqlpp::SetCharsetNameOption("gbk");

	conn.set_option(opt);
	if (conn.connect("数据库",
		"ip地址",
		"用户",
		"密码"))
	{
		conn.query("set names 'gbk' ");
		mysqlpp::Query query = conn.query("select * from test_table");
		mysqlpp::UseQueryResult res = query.use();
		if (res)
		{
			while (mysqlpp::Row row = res.fetch_row())
			{
				cout << "-------------------------------" << endl;
				cout << setw(9) << "name:" << row["name"] << endl;
				cout << setw(9) << "age:" << row["age"] << endl;
				cout << setw(9) << "addr:" << row["addr"] << endl;
				cout << setw(9) << "company:" << row["company"] << endl;
			}
		}
		else
		{
			cerr << "Failed to get item list: " << query.error() << endl;
			return 0;
		}
	}
	else
	{
		cerr << "DB connection failed: " << conn.error() << endl;
		return 0;
	}
	
	return 0;
}

build.sh内容如下:

#!/bin/bash

g++ main.cpp -I ./MySQL++/include/ -l mysqlpp -L ./MySQL++/lib/ -Wl,-R ./MySQL++/lib/ -I /usr/include/mysql/ -l mysqlclient -L /usr/lib64/mysql/ -Wl,-R /usr/lib64/mysql/

即:

g++ main.cpp 

-I ./MySQL++/include/ 
-l mysqlpp 
-L ./MySQL++/lib/ 
-Wl,-R ./MySQL++/lib/ 

-I /usr/include/mysql/ 
-l mysqlclient 
-L /usr/lib64/mysql/ 
-Wl,-R /usr/lib64/mysql/

MySQL++文件夹即可上面产生的动态库。

如上面所示,使用MySQL++库时,需要指定mysqlclient库的包含目录以及库目录相关信息,否则编译或者链接会报错。

MySQL数据库中test库表test_table的建表语句如下:

DROP TABLE IF EXISTS `test_table`;
CREATE TABLE `test_table`  (
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  `age` int(11) NOT NULL,
  `addr` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  `company` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  PRIMARY KEY (`name`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of test_table
-- ----------------------------
INSERT INTO `test_table` VALUES ('frh', 28, 'beijing', 'huaru');
INSERT INTO `test_table` VALUES ('hf', 34, 'chengdu', 'jiushi');
INSERT INTO `test_table` VALUES ('xp', 30, 'chengdu', 'huaru');
INSERT INTO `test_table` VALUES ('ys', 30, 'dujiangyan', 'nongyeju');
INSERT INTO `test_table` VALUES ('zs', 26, 'chengdu', 'jiushi');

运行build.sh生成可执行文件a.out,输出信息如下:

-------------------------------
    name:frh
     age:28
    addr:beijing
 company:huaru
-------------------------------
    name:hf
     age:34
    addr:chengdu
 company:jiushi
-------------------------------
    name:xp
     age:30
    addr:chengdu
 company:huaru
-------------------------------
    name:ys
     age:30
    addr:dujiangyan
 company:nongyeju
-------------------------------
    name:zs
     age:26
    addr:chengdu
 company:jiushi

补充

MySQL++与Connector/C++的比较

How does MySQL++ compare to MySQL’s Connector/C++?

Connector/C++ is a much younger C++ wrapper for the MySQL C API library, fully developed under the MySQL corporate aegis. By contrast, MySQL++ has a long, complex development history. MySQL++ started out as a third-party library, was maintained and enhanced by MySQL for several years, then got spun back out again, this time probably for good. Oracle still hosts our old mailing list, which is still useful as an archive of answered questions, for which we thank them, but they don’t control the hosting or development of MySQL++.

Before Sun (and then Oracle) bought them, MySQL AB decided to create a competing library despite once having controlled MySQL++ in-house for two reasons. First, having the entire thing to themselves mean they can do the same dual-licensing thing they do with the C API library. Second, they wanted to put MySQL support into OpenOffice, and they wanted it to have a JDBC style API.

By contrast with Connector/C++’s Java-style database API, MySQL++ is very much a native C++ library: we use STL and other Standard C++ features heavily. If you are a Java developer or simply admire its database interface design, you may prefer Connector/C++.

Another way to look at it is that Connector/C++ because is newer, it is perhaps less crufty, while MySQL++ is mature and featureful.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值