Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- set(MYSQL_DIR /home/mark/apps/cppdev/next_mysql_proj/mysql-connector-c++-1.1.9/driver/)
- add_library(mysql STATIC IMPORTED)
- set_property(TARGET mysql PROPERTY
- IMPORTED_LOCATION ${MYSQL_DIR}libmysqlcppconn-static.a)
- add_executable(${PROJECT_NAME} "main.cpp")
- target_link_libraries(${PROJECT_NAME} mysql)
- #include <iostream>
- #include <cstdlib>
- #include <sstream>
- #include <stdexcept>
- #include "mysql_connection.h"
- #include "mysql_driver.h"
- #include <cppconn/driver.h>
- #include <cppconn/exception.h>
- #include <cppconn/resultset.h>
- #include <cppconn/statement.h>
- #define EXAMPLE_HOST "localhost"
- #define EXAMPLE_USER "little_pig"
- #define EXAMPLE_PASS "little_pig"
- #define EXAMPLE_DB "little_pig"
- using namespace std;
- int main(int argc, const char **argv)
- {
- string url(argc >= 2 ? argv[1] : EXAMPLE_HOST);
- const string user(argc >= 3 ? argv[2] : EXAMPLE_USER);
- const string pass(argc >= 4 ? argv[3] : EXAMPLE_PASS);
- const string database(argc >= 5 ? argv[4] : EXAMPLE_DB);
- cout << "Connector/C++ tutorial framework..." << endl;
- cout << endl;
- sql::Driver* driver;
- sql::Connection* conn;
- sql::Statement* stmt;
- sql::ResultSet* result;
- try
- {
- driver = sql::mysql::get_driver_instance();
- conn = driver->connect("tcp://127.0.0.1:3306", EXAMPLE_USER, EXAMPLE_PASS);
- stmt = conn->createStatement();
- result = stmt->executeQuery("use little_pig; SELECT question FROM little_pig.littlepigmodel where id = 1;");
- while (result->next())
- {
- cout << result->getString(1) << endl;
- }
- conn->close();
- delete conn;
- delete stmt;
- delete result;
- }
- catch (sql::SQLException &e)
- {
- cout << "# ERR: SQLException in " << __FILE__;
- cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
- /* what() (derived from std::runtime_error) fetches error message */
- cout << "# ERR: " << e.what();
- cout << " (MySQL error code: " << e.getErrorCode();
- cout << ", SQLState: " << e.getSQLState() << " )" << endl;
- delete conn;
- delete stmt;
- delete result;
- return EXIT_FAILURE;
- }
- cout << "Done." << endl;
- return EXIT_SUCCESS;
- }
- set(MYSQL_DIR /home/mark/apps/cppdev/next_mysql_proj/mysql-connector-c++-1.1.9/driver/)
- set(MYSQL_INCL /home/mark/apps/cppdev/next_mysql_proj/mysql-connector-c++-1.1.9/)
- add_library(mysql SHARED IMPORTED)
- # Указываем путь к файлу библиотеки
- set_property(TARGET mysql PROPERTY
- IMPORTED_LOCATION ${MYSQL_DIR}libmysqlcppconn.so)
- add_executable(${PROJECT_NAME} "main.cpp")
- target_link_libraries(${PROJECT_NAME} mysql)
- # Указываем путь к заголовочным файлам
- target_include_directories(${PROJECT_NAME} PUBLIC ${MYSQL_INCL})
- #include "driver/mysql_connection.h" // В кавычках пишутся пути относительно текущего файла
- #include "driver/mysql_driver.h"
- #include <cppconn/driver.h> // В угловых скобках, относительно
- #include <cppconn/exception.h> // target_include_directories, который
- #include <cppconn/resultset.h> // определен в скрипте СМаке
- #include <cppconn/statement.h>
Add Comment
Please, Sign In to add comment