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