C++ 如何在为libpqxx/C+准备的SQL语句中绑定引号内的参数+;
我有一个PostGIS扩展的PotgreSQL数据库,我想知道如何使用C++ 如何在为libpqxx/C+准备的SQL语句中绑定引号内的参数+;,c++,postgresql,prepared-statement,postgis,libpqxx,C++,Postgresql,Prepared Statement,Postgis,Libpqxx,我有一个PostGIS扩展的PotgreSQL数据库,我想知道如何使用pqxx::connection\u base::prepare创建带有PostGIS函数的SQL语句,例如st\u distance() 当我向PostgreSQL查询SQL语句时,如下所示: database_name=# select st_geomfromtext('POINT(50.0 90.0)', 4326); 我得到的答复是: st_geomfromtext -------
pqxx::connection\u base::prepare
创建带有PostGIS函数的SQL语句,例如st\u distance()
当我向PostgreSQL查询SQL语句时,如下所示:
database_name=# select st_geomfromtext('POINT(50.0 90.0)', 4326);
我得到的答复是:
st_geomfromtext
----------------------------------------------------
0101000020E610000000000000000049400000000000805640
< C++ > 如何在LIbpqxx上获得上述代码?
我试过:
#include <iostream>
#include <pqxx/pqxx>
int main()
{
try {
pqxx::connection conn("dbname=xxx user=yyy password=zzz");
pqxx::work xaction(conn);
conn.prepare(
"text to geometry point",
"select st_geomfromtext('POINT($1 $2)', 4326);"
);
pqxx::result selected = xaction.prepared("text to geometry point")(50.0)(90.0).exec();
for (auto row : selected) {
for (auto col : row) {
std::cout << col.c_str() << "\t";
}
std::cout << "\n";
}
std::cout << std::endl;
conn.disconnect();
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
libpqxx可能无法分析'点($1$2)
,因为其中的参数以文本形式引用,因此无法绑定参数
我不知道如何纠正这个错误
这是我想做的一个简单的例子。我需要在实际工作中使用
pqxx::connection\u base::prepare
。由于$1
位于单引号内,因此它不会被解释为参数,而是字符串文字
试一试
我跟随Laurenz的评论,将SQL语句从“选择st|u geomfromtext('POINT($1$2)'4326);”更改为“选择st|u geomfromtext('POINT('124; |$1 | |')”,'4326);,并得到了正确的响应。谢谢
ERROR: bind message supplies 2 parameters, but prepared statement "text to geometry point" requires 0
'POINT(' || $1 || ' ' || $2 || ')'