Store data from a mysql database into a C++ variable using mysql connector

一笑奈何 提交于 2019-12-13 06:26:23

问题


So I downloaded the c++/mysql connector from http://dev.mysql.com/downloads/connector/cpp/
Now I want to store data that I've retrieved from the database to a variable. This is as far as I've gotten.

string str;
/////////////////////////////////get data from database////////////////////////////////////////////////////////
  sql::Driver *driver;
  sql::Connection *con;
  sql::Statement *stmt;
  sql::ResultSet *res;

  /* Create a connection */
  driver = get_driver_instance();
  con = driver->connect("tcp://127.0.0.1:3306", "root", "");
  /* Connect to the MySQL test database */
  con->setSchema("awesome");

  stmt= con->createStatement();

      res = stmt->executeQuery(

"SELECT * FROM master where choice='Y'");

/////////////////////////////Store it somewhere/////////////////////////////// 
          str= res->getString("rfid");

Any and all help is welcomed


回答1:


While it is not that easy to guess what is happening without rest of the code and database data, following code should help to at least get better understanding on what went wrong:

  try
  {
    while (res->next())
    {
          str= res->getString("rfid");
    }
  }
  catch (std::exception &e)
  {
    std::cerr << e.what();
  }


来源:https://stackoverflow.com/questions/16004540/store-data-from-a-mysql-database-into-a-c-variable-using-mysql-connector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!