Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Standard C++ includes */
- #include <stdlib.h>
- #include <iostream>
- #include <vector>
- /*
- Include directly the different
- headers from cppconn/ and mysql_driver.h + mysql_util.h
- (and mysql_connection.h). This will reduce your build time!
- */
- #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(void)
- {
- std::string query = std::string("SELECT ID,Username FROM Users WHERE Username='admin'");
- try {
- sql::Driver *driver;
- sql::Connection *con;
- sql::Statement *stmt;
- sql::ResultSet *res;
- sql::PreparedStatement *pstmt;
- /* Create a connection */
- driver = get_driver_instance();
- con = driver->connect("tcp://127.0.0.1:3306", "root", "password");
- /* Connect to the MySQL test database */
- con->setSchema("LootGame");
- /* Select in ascending order */
- pstmt = con->prepareStatement(query);
- res = pstmt->executeQuery();
- /* Fetch in reverse = descending order! */
- res->afterLast();
- while (res->previous()) {
- cout << "\t Id: " << res->getInt("ID") << " | Name: " << res->getString("Username") << endl;
- }
- delete res;
- delete pstmt;
- delete con;
- }
- catch (sql::SQLException &e) {
- cout << "# ERR: SQLException in " << __FILE__;
- cout << "(" << __FUNCTION__ << ") on line ยป " << __LINE__ << endl;
- cout << "# ERR: " << e.what();
- cout << " (MySQL error code: " << e.getErrorCode();
- cout << ", SQLState: " << e.getSQLState() << "ยป )" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement