在Ubuntu机器上从c ++连接到mysql

时间:2015-05-01 06:16:53

标签: c++ mysql virtual-machine

我在Ubuntu OS的虚拟机上运行MySQL。

我想使用带有Windows 7操作系统的c ++读取和写入数据到MySQL,我几乎没有问题:

  1. 我如何知道MySQL服务器的IP地址?它是Ubuntu VM的IP还是127.0.0.1?

  2. 如何使用c ++读取数据?我需要司机或其他什么吗?

  3. 我试过;安装Visual Studio Express 2010 c ++,在Ubuntu上安装MySQL。

    我也试过这段代码:来自oracle网站。

    /*
      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>
    
    using namespace std;
    
    int main(void)
    {
    cout << endl;
    cout << "Running 'SELECT 'Hello World!' »";
    
    try {
      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", "root");
      /* Connect to the MySQL test database */
      con->setSchema("test");
    
      stmt = con->createStatement();
      res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
      while (res->next()) {
        cout << "\t... MySQL replies: ";
        /* Access column data by alias or column name */
        cout << res->getString("_message") << endl;
        cout << "\t... MySQL says it again: ";
        /* Access column fata by numeric offset, 1 is the first column */
        cout << res->getString(1) << endl;
      }
      delete res;
      delete stmt;
      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;
    }
    
    cout << endl;
    
    return EXIT_SUCCESS;
    }
    

0 个答案:

没有答案