0

I'm trying to execute a very basic MySQL statement in C++ using MySQL Connector/C++. Unfortunately, my very basic code is failing in a way that has stumped me. Here is the code that I have:

try
{
  sql::mysql::MySQL_Driver *driver = NULL;
  sql::Connection *conn = NULL;
  sql::Statement *stmt = NULL;

  driver = sql::mysql::get_mysql_driver_instance();
  if (driver != NULL)
  {
    conn = driver->connect("127.0.0.1:3306", "root", "root");
    cout << "Test" << endl;
    stmt = conn->createStatement();
    stmt->execute("USE mysql");
  }
  delete conn;
  conn = NULL;
}

This code fails with an error that says "My SQL server has gone away". I've read that this error occurs because of a timeout after something like an hour, but this is happening immediately when I execute the program. Any ideas on how I can fix this issue?

user2701114
  • 141
  • 1
  • 1
  • 5
  • You sure you have upgraded to [MySQL Connector/C++ 8.0](https://dev.mysql.com/downloads/connector/cpp/8.0.html).? MySQL Connector/C++ 8.0 works for MySQL modern versions like 5.5, 5.6, 5,7 and 8.0 – Raymond Nijland Nov 27 '18 at 22:58
  • The title of your post is very unspecific. You may want to edit it such that other users can benefit from your question. – normanius Nov 27 '18 at 23:02
  • 2
    Possible duplicate of [MySQL error 2006: mysql server has gone away](https://stackoverflow.com/questions/7942154/mysql-error-2006-mysql-server-has-gone-away) - there seems to be a multitude of reasons why this might occur, so please provide more details such as OS and the exact line that the error occurs on, etc, and also a [MCVE], as the code as-is is missing a `catch`. – Ken Y-N Nov 27 '18 at 23:04
  • [Manual on Gone Away](https://dev.mysql.com/doc/refman/5.7/en/gone-away.html), adjust URL for server version. – danblack Nov 28 '18 at 00:09

0 Answers0