Here's the corrected example:
#include "postgres.h"
#include "executor/spi.h"
#include "fmgr.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(my_spi_function);
Datum
my_spi_function(PG_FUNCTION_ARGS)
{
/* 建立与 SPI 的连接 */
if (SPI_connect() != SPI_OK_CONNECT)
elog(ERROR, "Could not connect to SPI");
/* 执行 SQL 查询 */
int ret = SPI_exec("SELECT * FROM my_table", 0);
if (ret != SPI_OK_SELECT)
elog(ERROR, "SELECT command failed");
/* 处理查询结果等其他逻辑 */
/* 断开与 SPI 的连接 */
SPI_finish();
PG_RETURN_NULL();
}
In this corrected example, SPI_exec is used instead of SPI_execute. The rest of the code structure remains the same. SPI_exec takes the SQL query string and returns an integer result, indicating the success or failure of the query execution.
I appreciate your understanding, and if you have further questions or need additional clarification, feel free to ask.
转载请注明出处:http://www.zyzy.cn/article/detail/8550/PostgreSQL