(one more time, for the list...)

Hi jerome.

I just added some comments...

[EMAIL PROTECTED] schrieb:
>>
> 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.

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


> 
>                         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()) );
                                                              ^^^^^^^


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

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

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

Reply via email to