Advertisement
Guest User

Untitled

a guest
Feb 24th, 2019
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. /* Standard C++ includes */
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. /*
  7. Include directly the different
  8. headers from cppconn/ and mysql_driver.h + mysql_util.h
  9. (and mysql_connection.h). This will reduce your build time!
  10. */
  11. #include "mysql_connection.h"
  12.  
  13. #include <cppconn/driver.h>
  14. #include <cppconn/exception.h>
  15. #include <cppconn/resultset.h>
  16. #include <cppconn/statement.h>
  17. #include <cppconn/prepared_statement.h>
  18.  
  19. using namespace std;
  20.  
  21. int main(void)
  22. {  
  23.     std::string query = std::string("SELECT ID,Username FROM Users WHERE Username='admin'");
  24.  
  25.     try {
  26.         sql::Driver *driver;
  27.         sql::Connection *con;
  28.         sql::Statement *stmt;
  29.         sql::ResultSet *res;
  30.         sql::PreparedStatement *pstmt;
  31.  
  32.         /* Create a connection */
  33.         driver = get_driver_instance();
  34.         con = driver->connect("tcp://127.0.0.1:3306", "root", "password");
  35.         /* Connect to the MySQL test database */
  36.         con->setSchema("LootGame");
  37.  
  38.         /* Select in ascending order */
  39.         pstmt = con->prepareStatement(query);
  40.         res = pstmt->executeQuery();
  41.  
  42.         /* Fetch in reverse = descending order! */
  43.         res->afterLast();
  44.         while (res->previous()) {
  45.             cout << "\t Id: " << res->getInt("ID") << " | Name: " << res->getString("Username") << endl;
  46.         }
  47.         delete res;
  48.  
  49.         delete pstmt;
  50.         delete con;
  51.  
  52.     }
  53.     catch (sql::SQLException &e) {
  54.         cout << "# ERR: SQLException in " << __FILE__;
  55.         cout << "(" << __FUNCTION__ << ") on line ยป " << __LINE__ << endl;
  56.         cout << "# ERR: " << e.what();
  57.         cout << " (MySQL error code: " << e.getErrorCode();
  58.         cout << ", SQLState: " << e.getSQLState() << "ยป )" << endl;
  59.     }
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement