Question

I'm trying to create a MySQL statement with MySQL++ library, but I can't find how to do it. I found something called query templates, but, is it like a real MySQL statement?

Was it helpful?

Solution

So I was just checking out this and it looks like using template queries is, in part, the means by which an SQL statement is passed to MySQL++. So I guess that would be, in part, the route to take to create a MySQL statement with MySQL++.

    // Establish the connection to the database server.
    mysqlpp::Connection con(mysqlpp::examples::db_name,
            cmdline.server(), cmdline.user(), cmdline.pass());

    // Build a template query to retrieve a stock item given by
    // item name.
    mysqlpp::Query query = con.query(
            "select * from stock where item = %0q");
    query.parse();

Also, in response to:

is it like a real MySQL statement?

What's going on is that the compiler is taking your c++ code which contains the statement that you want to execute and then communicating with the MySQL db for you, at which point the 'real' SQL statement is used.

OTHER TIPS

The most straightforward way is to use the query() public member function of the Connection class.

There is no real need to use templates, although you can if you want to.

The query() function takes an SQL statement as string value and returns a Query object.

The class' query functions are defined as follows:

  • query (const std::string &qstr)
  • query (const char *qstr=0)

You can find lots of details in the reference guide.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top