From: Volkan YAZICI <volkan.yazici@gmail.com>
To: Jochen Kokemüller <jochen@kokemueller.de>
Cc: pgsql-interfaces@postgresql.org
Subject: Re: Reading bytea field
Date: Sat, 22 Oct 2005 02:30:02 +0300
Message-ID: <7104a7370510211630s294123f6te3465b593c5e32a9@mail.gmail.com> (raw)
In-Reply-To: <43593798.5060707@kokemueller.de>
References: <43593798.5060707@kokemueller.de>
Hi,
On 10/21/05, Jochen Kokemüller <jochen@kokemueller.de> wrote:
> I read that it is better to use PQexecParams instead of PQescape. In the
> bytea field is then the picture with every Byte escaped. So the first
> few bytes look like this:
>
> \377\330\377\340\000\020JFIF ...
>
> I wouldn't bother if i knew how to unescape this sequence. I supposed
> that this is more or less an internal representation, but when i try to
> retrieve the data. i get it like this.
If you'd use PQexecParams() for SELECT query with resultType = 1, you
shouldn't have any problem. Take a look at the examples in the
attachment.
Attachments:
[text/x-csrc] recv.c (957B, ../7104a7370510211630s294123f6te3465b593c5e32a9@mail.gmail.com/2-recv.c)
download | inline:
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
#define MAX_BUF_SZ 8192
void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
int
main(void)
{
PGconn *conn;
PGresult *res;
const char fname[] = "newimage.jpg";
FILE *fp;
char *s;
int len;
conn = PQconnectdb("dbname=test");
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Error: PQconnectdb: %s", PQerrorMessage(conn));
exit_nicely(conn);
}
res = PQexecParams(conn, "SELECT data FROM file_arch LIMIT 1",
0, NULL, NULL, NULL, NULL, 1);
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "Error: PQexecParams: %s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
fp = fopen(fname, "w");
if (!fp)
{
perror("fopen");
PQclear(res);
exit_nicely(conn);
}
len = PQgetlength(res, 0, 0);
s = PQgetvalue(res, 0, 0);
fwrite(s, sizeof(char), len, fp);
fclose(fp);
PQclear(res);
PQfinish(conn);
return 0;
}
[text/x-csrc] send.c (1.3K, ../7104a7370510211630s294123f6te3465b593c5e32a9@mail.gmail.com/3-send.c)
download | inline:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <libpq-fe.h>
void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
int
main(void)
{
const char fname[] = "image.jpg";
FILE *fp;
PGconn *conn;
PGresult *res;
struct stat st;
char *buf;
const int l = 1;
const char *param_vals[l];
int param_lens[l];
int param_fmts[l];
conn = PQconnectdb("dbname=test");
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Error: PQconnectdb: %s", PQerrorMessage(conn));
exit_nicely(conn);
}
if(stat(fname, &st) < 0)
{
perror("stat");
exit_nicely(conn);
}
buf = malloc(st.st_size);
if (!buf)
{
perror("malloc");
exit_nicely(conn);
}
fp = fopen("image.jpg", "r");
if (!fp)
{
free(buf);
perror("fopen");
exit_nicely(conn);
}
fread(buf, sizeof(char), st.st_size, fp);
fclose(fp);
param_vals[0] = buf;
param_lens[0] = st.st_size;
param_fmts[0] = 1;
res = PQexecParams(conn, "INSERT INTO file_arch (data) VALUES ($1)",
l, NULL, param_vals, (const int *) param_lens,
(const int *) param_fmts, 1);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "INSERT failed: %s", PQerrorMessage(conn));
PQclear(res);
free(buf);
exit_nicely(conn);
}
PQclear(res);
free(buf);
return 0;
}
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-interfaces@postgresql.org
Cc: volkan.yazici@gmail.com, jochen@kokemueller.de
Subject: Re: Reading bytea field
In-Reply-To: <7104a7370510211630s294123f6te3465b593c5e32a9@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox