Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ PostgreSql查询中的参数数据类型出错_C++_Sql_C_Postgresql_Concatenation - Fatal编程技术网

C++ PostgreSql查询中的参数数据类型出错

C++ PostgreSql查询中的参数数据类型出错,c++,sql,c,postgresql,concatenation,C++,Sql,C,Postgresql,Concatenation,我正在尝试使用以下代码在PostgreSQL中执行查询。它是用C/C++编写的,我在声明游标时经常遇到以下错误: DECLARE CURSOR failed: ERROR: could not determine data type of parameter $1 在这里和谷歌上搜索,我找不到解决方案。有人能找到我犯的错误和原因吗 void searchdb( PGconn *conn, char* name, char* offset ) { // Will hold the number

我正在尝试使用以下代码在PostgreSQL中执行查询。它是用C/C++编写的,我在声明游标时经常遇到以下错误:

DECLARE CURSOR failed: ERROR:  could not determine data type of parameter $1
在这里和谷歌上搜索,我找不到解决方案。有人能找到我犯的错误和原因吗

void searchdb( PGconn *conn, char* name, char* offset )
{
// Will hold the number of field in table
int nFields;

// Start a transaction block
PGresult *res  = PQexec(conn, "BEGIN");

if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
    printf("BEGIN command failed: %s", PQerrorMessage(conn));
    PQclear(res);
    exit_nicely(conn);
}

// Clear result
PQclear(res);
printf("BEGIN command  - OK\n");
//set the values to use
const char *values[3] = {(char*)name, (char*)RESULTS_LIMIT, (char*)offset};
//calculate the lengths of each of the values
int lengths[3] = {strlen((char*)name), sizeof(RESULTS_LIMIT), sizeof(offset)};
//state which parameters are binary
int binary[3] = {0, 0, 1};

    res = PQexecParams(conn, "DECLARE emprec CURSOR for SELECT name, id, 'Events' as source FROM events_basic WHERE name LIKE '$1::varchar%' UNION ALL "
            "                 SELECT name, fsq_id, 'Venues' as source FROM venues_cache WHERE name LIKE '$1::varchar%' UNION ALL "
            "                 SELECT name, geo_id, 'Cities' as source FROM static_cities WHERE name LIKE '$1::varchar%' OR FIND_IN_SET('$1::varchar%', alternate_names) != 0 LIMIT $2::int4 OFFSET $3::int4",
    3, //number of parameters
    NULL, //ignore the Oid field
    values, //values to substitute $1 and $2
    lengths, //the lengths, in bytes, of each of the parameter values
    binary, //whether the values are binary or not
    0); //we want the result in text format

// Fetch rows from table
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
    printf("DECLARE CURSOR failed: %s", PQerrorMessage(conn));
    PQclear(res);
    exit_nicely(conn);
}

 // Clear result
PQclear(res);

res = PQexec(conn, "FETCH ALL in emprec");

if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
    printf("FETCH ALL failed");
    PQclear(res);
    exit_nicely(conn);
}

// Get the field name
nFields = PQnfields(res);

  // Prepare the header with table field name
  printf("\nFetch record:");
  printf("\n********************************************************************\n");
    for (int i = 0; i < nFields; i++)
        printf("%-30s", PQfname(res, i));
    printf("\n********************************************************************\n");

// Next, print out the record for each row
for (int i = 0; i < PQntuples(res); i++)
{
    for (int j = 0; j < nFields; j++)
        printf("%-30s", PQgetvalue(res, i, j));
    printf("\n");
}

  PQclear(res);

  // Close the emprec
  res = PQexec(conn, "CLOSE emprec");
  PQclear(res);

  // End the transaction
  res = PQexec(conn, "END");

  // Clear result
 PQclear(res);
}
void searchdb(PGconn*conn,char*name,char*offset)
{
//将保留表中字段的编号
内陆地区;
//启动事务块
PGresult*res=PQexec(连接,“开始”);
如果(PQresultStatus(res)!=PGRES\u命令\u正常)
{
printf(“开始命令失败:%s”,PQerrorMessage(conn));
PQclear(res);
出口(康涅狄格州);
}
//明确的结果
PQclear(res);
printf(“BEGIN命令-OK\n”);
//设置要使用的值
常量字符*值[3]={(字符*)名称,(字符*)结果_限制,(字符*)偏移量};
//计算每个值的长度
int length[3]={strlen((char*)name)、sizeof(RESULTS\u LIMIT)、sizeof(offset)};
//说明哪些参数是二进制的
int binary[3]={0,0,1};
res=PQexecParams(conn,“将emprec游标声明为SELECT name,id,'Events'作为来自Events_basic的源,其中名称类似“$1::varchar%”UNION ALL”
“从场馆缓存中选择名称、fsq_id、“场馆”作为源,其中名称类似于“$1::varchar%”UNION ALL”
“选择名称、地理位置id、“城市”作为来自静态城市的源,其中名称如“$1::varchar%”或在集合中查找(“$1::varchar%”,备用名称)!=0限制$2::int4偏移$3::int4”,
3,//参数数量
NULL,//忽略Oid字段
值,//替换$1和$2的值
Length,//每个参数值的长度(以字节为单位)
binary,//值是否为二进制
0);//我们想要文本格式的结果
//从表中获取行
如果(PQresultStatus(res)!=PGRES\u命令\u正常)
{
printf(“声明游标失败:%s”,PQerrorMessage(conn));
PQclear(res);
出口(康涅狄格州);
}
//明确的结果
PQclear(res);
res=PQexec(conn,“在emprec中获取所有数据”);
如果(PQresultStatus(res)!=PGRES\u元组\u正常)
{
printf(“获取全部失败”);
PQclear(res);
出口(康涅狄格州);
}
//获取字段名
nFields=PQnfields(res);
//准备带有表字段名的标题
printf(“\n蚀刻记录:”);
printf(“\n******************************************************************************************************************\n”);
对于(int i=0;i
尝试更换

WHERE name LIKE '$1::varchar%'

类似地处理所有其他发生的
“$1::varchar%”

有关的更多信息

WHERE name LIKE ($1::varchar || '%')