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.


Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/61/4091161/html/wptalreg/wp-content/plugins/vsf-simple-stats/vsf_simple_stats_shutdown.php on line 17

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/content/61/4091161/html/wptalreg/wp-content/plugins/vsf-simple-stats/vsf_simple_stats_shutdown.php on line 17

Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in /home/content/61/4091161/html/wptalreg/wp-content/plugins/vsf-simple-stats/vsf_simple_stats_shutdown.php on line 17

Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/61/4091161/html/wptalreg/wp-content/plugins/vsf-simple-stats/vsf_simple_stats_shutdown.php on line 45

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/content/61/4091161/html/wptalreg/wp-content/plugins/vsf-simple-stats/vsf_simple_stats_shutdown.php on line 45

Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/61/4091161/html/wptalreg/wp-content/plugins/vsf-simple-stats/vsf_simple_stats_shutdown.php on line 47

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/content/61/4091161/html/wptalreg/wp-content/plugins/vsf-simple-stats/vsf_simple_stats_shutdown.php on line 47

Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in /home/content/61/4091161/html/wptalreg/wp-content/plugins/vsf-simple-stats/vsf_simple_stats_shutdown.php on line 47