First, install Cpp Connector
Installation Connectoor \ C ++
Installation libmysqlcppconn-dev
apt-cache search libmysqlcppconn-dev sudo apt-get update sudo apt-get install libmysqlcppconn-dev
After installing the visible / usr / include / header files more than three mysql_connection.h, mysql_deriver.h, mysql_error.h and folders cppconn.
Second, the use libmysqlcppconn-dev library database connection
1. Perform a simple query
#include <stdlib.h> #include <iostream> #include <sstream> #include <stdexcept> #include <mysql_connection.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(int argc, const char **argv) { const string url="localhost"; const string user="root"; const string pass="zjq588"; const string database="SRS"; // 1.获取驱动 sql::Driver* driver = get_driver_instance(); // 2.连接到mysql server std::auto_ptr<sql::Connection> conn(driver->connect(url, user, pass)); // 3. The selected database conn-> the setSchema (Database); // 4. Create the Statement SQL * the Statement stmt :: = conn-> the createStatement (); // 5. The execution Query SQL :: = RES * the ResultSet stmt- > the executeQuery ( "SELECT * from Student;"); // 6. The result set the while (RES-> Next ()) { int ID = RES-> the getInt ( "stu_id"); String name = RES-> the getString ( "stu_name"); COUT ID << << "," << endl << name; } // 7. The release of the Statement Delete stmt; return 0; }
2. Perform PrepareStatement