一、下载Connector/C++库
官网地址:
可以下载源代码自己编译,也可以根据不同的OS环境下载编译好的包,这里下来mysql-connector-c++-1.1.9-linux-ubuntu16.04-x86-64bit.tar.gz
二、解压
这里放在mysql的安装目录/opt/mysql下:
$ cd /opt/mysql/
$ sudo tar xzvf ~/mysql-connector-c++-1.1.9-linux-ubuntu16.04-x86-64bit.tar.gz
$ sudo ln -s mysql-connector-c++-1.1.9-linux-ubuntu16.04-x86-64bit mysql-connector-c++
把/opt/mysql/mysql-connector-c++/lib放到动态库搜索路径或用,-rpath=在编译时指定路径。
三、编译运行
建库脚本:
-- phpMyAdmin SQL Dump
-- version 4.6.4
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Generation Time: 2017-12-30 08:49:43
-- 服务器版本: 5.7.14
-- PHP Version: 5.6.26
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `mymotif`
--
-- --------------------------------------------------------
--
-- 表的结构 `users`
--
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`fullname` varchar(40) DEFAULT NULL,
`password` varchar(12) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- 转存表中的数据 `users`
--
INSERT INTO `users` (`id`, `name`, `fullname`, `password`) VALUES
(1, 'ed', 'Ed Jones', 'edspassword'),
(2, 'wendy', 'Wendy Williams', 'foobar'),
(3, 'mary', 'Mary Contrary', 'xxg527'),
(4, 'lisa', 'lisa Contrary', 'ls123'),
(5, 'cred', 'cred Flinstone', 'bla123'),
(6, 'fred', 'Fred Flinstone', 'blah'),
(7, 'jack', 'Jack Bean', 'gjffdd'),
(8, 'ed', 'Ed Jones', 'edspassword'),
(14, 'jack', 'Jack Bean', 'gjffdd'),
(15, 'ed', 'Ed Jones', '888'),
(16, 'wendy', 'Wendy Williams', 'foobar'),
(17, 'mary', 'Mary Contrary', '123'),
(18, 'lisa', 'lisa Contrary', 'ls123'),
(19, 'cred', 'cred Flinstone', 'bla123'),
(20, 'fred', 'Fred Flinstone', 'blah'),
(21, 'jack', 'Jack Bean', 'gjffdd'),
(22, 'wxw01', 'Jack wxw', '123'),
(23, 'wxw02', 'Jack wxw2', '234');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);
--
-- 在导出的表使用AUTO_INCREMENT
--
--
-- 使用表AUTO_INCREMENT `users`
--
ALTER TABLE `users`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=24;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
c++代码testcppconn.cpp
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
sql::mysql::MySQL_Driver *driver;
sql::Connection *conn;
sql::Statement *state;
sql::ResultSet *result;
driver = sql::mysql::get_driver_instance();
conn = driver->connect("localhost", "your-user", "your-passwd");
state = conn->createStatement();
state->execute("use test");
result = state->executeQuery("select * from users");
输出查询
while (result->next()!=NULL)
{
cout<getString("id")<
cout<getString("fullname")<
}
return 0;
}
编译运行
$ g++ testcppconn.cpp -o testcppconn -I/opt/mysql/mysql-connector-c++/include -L/opt/mysql/mysql-connector-c++/lib -lmysqlcppconn -Wl,-rpath=/opt/mysql/mysql-connector-c++/lib
$ ./testcppconn
1Ed Jones
2Wendy Williams
3Mary Contrary
4lisa Contrary
5cred Flinstone
6Fred Flinstone
7Jack Bean
8Ed Jones
14Jack Bean
15Ed Jones
16Wendy Williams
17Mary Contrary
18lisa Contrary
19cred Flinstone
20Fred Flinstone
21Jack Bean
22Jack wxw
23Jack wxw2
pkg-config的使用,写个mysqlconncpp.pc放在$PKG_CONFIG_PATH路径里:
prefix=/opt/mysql/mysql-connector-c++
includedir=${prefix}/include
libdir=${prefix}/lib
Name: mysqlcppconn
Description: MySQL Connector/C++ library
Version: 1.1.9
Cflags: -I${includedir}
Libs: -L${libdir} -lmysqlcppconn
Libs.private: -lpthread -lm -lrt -ldl
这样编译命令就可以变成这样:
$ g++ -o testcppconn testcppconn.cpp `pkg-config --libs --cflags mysqlconncpp` -Wl,-rpath=/opt/mysql/mysql-connector-c++/lib