Recently, I need to use MySQL to access the mysql database when I work. After downloading the source code, I found that some code is very elegant. So it is posted, share it with you. Briefly introduces the Mysql MySQL API is a set of C API packages accessed by MySQL. The main purpose is to combine a variety of Query's operations and STL containers. Mysql can download here: http://www.mysql.com/download_mysql .htmlmlmysql Mail list is: MySQL-PLUSPLUS@lists.mysql.com MySQL API Several typical examples:
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 kind of method is query, it is really simple! TCF Resuse Res = query.use (); row row; // Start SQL 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.rase (output.size () - 1, 1 ); // remove the last one, Output = ")"; query.exec (const string &); // print result cout << Output << end1; return 0;
-------------------------------------------------- -------------------------------------------------- --- query.reset (); query << "Select * from cpptest"; // Print Query statement cout << "Query: << query.preview () << ENDL;
// Execute Query and save to Result MySQLPPP :: result res = query.store (); cout << "record number: << res.size () << endl << endl; // print table data Cout.setf; cout << setw (20) << "item" << SETW (9) << "Num" << SETW (9) << "Weight" << SETW (9) << "Price" << "Date" << Endl << Endl; // Result class provides iTerator is a read-only random display read record mysqlpp :: Row Row; MySQLPP :: Result :: Iterator i; for i = res. begin (); i! = res., (); i) {row = * i; // can display COUT << (20) << Row [0) using the location or name of the column ] .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;}
-------------------------------------------------- -------------------------------------------------- -
A simple query query query = con.query (); stock row; row.set ("Hot Dogs", 100, 1.5, 1.75, "1998-09-25"); query.insert (row); cout << "Query:" query.preview () << end1; query.execute (); print_stock_table (query); ------------------------ -------------------------------------------------- ------------------------------
What I have to do now is to use mysql in ATL.