ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

I did this in Kinetic, but I think it's same in Jade.

  1. Install Ubuntu, ROS and MySQL Server (Please search instruction yourself)
  2. Install libmysqlclient-dev (sudo apt-get install libmysqlclient-dev)
  3. Create catkin package (follow tutorial)
  4. Make sure following code in "package.xml"

<build_depend>cmake_modules</build_depend>
<build_depend>libmysqlclient-dev</build_depend>

<run_depend>cmake_modules</run_depend>
<run_depend>libmysqlclient-dev</run_depend>

<export>
  <cpp lflags="-lmysqlclient"/>
</export>

5. Make sure following code in "CMakeList.txt" of your package, sequence is important.

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake-modules) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

find_package(MySqlClient REQUIRED)

include_directories(${MYSQL_INCLUDE_DIRS})

add_definitions(${MYSQL_DEFINITIONS})

target_link_libraries(package_name ${catkin_LIBRARIES} ${MYSQL_LIBRARIES} )

6. Create a folder named "cmake-modules" in your package folder. This name is follow 1st line of code in step 5.

7. Copy contents in this URL: https://cmake.org/Wiki/CMakeUserFindMySQL save it to "FindMySqlClient.cmake", in the folder you created in step 6.

8. In your .cpp file, add this include line:

#include <mysql mysql.h="">


9. Here is a simple sample to get data from sys database.

MYSQL *connection;
MYSQL_RES *result;
MYSQL_ROW row;

connection = mysql_init(NULL); connection = mysql_real_connect(connection, "mysqlserver", "username", "password", "sys", 0, NULL, 0);

if(mysql_query(connection, "SELECT * FROM sys_config"))
{
ROS_INFO("Query Error: %s", mysql_error(connection));
exit(1);
}
else
{

result = mysql_use_result(connection); for(int i=0; i < mysql_field_count(connection); ++i)
{ std::stringstream ss;
row = mysql_fetch_row(result);
if(row <= 0)
{
break;
}
for(int j=0; j < mysql_num_fields(result); ++j)
{
ss << row[j] << " ";
}
ROS_INFO("%s", ss.str().c_str());
} mysql_free_result(result);
}