--------------060506000704060806000405
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
I was working through the samples and to get a first overview, I feel a
smaller sample may serve beginners better. That simple1.cpp really says
little and the util.cpp has the stuff litered around and obscured by
variables changing name with scope. Sure recycling functions makes sense
for the bunch of samples later.
I worked backward to get to this to get some understanding, both about
source and necessary linking:
trisim.cpp to be compiled in the examples dir under Linux (source
install) with
g++ -DHAVE_CONFIG_H -I. -I. -I.. -I../lib -I/usr/include/mysql -g -O2
-c -o trisim.o `test -f 'trisim.cpp' || echo './'`trisim.cpp
/bin/sh ../libtool --mode=link g++ -g -O2 -L/usr/lib/mysql -o trisim
trisim.o util.o ../lib/libmysqlpp.la -lz -lmysqlclient
Will prob only work in the examples dir due to the lib wrappers.
Source reassembled from simple1.cpp and util.cpp:
#include "util.h"
#include <mysql++.h>
#include <iostream>
#include <iomanip>
using namespace std;
int
main(int argc, char *argv[])
{
// Wrap all MySQL++ interactions in one big try block, so any
// errors are handled gracefully.
try {
// Connect to the sample database
mysqlpp::Connection con(mysqlpp::use_exceptions);
bool success = con.connect("mysql_cpp_data",
"localhost", "root", "uPkStpna");
if (!success) {
cerr << "trisim: atabase connection failed." << endl
<< endl;
return 1;
}
// Retrieve the entire stock table from the database server
// we're connected to, and print its contents out.
mysqlpp::Query query = con.query();
// You can write to the query object like you would any
ostream.
query << "select * from stock";
// Show the query string. If you do this, you have to
do it before
// you execute() or store() or use() it.
cout << "Query: " << query.preview() << endl;
// Execute the query and display the result set.
mysqlpp::Result res = query.store();
cout << "Records found: " << res.size() << endl << endl;
cout.setf(ios::left);
cout << setw(21) << "Item" <<
setw(10) << "Num" <<
setw(10) << "Weight" <<
setw(10) << "Price" <<
"Date" << endl << endl;
// Display Rows
// Use the Result class's read-only random access
iterator to walk
// through the query results.
mysqlpp::Result::iterator i;
for (i = res.begin(); i != res.end(); ++i) {
// Convert the Result iterator into a Row
object, for easier
// access.
mysqlpp::Row row(*i);
// Notice that you can use either the column
index or name to
// retrieve the data. Also notice that we do no
explicit
// conversions to match print_stock_row()'s
parameter types:
// Row elements are ColData strings, so they
auto-convert to
// any standard C++ type.
const char* item = row[0];
mysqlpp::longlong num = row[1];
double weight =
row.lookup_by_name("weight");
double price = row[3];
mysqlpp:

ate date = row[4];
// Output first column, the item string. The
UCS2 option shows
// how we can convert the data to get Unicode
output on Windows.
// On modern Unices, the terminal code
interprets UTF-8 data
// directly.
#ifdef USE_WIN32_UCS2
char item_ansi[100];
if (utf8_to_win32_ansi(item, item_ansi,
sizeof(item_ansi))) {
cout << setw(20) << item_ansi << ' ';
}
#else
cout << setw(20) << item << ' ';
#endif
// Output remaining columns
cout << setw(9) << num << ' ' <<
setw(9) << weight << ' ' <<
setw(9) << price << ' ' <<
date << endl;
}
}
catch (exception& er) {
// Catch-all for any exceptions
cerr << "Error: " << er.what() << endl;
return -1;
}
return 0;
}
Sorry if it loses indention, its tabs and spaces mixed I guess.
Henning
--------------060506000704060806000405--