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.