#######################################################################
SQL: CREATE TABLE test(id serial NOT NULL,bt bytea);
#######################################################################
#include "stdafx.h"
#include "stdlib.h"
#include "stdio.h"
#include <libpq-fe.h>
#include <string>
PGconn *pgConnection;
#pragma comment(lib, "libpq.lib")
#pragma comment(lib, "geogisf.lib")
int main(int argc, char* argv[])
{
char szConnectionString[_MAX_PATH];
const char* szServer = "10.0.0.134";
int iPort = 5432;
const char* szDatabase = "test";
const char* szUser = "postgres";
const char* szPassword = "postgres";
sprintf(szConnectionString, "hostaddr=%s port=%d dbname=%s user=%s password=%s",
szServer, iPort, szDatabase, szUser, szPassword);
pgConnection = PQconnectdb(szConnectionString);
char* strtext = "delivers the latest breaking news and information on the latest top";
size_t to_length;
unsigned char* bytea = PQescapeByteaConn(pgConnection,
(unsigned char*)strtext,
strlen(strtext),
&to_length);
std::string strsql;
strsql = "insert into test (bt) values ('";
strsql += (char*)bytea;
strsql += "')";
PGresult* pgResult = PQexec(pgConnection, strsql.c_str());
int status = PQresultStatus(pgResult);
if(status!=PGRES_COMMAND_OK)
{
printf("insert error");
}
PQclear(pgResult);
PQfinish(pgConnection);
return 0;
}
Postgres插入bytea数据
最新推荐文章于 2022-02-25 18:19:43 发布