`
marsprj
  • 浏览: 65107 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Postgres插入bytea数据

    博客分类:
  • GIS
阅读更多
#######################################################################

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;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics