473,595 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(im age.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 11421


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

SELECT bytea_export(im age.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(im age.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.pic s"
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_na me='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.pic s"
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_na me='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
7766
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. The obvious way to do this is by using the static Console type properties, In and Out. When trying to debug the app in the IDE however, this doesn't appear to work. I've edited the project properties and added the necessary text to the "command...
3
3099
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 please help. thanks
2
2648
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
1660
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 argument like '< file', but I couldn't make it. What have I done wrong? --
2
1882
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
2113
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 be coded in BLUeJ using SWing. Thanks
3
6094
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 learned file input/output very well (I self taught all the programming I know...and my c++ book was not good) and so I'm not sure what's wrong with this. The problem is the function void encodemsg(fstream *img, fstream *msg, fstream *out, char key) ...
3
1987
by: silverWolf | last post by:
2. What is the relation between header file ( #include<iostream.h>) and input output function?
3
6198
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 sometimes I get about half of the response string with beginning cut off. About half the time an empty string is returned, the other ~half time good response, and more rarely I get partial response. When I try to use the same Serial instance to...
0
7954
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8379
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8250
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5416
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3872
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3908
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2391
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.