Search Postgresql Archives

Re: Insert and Retrieve unsigned char sequences using C

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2010/7/22 Vinícius Soares <viniciusams@xxxxxxxxxxxx>:
> Hey,
>
> thanks for your response.
> I did it:
>
>         S8 sql[1500] = "insert into t values ( E'";
>         U8 *msg;
>         msg = PQescapeByteaConn(conn, pending_cmd->cmd.value,
> sizeof(msg_cmd_t), &to_length);
>         for (i=0; i < sizeof(msg_cmd_t); i++){
>             S8 str[20] = "";
>             sprintf(str, "%c", *(msg+i) );
>             strcat(sql, str);
>         }
>         strcat(sql, "' );");
>         PQexec(conn, sql);
>
> But it is very strange because sometimes it works but others times it does
> not work.
> is it right?

That code doesn't look right: you need to make sure your 'to' is big
enough: at has to be at least (2*N)+1 where N is the input size.  it
returns a size_t, not a char*, and you should be able to just sprintf
the 'to' into your query, not copy the chars in a loop.  see the
following fragment:

#define ARGSZ 64
char my_bytea[ARGSZ];
char escaped_bytea[(2*ARGSZ)+1];
int error;
size_t nbytes;

nbytes = PQescapeStringConn (conn, escaped_bytea, my_bytea,
sizeof(my_bytea), &error);

if(error != 0)
  // handle error

sprintf(querybuf, "insert into foo(bytea_col) values (E'%s')", escaped_bytea);

like I said earlier, this is just about the absolute worst way to
transfer a bytea to the server.  I had to look up the docs for
PQescapeStringConn -- I've never once used it my entire life (or it's
even more evil cousin, PQescapeString).

merlin

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux