Use of MySQL Connector/C++ library

1. Download the Connector/C++ library

Official website address:

https://dev.mysql.com/downloads/connector/cpp/

You can download the source code and compile it yourself, or you can download the compiled package according to different OS environments, here mysql-connector-c++-1.1.9-linux-ubuntu16.04-x86-64bit.tar.gz

2. Decompression

Here it is placed in the mysql installation directory /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++

Put /opt/mysql/mysql-connector-c++/lib in the dynamic library search path or use, -rpath= to specify the path at compile time.

3. Compile and run

Library build script:

-- 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++ code testcppconn.cpp 

#include<iostream>
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
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<<result->getString("id")<<"	";
		cout<<result->getString("fullname")<<endl;
	}
	return 0;
}

compile and run

$ 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 
1	Ed Jones
2	Wendy Williams
3	Mary Contrary
4	lisa Contrary
5	cred Flinstone
6	Fred Flinstone
7	Jack Bean
8	Ed Jones
14	Jack Bean
15	Ed Jones
16	Wendy Williams
17	Mary Contrary
18	lisa Contrary
19	cred Flinstone
20	Fred Flinstone
21	Jack Bean
22	Jack wxw
23	Jack wxw2

To use pkg-config, write a mysqlconncpp.pc and put it in the $PKG_CONFIG_PATH 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

So the compile command can become like this:

$ g++ -o testcppconn testcppconn.cpp `pkg-config --libs --cflags mysqlconncpp` -Wl,-rpath=/opt/mysql/mysql-connector-c++/lib

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325472280&siteId=291194637