On Thu, June 15, 2006 4:22 pm, Jeroen T. Vermeulen said:
> On Tue, June 13, 2006 18:55, [EMAIL PROTECTED] wrote:
>
>> I'd like to make something like select * from my_table where id=<my_id>
>> the string is built using ostringstream with << C++ operator
>> The query is good, pasted into the psql interpreter runs fine but fails
>> with a syntax error in my code ...
>> I thought about any special character but it would be a problem after a
>> paste in the postgres console too..
>
> There is no way we can guess what the problem is if you don't show us what
> you're trying to do! What is the line of code that fails, and do you get
> a syntax error in your C++ (i.e. when you compile your program) or in the
> SQL query (when you run your program)?
>
>
this is a snippet of code ...
const char* fetchUserData(int abonneid,int cdid,int superviseurid){
connection Conn("dbname="+dbname);
// 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();
cout << " query built = ------- " << s.str()<<"
-----"<< endl;
// 3) utilise la transaction pour executer une
requete ...(select)
result R( T.exec(query) );
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());
}
T.commit();
cout<<"returned from query = " << returned;
return returned.c_str();
}catch (const exception &e) { cout<<"exception recue
.."<<e.what()<<endl; }
}
the query built sounds fine (pasted in psql console works just fine) but
an exception is raised with a message:
exception recue ..ERREUR: erreur de syntaxe à la fin de l'entrée at
character 50
(syntax error at end of entry at caharcter 50)
I find the same entry in the postgres log file but executing in psql
works so I guess a problem with evil characters inserted in the
string...just a guess...
I don't see what is bad with this code ...Is quite basic with a string
generated dynamically...
this is the schema for the queried table:
x83db=> \d usager_tbl
Table «public.usager_tbl»
Colonne | Type | Modificateurs
---------------+---------+---------------
usagerid | integer | not null
cdid | integer | not null
superviseurid | integer | not null
binarydata | bytea |
Index :
«usager_tbl_pkey» PRIMARY KEY, btree (usagerid)
«usager_cdid_idx» btree (cdid)
«usager_superviseur_idx» btree (superviseurid)
I don't know if it's the right way for getting bytea values but this is
not the problem yet ...
Cheers
jerome
>> where could I find more examples of libpqxx usage because I think that
>> the
>> ones provided with the docs are too simple and not sufficient..
>
> Well, like the documentation says, the 91 test programs in the "test"
> directory are also useful as examples! Every function in libpqxx is used
> in some way there.
>
>
> Jeroen
>
>
>
--
Auteur cahiers du programmeur J2EE - Eyrolles 2003
_______________________________________________
Libpqxx-general mailing list
[email protected]
http://gborg.postgresql.org/mailman/listinfo/libpqxx-general