1、下载Connector/C++库php
官网地址:mysql
https://dev.mysql.com/downloads/connector/cpp/linux
能够下载源代码本身编译,也能够根据不一样的OS环境下载编译好的包,这里下来mysql-connector-c++-1.1.9-linux-ubuntu16.04-x86-64bit.tar.gzios
2、解压c++
这里放在mysql的安装目录/opt/mysql下:sql
$ 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=在编译时指定路径。ubuntu
3、编译运行服务器
建库脚本:tcp
-- 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 spa
#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; }
编译运行
$ 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
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