X-Original-To: pgsql-interfaces-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 2962DDAA10 for ; Fri, 21 Oct 2005 20:30:01 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 24661-07 for ; Fri, 21 Oct 2005 23:29:57 +0000 (GMT) Received: from qproxy.gmail.com (qproxy.gmail.com [72.14.204.202]) by svr1.postgresql.org (Postfix) with ESMTP id D6810DAA19 for ; Fri, 21 Oct 2005 20:29:58 -0300 (ADT) Received: by qproxy.gmail.com with SMTP id v28so396188qbe for ; Fri, 21 Oct 2005 16:30:02 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=lQbi+TDN6VaXCMf9b2UO31cSMQZgZQd18wzk7fS7ykNT1JiWURZNEt5YrU5mJI6PCAz64skmWmgEFnAro6sIVBb91ypKD0YPUm5mXJ2HjRFYhDaKX4PXzMr25M7baCHXTgz9ePEzRnNl9sIN7kpGk9EWK3E1P78a9nve5qfYMlE= Received: by 10.65.236.18 with SMTP id n18mr2291587qbr; Fri, 21 Oct 2005 16:30:02 -0700 (PDT) Received: by 10.65.116.14 with HTTP; Fri, 21 Oct 2005 16:30:02 -0700 (PDT) Message-ID: <7104a7370510211630s294123f6te3465b593c5e32a9@mail.gmail.com> Date: Sat, 22 Oct 2005 02:30:02 +0300 From: Volkan YAZICI To: =?ISO-8859-1?Q?Jochen_Kokem=FCller?= Subject: Re: Reading bytea field Cc: pgsql-interfaces@postgresql.org In-Reply-To: <43593798.5060707@kokemueller.de> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_24272_9399762.1129937402521" References: <43593798.5060707@kokemueller.de> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.222 required=5 tests=[AWL=0.198, RCVD_BY_IP=0.024] X-Spam-Level: X-Archive-Number: 200510/22 X-Sequence-Number: 5784 ------=_Part_24272_9399762.1129937402521 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hi, On 10/21/05, Jochen Kokem=FCller 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 =3D 1, you shouldn't have any problem. Take a look at the examples in the attachment. ------=_Part_24272_9399762.1129937402521 Content-Type: text/x-csrc; name=recv.c; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="recv.c" #include #include #include #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; } ------=_Part_24272_9399762.1129937402521 Content-Type: text/x-csrc; name=send.c; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="send.c" #include #include #include #include #include #include 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; } ------=_Part_24272_9399762.1129937402521--