MySQL Development Kit

Source: Internet
Author: User
Tags mysql client

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;
}
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.