---
title: "Postgres插入bytea数据"
author: "marsprj"
date: "2010-03-23T16:15:49Z"
tags: ["C", "C#", "C++", "SQL"]
categories: ["编程语言"]
slug: "postgres-bytea"
draft: false
lastmod: "2010-03-23T16:15:49Z"
id: 622352
url: "https://yingchao.iteye.com/blog/622352"
---

\[code="java"] ####################################################################### SQL: CREATE TABLE test(id serial NOT NULL,bt bytea); ####################################################################### #include "stdafx.h" #include "stdlib.h" #include "stdio.h" #include #include 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; } \[/code]