Guest User

Untitled

a guest
Apr 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. set(MYSQL_DIR /home/mark/apps/cppdev/next_mysql_proj/mysql-connector-c++-1.1.9/driver/)
  2. add_library(mysql STATIC IMPORTED)
  3. set_property(TARGET mysql PROPERTY
  4. IMPORTED_LOCATION ${MYSQL_DIR}libmysqlcppconn-static.a)
  5. add_executable(${PROJECT_NAME} "main.cpp")
  6. target_link_libraries(${PROJECT_NAME} mysql)
  7.  
  8. #include <iostream>
  9. #include <cstdlib>
  10. #include <sstream>
  11. #include <stdexcept>
  12.  
  13. #include "mysql_connection.h"
  14. #include "mysql_driver.h"
  15.  
  16. #include <cppconn/driver.h>
  17. #include <cppconn/exception.h>
  18. #include <cppconn/resultset.h>
  19. #include <cppconn/statement.h>
  20.  
  21.  
  22. #define EXAMPLE_HOST "localhost"
  23. #define EXAMPLE_USER "little_pig"
  24. #define EXAMPLE_PASS "little_pig"
  25. #define EXAMPLE_DB "little_pig"
  26.  
  27.  
  28. using namespace std;
  29.  
  30. int main(int argc, const char **argv)
  31. {
  32. string url(argc >= 2 ? argv[1] : EXAMPLE_HOST);
  33. const string user(argc >= 3 ? argv[2] : EXAMPLE_USER);
  34. const string pass(argc >= 4 ? argv[3] : EXAMPLE_PASS);
  35. const string database(argc >= 5 ? argv[4] : EXAMPLE_DB);
  36.  
  37. cout << "Connector/C++ tutorial framework..." << endl;
  38. cout << endl;
  39.  
  40. sql::Driver* driver;
  41. sql::Connection* conn;
  42. sql::Statement* stmt;
  43. sql::ResultSet* result;
  44.  
  45. try
  46. {
  47. driver = sql::mysql::get_driver_instance();
  48. conn = driver->connect("tcp://127.0.0.1:3306", EXAMPLE_USER, EXAMPLE_PASS);
  49.  
  50. stmt = conn->createStatement();
  51. result = stmt->executeQuery("use little_pig; SELECT question FROM little_pig.littlepigmodel where id = 1;");
  52. while (result->next())
  53. {
  54. cout << result->getString(1) << endl;
  55. }
  56. conn->close();
  57.  
  58. delete conn;
  59. delete stmt;
  60. delete result;
  61. }
  62. catch (sql::SQLException &e)
  63. {
  64. cout << "# ERR: SQLException in " << __FILE__;
  65. cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
  66. /* what() (derived from std::runtime_error) fetches error message */
  67. cout << "# ERR: " << e.what();
  68. cout << " (MySQL error code: " << e.getErrorCode();
  69. cout << ", SQLState: " << e.getSQLState() << " )" << endl;
  70.  
  71. delete conn;
  72. delete stmt;
  73. delete result;
  74.  
  75. return EXIT_FAILURE;
  76. }
  77.  
  78. cout << "Done." << endl;
  79. return EXIT_SUCCESS;
  80.  
  81. }
  82.  
  83. set(MYSQL_DIR /home/mark/apps/cppdev/next_mysql_proj/mysql-connector-c++-1.1.9/driver/)
  84. set(MYSQL_INCL /home/mark/apps/cppdev/next_mysql_proj/mysql-connector-c++-1.1.9/)
  85. add_library(mysql SHARED IMPORTED)
  86. # Указываем путь к файлу библиотеки
  87. set_property(TARGET mysql PROPERTY
  88. IMPORTED_LOCATION ${MYSQL_DIR}libmysqlcppconn.so)
  89. add_executable(${PROJECT_NAME} "main.cpp")
  90.  
  91. target_link_libraries(${PROJECT_NAME} mysql)
  92. # Указываем путь к заголовочным файлам
  93. target_include_directories(${PROJECT_NAME} PUBLIC ${MYSQL_INCL})
  94.  
  95. #include "driver/mysql_connection.h" // В кавычках пишутся пути относительно текущего файла
  96. #include "driver/mysql_driver.h"
  97.  
  98. #include <cppconn/driver.h> // В угловых скобках, относительно
  99. #include <cppconn/exception.h> // target_include_directories, который
  100. #include <cppconn/resultset.h> // определен в скрипте СМаке
  101. #include <cppconn/statement.h>
Add Comment
Please, Sign In to add comment