Re: Import binary file to h2 database?

272 views
Skip to first unread message

litailang

unread,
May 25, 2013, 1:35:49 AM5/25/13
to h2-da...@googlegroups.com
hi,

try like this query.

create table file(name varchar, data binary);
insert into file values ('filename', file_read('/path/to/file'));

litailang

2013/5/23 Andreas Fischer <fischer.a...@googlemail.com>:
> Hey there,
>
> What is the nicest/fastest way to import a binary file content into a data
> base table?
> I have some binary files and i want to merge them into one data base table.
> The key is the file name and a field of type BINARY named 'data' should
> contain the exact copy of the file content.
>
> Thanks in advance.
>
> --
> You received this message because you are subscribed to the Google Groups
> "H2 Database" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to h2-database...@googlegroups.com.
> To post to this group, send email to h2-da...@googlegroups.com.
> Visit this group at http://groups.google.com/group/h2-database?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Andreas Fischer

unread,
May 27, 2013, 5:49:21 AM5/27/13
to h2-da...@googlegroups.com
Hi! OK this worked out. But i recognized some funny things:

From the beginning:
I first used an algorithm that read the file and translated it's bytes into a hex string (e.g.: byte fileByte=125 -> String hexByte="7D"). Then i had a hex string which represents the file content and i saved it to the data base like this:
String sql = "INSERT INTO myTable(fileName, data) VALUES('"+file.getName()+"', X'"+fileContentAsHexString+"')";

If i compare the data fields of my algorithm with the data fields inserted by FILE_READ(), thy have the same number of bytes (that says my h2 console).

But the data base file itself is almost doubled the size when i use FILE_READ()!!!

Why is that?
Reply all
Reply to author
Forward
0 new messages