onsdagen den 17 augusti 2005 18:46 skrev Warren Young:
> Björn Persson wrote:
> > But I have to
> > keep the Result until I'm done with all Row and ColData objects, don't I?
>
> Probably not. For example, Result::at() plainly returns Rows by value.
> If you get a Row object, and are then done retrieving results, you can
> reuse the Result object.
Here's a program that does just that – gets a Row and reuses the Result. Then
it tries to use the Row object, but the expected data isn't there.
#include <mysql++/mysql++.h>
using namespace std;
using namespace mysqlpp;
int main(int argc, char *argv[]) {
Connection con(use_exceptions);
con.connect("mysql_cpp_data", "", "user", "pw"); // Adjust as needed.
Query query = con.query();
Result res;
Row row;
// Assign a result to res and copy the only row to row.
query << "select * from stock where item='Hotdog Buns'";
res = query.store();
row = res.at(0);
// Assign a new result to res.
query.reset();
query << "select price from stock";
res = query.store();
// Access the data.
cout << row["item"] << ", "
<< row["num"] << ", "
<< row["weight"] << ", "
<< row["price"] << ", "
<< row["sdate"] << endl;
for (int i = 0; i <= 3; i++) {
cout << res.at(i)["price"] << endl;
}
return 0;
}
Here's what happens on Fedora Core 4 with MySQL++ 2.0.6:
$ g++ mysql++test.cpp -I /usr/include/mysql/ -l mysqlpp -o mysql++test
$ ./mysql++test
terminate called after throwing an instance of 'mysqlpp::BadFieldName'
what(): Unknown field name: sdate
Avbruten (SIGABRT)
On Suse 9.2 with MySQL++ 2.0.6 it prints "Aborted". If I change it to use
separate Result variables I get the expected output on both platforms:
Hotdog Buns, 65, 1.1, 1.1, 1998-04-23
8.79
1.75
0.97
1.1
Björn Persson