473,397 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Simplfied Bytea input/output?

Hi,
I am adding image and large object support in my Cocoa
postgresql browser.

Are there going to be any enhanced bytea support functions
coming along?

It seems sorta silly to have to write customized C code
to import a file into a bytea field.

Maybe something like

CREATE TABLE image (
name text,
raster bytea
);

INSERT INTO image (name, raster)
VALUES ('beautiful image', bytea_import('/etc/motd'));

SELECT bytea_export(image.raster, '/tmp/motd') FROM image
WHERE name = 'beautiful image';

Jerry
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #1
4 11402


On Wed, 4 Aug 2004, Jerry LeVan wrote:
INSERT INTO image (name, raster)
VALUES ('beautiful image', bytea_import('/etc/motd'));

SELECT bytea_export(image.raster, '/tmp/motd') FROM image
WHERE name = 'beautiful image';


Doing this kind of file/read write is only useful if the client and server
are on the same machine which is a kind of limiting restriction if you are
trying to make a generic data browsing tool. Further any function that
does file access like this must run with superuser permissions to prevent
misuse which is another serious restriction.

Kris Jurka

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #2
Thanks for the reply Kris...

I guess I did not state my case very clearly, what I would
like to see is a simple flexible tool that can get binary
data into the database...I can get it out fairly easy.

Of course it would be easy to "update" pg_hba.conf if you could
write files as postgres

Jerry

On Aug 4, 2004, at 3:40 PM, Kris Jurka wrote:


On Wed, 4 Aug 2004, Jerry LeVan wrote:
INSERT INTO image (name, raster)
VALUES ('beautiful image', bytea_import('/etc/motd'));

SELECT bytea_export(image.raster, '/tmp/motd') FROM image
WHERE name = 'beautiful image';


Doing this kind of file/read write is only useful if the client and
server
are on the same machine which is a kind of limiting restriction if you
are
trying to make a generic data browsing tool. Further any function that
does file access like this must run with superuser permissions to
prevent
misuse which is another serious restriction.

Kris Jurka

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #3
Hi,

I have been looking for a fairly simple way to upload data into
a bytea field without having to write custom C code for each table
that contains a bytea field.

With some good advice from Daniel Verite and reading the fine manual
here is my procedure for uploading files to bytea fields.

1) Create an "upload table" that might look like
\d pics Table "public.pics"
Column | Type | Modifiers
--------+---------
+---------------------------------------------------------
info | text |
image | bytea |
ident | integer | not null default
nextval('public.pics_ident_seq'::text)

2) I wrote a single C procedure that would upload to this table
here is a fragment of the code:

int usage()
{
fprintf(stderr,"loadBytea <connect string> <table> <comment>
<file>\n");
fprintf(stderr," This will insert the comment (a string) and the
contents of file\n");
fprintf(stderr," into the first two columns of the specified
table.\n");
exit(0);
}

The core of the program is a wrapper around the PQexecParams routine.

The source code for the program is located here:
http://homepage.mac.levanj/Cocoa/programs/loadBytea.c

3) Once the file is uploaded (say to table pics) I can use the update
command to load the bytea field into the desired table, perhaps
something like:

update person set picture = pics.image
from pics
where pics.ident=15 and person.first_name='Marijo'

Once the bytea field has been loaded into the target, it
can be deleted from the upload table ( or a reference could
be placed in the "person" table to the appropriate picture and
all of the pictures keep in the upload table).

This method does not scale well to bulk input but I think it
would not be difficult to rewrite the loadBytea.c program as
needed.

Jerry
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #4
Incredibly, I was just sitting down to do something similar for a
problem I have when I read this email. I'm going to do a temp table
too, but I did think of another solution. It would work for me but it's
a little complex for my stage of PG expertise: Create a user-defined
type for "pic" and define an input/output function for it. Someone can
correct me but my understanding says that probably won't be as fast as a
direct bulk copy, but it can't be slower than the temp table method and
it is certainly simpler.

Jerry LeVan wrote:
Hi,

I have been looking for a fairly simple way to upload data into
a bytea field without having to write custom C code for each table
that contains a bytea field.

With some good advice from Daniel Verite and reading the fine manual
here is my procedure for uploading files to bytea fields.

1) Create an "upload table" that might look like
\d pics Table "public.pics"
Column | Type | Modifiers
--------+---------
+---------------------------------------------------------
info | text |
image | bytea |
ident | integer | not null default
nextval('public.pics_ident_seq'::text)

2) I wrote a single C procedure that would upload to this table
here is a fragment of the code:

int usage()
{
fprintf(stderr,"loadBytea <connect string> <table> <comment>
<file>\n");
fprintf(stderr," This will insert the comment (a string) and the
contents of file\n");
fprintf(stderr," into the first two columns of the specified
table.\n");
exit(0);
}

The core of the program is a wrapper around the PQexecParams routine.

The source code for the program is located here:
http://homepage.mac.levanj/Cocoa/programs/loadBytea.c

3) Once the file is uploaded (say to table pics) I can use the update
command to load the bytea field into the desired table, perhaps
something like:

update person set picture = pics.image
from pics
where pics.ident=15 and person.first_name='Marijo'

Once the bytea field has been loaded into the target, it
can be deleted from the upload table ( or a reference could
be placed in the "person" table to the appropriate picture and
all of the pictures keep in the upload table).

This method does not scale well to bulk input but I think it
would not be difficult to rewrite the loadBytea.c program as
needed.

Jerry
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if
your
joining column's datatypes do not match

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bill Cohagan | last post by:
I'm writing a console app (in C#) and I want to be able to redirect the standard input/output streams when it's run at a command prompt. IOW I want to support the "<" and ">" redirection syntax....
3
by: qbschmidt | last post by:
i can input and output to a text file as long as the file is in the same directory as my c++ program. but i cant figure out how to input/output to a file in another directory i.e. I:\c++projects...
2
by: qazmlp1209 | last post by:
FAQ at "http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.6" states that the following program will not work properly: --------------- int main() { char name; int age; for (;;)
3
by: ES Kim | last post by:
Hi, I am using VC++ .NET 7.0. I'd like to debug my console program, using redirection of input/output for convenience. Help menu tells me it's possible to specify a file name as a command-line...
2
by: Key9 | last post by:
Question about basic input output. HI All I am a new C++er Suppose I am designing a i/o device . system have screens . system have keyboards.
4
by: lenin42001 | last post by:
Can someone be off assistance? We're trying to input/output some patients data in a hypothetical form. The records display sequentially & can be retrieved from a data store................This must...
3
by: John Williams | last post by:
I'm writing a stagenography program to experiment with how it works. The algorithm I'm using appears to be producing the correct result...however I'm struggling with the file input. I never...
3
by: silverWolf | last post by:
2. What is the relation between header file ( #include<iostream.h>) and input output function?
3
by: Rainy | last post by:
Hello! I'm having some trouble with pyserial package, I'm sending commands and reading responses from a custom pcb, and sometimes I get a proper response, at other times I get nothing, and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.