Developping linux mysql application under ubuntu

apt-get this:

apt-get install libmysqlcppconn5 libmysqlcppconn-dev libmysqlclient-dev -y

Note here that you need to check the version of libmysqlcppconn. It might be higher then 5.

Include this library in your linker code:libmysqlcppconn.so.

To create a connection:

  1. create a driver
  2. call the drivers connect method (it might throw an error, so use a try catch block).

includes and name spaces:

#include<mysql_connection.h>
#include<mysql_driver.h>
#include<cppconn/statement.h>
using namespace sql::mysql;

You don’t have to use name spaces obviously, but this will usually make things easier.

create the driver:MySQL_Driver * p_driver = sql::mysql::get_driver_instance();
Then you can use it to connect with the connect method. Sample connection strings can be found here: http://dev.mysql.com/doc/refman/5.1/en/connector-cpp-examples-query.html.

creating the statement object:

p_stmt=p_connection->createStatement(); The important thing here is to remember to include the right headers. many of them are under cppconn, including the statement (<cppconn/statement.h>) and RecordSet(<cppconn/resultset.h>).

using the statement object:

ResultSet * set = p_stmt->executeQuery(“select username from users_tbl”);

The statement has many more options to look at. also, you should be looking at the prepare statement object(cppconn/prepared_statement.h).

Getting values from the record set

The first value from the record set can be fetched with the first() method.

To get the rest of the data use the next() method. Each of these return boolean that state the result.

To get a value from Result set use one of the getXXXXX(index column). methods. Note that the column index starts with 1.

 

Leave a Reply

You must be logged in to post a comment.