Another powerful feature provided by mysql++ is the template query, which provides a similar mechanism to printf in C: You provide mysql++ with a query string that contains fixed strings and variable placeholders, which you can then replace with variables for these placeholders.
The following example shows how to use this feature
1#include <iostream>2#include <mysql++.h>3#include <errno.h>4#include <stdlib.h>5#include <stdio.h>6 7 using namespacestd;8 9 intMain ()Ten { OneMysqlpp::connection Conn (false); A //Connect to the database - if(Conn.connect ("Mysql_cpp_data","127.0.0.1","Comoon","")) - { the //build a template query -Mysqlpp::query Query = Conn.query ("SELECT * from stock where item =%0q"); - query.parse (); - //get query result in a storeresult +Mysqlpp::storequeryresult res = Query.store ("hotdog Buns"); - if(RES) + { Acout << res[0]["Item"] <<Endl; atcout << res[0]["Num"] <<Endl; -cout << res[0][" Price"] <<Endl; - } - Else - { -Perror ("Failed to get data!"); in exit (exit_failure); - } toQuery.reset ();//Reset Previous template query data +Query <<"Update stock Set item =%0q where item =%1q"; - query.parse (); theMysqlpp::simpleresult res1 = Query.execute ("ABC","hotdog Buns"); * } $ ElsePanax Notoginseng { -Perror ("Connect Failed"); the exit (exit_failure); + } A return 0; the}
Query.parse () before the code is used to set up the template, the parse () call is used to make it effective, and you can use this template multiple times by invoking several functions of the query from the beginning of the entry
Create a template query
To create a template query, you first have to insert the content into the query object, use a number to represent the placeholder you want to replace, and then call the parse () function to tell the query object, which is a query template that needs to be parsed
" Select (%2:field1,%3:field2) from stock where%1:wheref =%0q:what " ; Query.parse ();
The placeholder is in the form of:
%### (modifier) (: name) (:)
"# # #" is a three-bit maximum number that represents a sequence of arguments to a Sqlqueryparms object, starting with 0
Modifier can be any one of the following:
% print an actual "%" symbol
"" No matter what, do not quote or ignore
Q is enclosed in single quotes and is not interpreted as a keyword.
Q will be enclosed in quotation marks, but it will not avoid keyword detection.
Setting parameters
When querying, you need to specify the parameters. " Parm0 "corresponds to the first parameter, and so on.
For example:
Storequeryresult res = Query.store ("Dinner Rolls""item"" Item"price")
The above query will be parsed as:
Select from where " Dinner Rolls "
Default parameters
Template query mechanism, which allows you to set default parameters. You just need to Query::template_defaults assign a value to the array corresponding to the element.
query.template_defaults[1"item"; query.template_defaults[" wheref""item";
This default parameter mechanism is similar to the function default parameter of C + +.
mysql++ Learning (iv)------template query