Thread: Insert and Retrieve unsigned char sequences using C
Hi All, I'm developing a system in C and I have a unsigned char pointer that represents a struct and I like to store it in a bytea column in postgreSQL. How can I do it? Example: str_t temp; unsigned char *ptr; ptr = (unsigned char *)&temp; store(ptr); I've already tried some examples, but I didnt have success. Could you help me? Thanks! -- View this message in context: http://old.nabble.com/Insert-and-Retrieve-unsigned-char-sequences-using-C-tp29210308p29210308.html Sent from the PostgreSQL - general mailing list archive at Nabble.com.
vinicius_bra wrote: > I'm developing a system in C and I have a unsigned char pointer that > represents a struct and I like to store it in a bytea column in postgreSQL. The pointer does not represent the struct. > How can I do it? > Example: > > str_t temp; > unsigned char *ptr; > ptr = (unsigned char *)&temp; > store(ptr); > > I've already tried some examples, but I didnt have success. > Could you help me? You won't have any joy storing the raw pointer value, because when you restore it it'll most likely be into a different memory map and the structure to which it used to point will no longer be at the same address, if anywhere. That's because a C pointer doesn't represent a struct, or anything else other than an address. It *points to* the struct. You need to serialize the struct itself then allocate the pointer when you deserialize the struct. -- Lew -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general
vinicius_bra wrote: > I'm developing a system in C and I have a unsigned char pointer that > represents a struct and I like to store it in a bytea column in postgreSQL. The pointer does not represent the struct. > How can I do it? > Example: > > str_t temp; > unsigned char *ptr; > ptr = (unsigned char *)&temp; > store(ptr); > > I've already tried some examples, but I didnt have success. > Could you help me? You won't have any joy storing the raw pointer value, because when you restore it it'll most likely be into a different memory map and the structure to which it used to point will no longer be at the same address, if anywhere. That's because a C pointer doesn't represent a struct, or anything else other than an address. It *points to* the struct. You need to serialize the struct itself then allocate the pointer when you deserialize the struct. -- Lew -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general
vinicius_bra wrote: > I'm developing a system in C and I have a unsigned char pointer that > represents a struct and I like to store it in a bytea column in postgreSQL. The pointer does not represent the struct. > How can I do it? > Example: > > str_t temp; > unsigned char *ptr; > ptr = (unsigned char *)&temp; > store(ptr); > > I've already tried some examples, but I didnt have success. > Could you help me? You won't have any joy storing the raw pointer value, because when you restore it it'll most likely be into a different memory map and the structure to which it used to point will no longer be at the same address, if anywhere. That's because a C pointer doesn't represent a struct, or anything else other than an address. It *points to* the struct. You need to serialize the struct itself then allocate the pointer when you deserialize the struct. -- Lew -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general
On Mon, Jul 19, 2010 at 8:14 PM, vinicius_bra <viniciusams@yahoo.com.br> wrote: > > Hi All, > > I'm developing a system in C and I have a unsigned char pointer that > represents a struct and I like to store it in a bytea column in postgreSQL. > How can I do it? > Example: you have several options: *) encode the memory for the structure PQescapeStringConn and send to PQexec (my least favorite method) *) set up a call to PQexecParams (more work, but faster and no escaping) *) make a composite type on the server and send your structure in a more classical SQL way *) use libpqtypes (this is the most set up work, but the best in the long run) :-) w/libpqtypes: PGbytea b = {sizeof(s), &s}; PGresult *res = PQexecf(conn, "insert into t values (%bytea)", b); PQclear(res); merlin
2010/7/22 Vinícius Soares <viniciusams@yahoo.com.br>: > 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