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

Using the MySQL database in ROS Jade project

asked 2015-10-12 01:17:39 -0500

Sergey I. gravatar image

Hello! Who can give a reference to a working example of using the MySQL database in the ROS Jade project? The simplest example of cpp code mysql_init() and the contents of files cmakelists.txt and package.xml. I've seen similar questions, but there is not complete answer.

edit retag flag offensive close merge delete

Comments

Not an answer, but this shouldn't be any different from using MySQL without ROS: any example you can find elsewhere showing how to create a CMakeLists.txt for a MySQL program should work. ROS/Catkin does not add anything here. Just make sure to build_depend on libmysqlclient-dev.

gvdhoorn gravatar image gvdhoorn  ( 2015-10-12 03:53:44 -0500 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2016-05-30 20:46:14 -0500

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/CMakeUserFindM... 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);  
    }

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-10-12 01:17:39 -0500

Seen: 1,313 times

Last updated: May 30 '16