Re: import_bytea function - Mailing list pgsql-general

From Thomas Kellerer
Subject Re: import_bytea function
Date
Msg-id nt7nr7$2ku$1@blaine.gmane.org
Whole thread Raw
In response to Re: import_bytea function  (Stephen Davies <sdavies@sdc.com.au>)
Responses Re: import_bytea function
Re: import_bytea function
List pgsql-general
Stephen Davies schrieb am 07.10.2016 um 10:46:
>> You can store the contents of a file in a bytea using plain JDBC no lo_import() required
>>
>>     String sql = "insert into images (id, image_data) values (?,?)";
>>     Connection con = ....;
>>     File uploaded = new File("...");
>>     InputStream in = new FileInputStream(uploaded);
>>     PreparedStatement pstmt = con.prepareStatement(sql);
>>     pstmt.setInt(1, 42);
>>     pstmt.setBinaryStream(in, (int)uploaded.length());
>>     pstmt.executeUpdate();
>>
>> This *only* works with bytea column, not with "large objects".
>>
>> In production code you obviously need to close all resources and handle errors.
>> I left that out for simplicity.

> That looks reasonable but I need to update rather than insert and my similar
> code with sql="update part set pic=? where id=3" did not work.

That *will* work (using that myself for updates as well).

What exactly is your problem? What was the error/exception?



pgsql-general by date:

Previous
From: Stephen Davies
Date:
Subject: Re: import_bytea function
Next
From: Rakesh Kumar
Date:
Subject: pgpool question