Thanks for your help (rupert, chris & so on)
I apologize for this so stupid error...
>> this is a snippet of code ...
>> const char* fetchUserData(int abonneid,int cdid,int superviseurid){
>> connection Conn("dbname="+dbname);
>
> Two things about the connction:
>
> - If you perform this query several times, it will be better to open the
> connection just once, so you may consider passing it as an argument
> inside the function.
>
Yes you 're right ...but this is optimization stuff & premature
optimization is root of all evil isn't it ? :)
> - If the database connection fails, the constructor will throw an
> exception. from the code below, it looks like you want to catch all
> exceptions from the database query. In this case, you can move the
> constructor inside the try block or use lazyconnection.
>
>> // 2) obtient une transaction a partir de la connexion
>> base
>> transaction<> T(Conn, "trx");
>> cout<<"transaction demarree.."<<endl;
>> char* user_data;
>> string returned;
>> try{
>> // construit la requete
>> ostringstream s;
>> string query ="SELECT binarydata FROM usager_tbl
>> where usagerid=";
>> s<<query<<abonneid<<" AND cdid="<<cdid<<" AND
>> superviseurid="<<superviseurid<<";"<<endl;
>> s.flush();
>
> s.flush() is superfluous here, for 2 different reasons: inserting the
> endl manipulator already called flush(), but for ostringstreams, flush()
> is a do-nothing operation.
>
thanks that's nice for my culture..
>
>>
>> cout << " query built = ------- " << s.str()<<"
>> -----"<< endl;
>> // 3) utilise la transaction pour executer une
>> requete ...(select)
>> result R( T.exec(query) );
> ^^^^^
>
> This is why the query fails, this should be result R( T.exec(s.str()) );
>
newbie error ^^^^^^^
>
>
>> int pk = 0;
>> cout<<"requete lancee..iteration sur les
>> resultats"<<R.size()<<endl;
>> for (result::const_iterator c = R.begin(); c !=
>> R.end(); ++c)
>> {
>>
>> returned= c[0].as(string());
>> }
>
> If there are several matching entries, do you care which one is returned?
>
>> T.commit();
>
> If all you do is read, T.commit() is not needed.
>
>> cout<<"returned from query = " <<
>> returned;
>> return returned.c_str();
>
> undefined behaviour. When the function returns, "returned" is destroyed,
> and the char* pointer is no longer valid. You should return a c++-string
> to avoid this problem.
>
thanks for this tricki too :)
>>
>> }catch (const exception &e) { cout<<"exception recue
>> .."<<e.what()<<endl; }
>> }
>>
>
> another point: for the caller of the function, there is not way to
> distinguish between an error in the implementation of the function,
> (e.g. a bad query was constructed), a database connection problem, an
> inconsistent database (e.g. the usager_tbl table does not exist), or no
> suitable record in the table. The entry might also be NUll or the empty
> string. In a real-world application, probably each of these cases needs
> different treatment.
>
yes but that was just a first draft , proper error handling will be done
this night ...
cheers
jerome
> hope this helps,
>
> Rupert
>
> --
> Rupert Kittinger-Sereinig <[EMAIL PROTECTED]>
> Krenngasse 32
> A-8010 Graz
> Austria
> _______________________________________________
> Libpqxx-general mailing list
> [email protected]
> http://gborg.postgresql.org/mailman/listinfo/libpqxx-general
>
--
Auteur cahiers du programmeur J2EE - Eyrolles 2003
_______________________________________________
Libpqxx-general mailing list
[email protected]
http://gborg.postgresql.org/mailman/listinfo/libpqxx-general