Postgresql 使用soci和PL/pgSQL进行变量绑定

Postgresql 使用soci和PL/pgSQL进行变量绑定,postgresql,plpgsql,soci,variable-binding,Postgresql,Plpgsql,Soci,Variable Binding,我使用SOCI库在Oracle和PostgreSQL数据库上执行数据库查询。我收到以下错误: 无法执行查询。致命错误。错误:bind消息提供1个参数,但准备好的语句“”需要0 当我绑定一个变量并执行以下PL/pgSQL时。但是Oracle变量可以正常工作 try { // This doesn't work std::string sql_pg = " \ do $$\n \

我使用SOCI库在Oracle和PostgreSQL数据库上执行数据库查询。我收到以下
错误

无法执行查询。致命错误。错误:bind消息提供1个参数,但准备好的语句“”需要0

当我绑定一个变量并执行以下PL/pgSQL时。但是Oracle变量可以正常工作

try
{
        // This doesn't work
        std::string sql_pg = " \
                               do $$\n \
                               declare\n \
                               n bigint := 1;\n \
                               begin\n \
                               while n <= :1\n \
                               loop\n \
                               insert into ip_table (ip, user) values('0.0.0.1', 'user1');\n \
                               n := n + 1;\n \
                               end loop;\n \
                               end;\n \
                               $$;";

        // This works
        std::string sql_ora = " \
                               declare\n \
                               n number := 1;\n \
                               sql_statement VARCHAR2(500);\n \
                               begin\n \
                               while n <= :1\n \
                               loop\n \
                               sql_statement := q'[insert into ip_table (ip, user) values('0.0.0.1', 'user1')]';\n \
                               execute immediate sql_statement;\n \
                               n := n + 1;\n \
                               end loop;\n \
                               commit;\n \
                               end;";

        int count=4;
        if (session.get_backend_name() == "postgresql")
                session << sql_pg.c_str(), soci::use(count); // error
        else
                session << sql_ora.c_str(), soci::use(count); // works

        std::cout << "executed successfully" << std::endl;
}
catch(const std::exception& err)
{
        std::cout << err.what() << std::endl;
}
试试看
{
//这不管用
std::字符串sql_pg=”\
执行$$\n\
声明\n\
n bigint:=1;\n\
开始\n\

而n在Postgres中无法执行此操作。
do
命令不允许使用外部参数。您只能在客户端应用参数(注意SQL注入)。

在Postgres中不需要PL/pgSQL:
插入ip_表(ip,用户)从generate_series(1,:1)中选择“0.0.0.1”,“user1”
是的。这个例子可能不适合这个问题。但是请考虑一下我是否使用了带有变量的
if
条件。