Re: [OT] PostgreSQL: bytea help needed.
From: mlw (mlw_at_nospam.no)
Date: 02/03/05
- Next message: True211: "Re: [OT] PostgreSQL: bytea help needed."
- Previous message: Mike Cox: "Re: [OT] PostgreSQL: bytea help needed."
- In reply to: Mike Cox: "Re: [OT] PostgreSQL: bytea help needed."
- Next in thread: Mike Cox: "Re: [OT] PostgreSQL: bytea help needed."
- Reply: Mike Cox: "Re: [OT] PostgreSQL: bytea help needed."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 03 Feb 2005 16:43:29 -0500
Mike Cox wrote:
> Christopher Browne <cbbrowne@acm.org> writes:
>
>> Centuries ago, Nostradamus foresaw when Mike Cox <mikecoxlinux@yahoo.com>
>> would write:
>> > Does anyone know how to add data to a bytea in PostgreSQL? I googled
>> > the archives, and they mostly talk about PHP. I'd like to put binary
>> > data in the bytea from my local filesystem, but google comes up empty
>> > other than using PHP.
>>
>> Well, you draw the data in, and then append it to the field.
>
> Well this is elementary stuff. Lets say I wanted to pull a jpg into
> the database from my local filesystem for example.
My first reaction would be to say you are insane. Using a database as a file
system isn't its best used. I would store the file path and name and have a
separate volume or directory for the image. Especially if you are using
large images.
> How would what you
> just wrote help me in this regard? I already know how to append to a
> bytea from within the database and using an interface like JDBC, but
> what about the psql terminal?
You probably can't do it in psql unless you create a C function to read the
data in, parse it and escape the characters correctly.
>
>> tutorial=# create table ba1 (stuff bytea);
>> CREATE TABLE
>> tutorial=# insert into ba1 values ('foo');
>> INSERT 2749794 1
>> tutorial=# update ba1 set stuff = stuff || ' more stuff';
>> UPDATE 1
>> tutorial=# select * from ba1;
>> stuff
>> ----------------
>> foo more stuff
>> (1 row)
>>
>> Your application needs to be in some language; that presumably needs
>> to be where the data is loaded from disk into a variable. You would
>> then push the variable into the update query.
>>
>> When you were googling, you mostly found PHP details; can you suggest
>> details about what language you're planning to use instead, be that
>> Perl, C, Python, Java, or such?
>
> Well I wanted to grab binary data off my filesystem directly from the
> psql terminal. Something like lo_import that is used for BLOBS or the
> COPY command which is used to pull text data in.
Again, you may want to create a C sql function to do this as I don't think
the psql program includes this functionality.
Something like this:
create or replace function bytealoader(varchar)
returns bytea
as 'mylib.so','bytealoader'
language 'c';
< This function can be made more efficient and better, but this is just
typing off the top of my head. >
Datum bytealoader(PG_FUNCTION_ARGS)
{
if(fcinfo->nargs >= 1)
{
text *tp = PG_GETARG_TEXT_P(0);
char *filename = (char *)VARDATA(tp);
FILE *fi = fopen(filename);
if(fi)
{
long file_len;
fseek(fi, 0, SEEK_END);
file_len = ftell(fi);
fseek(fi,0,SEEK_SET);
p = palloc(file_len + VARHDRSZ);
VARATT_SIZEP(p) = file_len;
fread(VARDATA(p), file_len, 1, fi);
fclose(fi);
PG_RETURN_BYTEA(p);
}
}
PG_RETURN_NULL;
}
>
> I know how to pull data from Java and those other interfaces, but then
> I'd have to get JDBC or install Apache, PHP etc. I want to be able
> to, like I posted, be able to do this within the psql terminal.
The psql terminal is designed to use a text console. Dumping binary image
data to a text screen will make a lot of noise can put a lot of jibberish
on the screen.
- Next message: True211: "Re: [OT] PostgreSQL: bytea help needed."
- Previous message: Mike Cox: "Re: [OT] PostgreSQL: bytea help needed."
- In reply to: Mike Cox: "Re: [OT] PostgreSQL: bytea help needed."
- Next in thread: Mike Cox: "Re: [OT] PostgreSQL: bytea help needed."
- Reply: Mike Cox: "Re: [OT] PostgreSQL: bytea help needed."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|