Download case code
Download SDK
Introduction:
We recommend a simple development kit for MYSQL client development. All functions are defined as two contained files. You can use the provided API and boost: shared_ptr in the header file to manage MySQL connections and result settings.
Code:
MySQL Development Kit
The class structure chart is as follows:
This structure is very simple. The only difference is that the member function cmysqlconn: query. This function has a touch parameter 'processquery ', which is an API of the 'policy' class used by the customer. Different users have different database usage intentions. Some users want to get data to draw conclusions. Some users do not need to return any data if they want to insert or update data. Other users may only obtain the data ID type, using the 'where' command.
Based on the addressing scheme in Andrei Alexandrescu's book "Modern C ++ design", the 'query' member functions Use templates to distinguish them from common query algorithms.
I have defined three classes for cmysqlconn: query. The following are examples:
Data needs to be returned to query the database:
Cmysqlset rset = conn. query <withdata> ("select * From mytable ");
No data is required to query the database:
Bool ret = conn. query <nodata> ("insert into mytable values
(5000, 'testing message ')");
The query database only obtains the value:
Pair <bool, string> value = conn. query <checkonerecord>
("Select field from mytable where errorcode = '000000 '");
Users can define their own classes as follows:
Struct nodata
{
// Define your return type
Typedef boolreturntype;
// Define your failed return function for the query Failure Case
Static returntype returninfail () {return false ;}
// Define your records processing implementation after
// Query statement is successfully issued.
Static returntype deepquery (connptr PTR) {return true ;}
};
This is an example of the Code for using the SDK:
Collapseint test ()
{
String query = "select * From mytable ";
Cmysqlconn conn ("myhost", "mytable", "login", "password ");
If (conn)
{
Cmysqlset rset = conn. query <withdata> (query );
If (rset)
{
Cout <"I got a result ";
Unsigned size = rset. numberofrecords ();
For (unsigned int I = 0; I <size; I ++ ){
Cmysqlrow ROW = rset. getnextrow ();
If (ROW)
{
For (unsigned Int J = 0; j <row. numberoffields (); j ++)
{
Cout <row [J];
}
Cout <Endl;
}
}
}
Cout <"last error is
"<Conn. getlastfiled1 () <": "<conn. getlasterrorstring () <Endl;
String updatequery = "insert into mytable values (5000, 'testing message ')";
Conn. query <nodata> (updatequery );
//
// Query single data test
//
Cout <"query single data..." <Endl;
Pair <bool, string> value = conn. query <checkonerecord>
("Select filed1s from mytable where filed1 = '2013 '");
If (value. First)
{
Cout <"get value of" <value. Second <Endl;
}
}
Return 0;
}