dbTalk Databases Forums  

More simple sample

mailing.database.mysql-plusplus mailing.database.mysql-plusplus


Discuss More simple sample in the mailing.database.mysql-plusplus forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Henning Diedrich
 
Posts: n/a

Default More simple sample - 07-24-2005 , 11:42 AM






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

Reply With Quote
  #2  
Old   
Robin
 
Posts: n/a

Default Re: More simple sample - 07-24-2005 , 12:30 PM






Here is the test I wrote to make sense of examples.... It is written
with wxWidgets, but you can replace that with Standard C++ stuff.

Hope this helps...

try
=09{
=09=09=09=09
=09=09mysqlpp::Connection link;
=09=09
=09=09mysqlpp::Result result;
=09=09mysqlpp::Row row;
=09=09
=09=09link.connect("database_name","hostname","use rname","password");
=09=09
=09=09wxString sQuery =3D "select * from servers";
=09=09
=09=09mysqlpp::Query query(&link,false);
=09=09query << sQuery;
=09=09
=09=09result =3D query.store();
=09=09=09
=09=09wxString s;
=09=09wxArrayString aResults;
=09=09int nResCounter =3D 0;
=09=09=09
=09=09
=09=09while(row =3D result.fetch_row())
=09=09{
=09=09=09for(unsigned int i =3D 0; i < row.size(); i++)
=09=09=09{
=09=09=09=09aResults.Insert((mysqlpp::cchar*)row[i],nResCounter);
=09=09=09=09nResCounter ++;
=09=09=09}=09=09=09=09
=09=09=09
=09=09}
=09=09
=09=09for(unsigned int j=3D 0; j < aResults.GetCount(); j++)
=09=09=09s +=3D aResults[j] + "\n";
=09=09
=09=09wxMessageBox(s);
=09=09
=09}
=09catch (mysqlpp::BadQuery& er) {
=09=09// Handle any connection or query errors
=09=09wxMessageBox("Bad Query: "+(wxString)er.what());
=09=09
=09}
=09catch (mysqlpp::BadConversion& er) {
=09=09// Handle bad conversions. We still need to catch bad
=09=09// conversions in case something goes wrong when the data
=09=09// is converted into stock.
=09=09wxMessageBox("Bad Conversion");
=09}
=09catch (exception& er) {
=09=09// Catch-all for any other standard C++ exceptions
=09=09wxMessageBox("General Exception");
=09}

--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.