Some exquisite demo examples provided by MySQL ++

Source: Internet
Author: User

Recently, I used MySQL ++ to access the MySQL database. After downloading the source code, I checked it and found that some code is very elegant.
So post it and share it with you.

Brief Introduction to MySQL ++

MySQL ++ APIs are a group of C ++ APIs that access MySQL.The main purpose is to better combine various query operations with STL containers.
MySQL ++ can be downloaded here: http://www.mysql.com/download_mysql++.html
The Mail list for MySQL ++ is: mysql-plusplus@lists.mysql.com
Typical examples of MySQL ++ APIs:

Use in to delete records:

Ostringstream strbuf;
Unsigned int I = 0;
Con. real_connect (my_database, my_host, my_user, my_password, 3306, (INT) 0, 60, null );
Query query = con. Query ();
Query <my_query; // This method is very concise! TCF
Resuse res = query. Use ();
Row row;
// Start SQL spelling
Strbuf <"delete from" <my_table <"where" <my_field <"in (";
For (; ROW = res. fetch_row (); I ++)
Strbuf <row [0] <",";
If (! I)
Return 0;
String output (strbuf. STR ());
Output. Erase (output. Size ()-1, 1); // remove the last one,
Output + = ")";
Query.exe C (const string &) output );
// Print the result
Cout <output <Endl;
Return 0;

Bytes -------------------------------------------------------------------------------------------------------
Query. Reset ();
Query <"select * From cpptest ";
// Print the query statement
Cout <"query:" <query. Preview () <Endl;

// Execute the query and save it to result
Mysqlpp: Result res = query. Store ();
Cout <"number of records:" <res. Size () <Endl;
// Print the data in the table
Cout. SETF (IOs: Left );
Cout <SETW (20) <"item" <
SETW (9) <"num" <
SETW (9) <"weight" <
SETW (9) <"price" <"date" <Endl;

// The iterator provided by the result class is read-only and random display of read records
Mysqlpp: Row row;
Mysqlpp: Result: iterator I;
 
For (I = res. Begin (); I! = Res. End (); ++ I ){
Row = * I;
// You can use the column position or name to display
Cout <SETW (20) <row [0]. c_str () <
SETW (9) <row [1]. c_str () <
SETW (9) <row. lookup_by_name ("weight"). c_str () <
SETW (9) <row [3]. c_str () <
Row [4] <Endl;
}

Bytes -----------------------------------------------------------------------------------------------------

A simple query
Query query = con. Query ();
Stock row;
Row. Set ("hot dogs", 100, 1.5, 1.75 ");
Query. insert (ROW );
Cout <"query:" <query. Preview () <Endl;
Query.exe cute ();
Print_stock_table (query );
Bytes --------------------------------------------------------------------------------------------------------

What I want to do now is to use MySQL ++ in ATL.

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.