How to manually insert a BLOB into a SQLite database - sql

I want to know how to manually insert a BLOB into my SQLite database. By manually I mean, without using a driver feature that will complete the command like setBytes:
Connection con = DriverManager.getConnection("jdbc:sqlite:database.db");
PreparedStatement stmt = con.prepareStatement("INSERT OR REPLACE INTO test (id, aBlobColumn) VALUES (0, ?)";
stmt.setBytes(1, new byte[] {0x37, 0xe7, 0x9f});
stmt.executeUpdate();
Is it possible to use a command like that:
INSERT OR REPLACE INTO test (id, aBlobColumn) VALUES (0, 37e79f);
or like that:
INSERT OR REPLACE INTO test (id, aBlobColumn) VALUES (0, BLOB(37, e7, 9f));
I don't mind if the command includes base64 data or raw data, I don't want to specifically use hexadecimal.

You can use the following :-
INSERT OR REPLACE INTO test (id, aBlobColumn) VALUES (0, x'37e79f');
However, the value has to be a hex string for it to be a BLOB.

Related

how to execute multiple insert statements from HANA at once

I am having a file file.txt which consists of millions of insert commands like below:
"INSERT INTO T (id, serial) VALUES (0, ARRAY (1) );"
"INSERT INTO T (id, serial) VALUES (1, ARRAY (1, 2) );"
"INSERT INTO T (id, serial) VALUES (2, ARRAY (1, 2, 3, 4, 5) );"
"INSERT INTO T (id, serial) VALUES (3, ARRAY (1, 2) ); "
"INSERT INTO T (id, serial) VALUES (4, ARRAY (1, 2, 3) );"
.....
.....
.....
I want to execute them in HANA using hdbsql or Studio. But how to execute them from the file? Any help is appreciated.
Ideally, the commands shouldn't be enclosed in quotation marks.
Then you can either just run the file as-is in HANA studio (only a good idea if your file is not actually MILLIONS of files, since HANA studio parses the SQL commands first and likely will stall, when there are too many), or you run it via hdbsql with the -I option:
hdbsql -U <your secure store logon key here> -I insert.sql
Thinking about it, the latter method would be the one I would likely go for.

Unable to insert oracle BLOB data greater than 3200 using Insert Statement

I am working on existing project and need to prepare DML for oracle database.
But i am unable to prepare for Insert statement for Blob data due to huge size which is grater than 4000 bytes. Can any one help me?
N.B: I got this error, ORA-06550: String literal too lang.
Only INSERT Statement, no java programe. I just need it to prepare DML like insert statement.
My INSERT STATEMENT:
INSERT INTO APP_PROF('ID', 'IMAGE') VALUES('2', TO_BLOB('4654655665....'))
This Image blob data is greater than 45000bytes
Thanks in advance.
Yes. Use stream, not String/byte array to insert BLOB. Something like this:
PreparedStatement ps = conn.prepareStatement("insert into blobs (blob_value) values (?)");
InputStream in = new StringBufferInputStream(aLagrgeStringValue);
ps.setBinaryStream(1,in,(int)in.length());
ps.execute();
The problem I find here is that you are using string value to insert in a Blob column and error that you are reciving is the limitation error for the input string and not the blob column of the table.
please refer to the below link for further clarification.
How to convert VARCHAR2 to BLOB inside Oracle 11g PL/SQL after ORA-06502

Failing to insert binary stream into a BLOB in hsqldb?

I have the following hsqldb schema (as reported by SQLWorkbench):
DROP TABLE TEST CASCADE;
CREATE TABLE TEST
(
NAME VARCHAR(256),
METADATA VARCHAR(2048),
DATA BLOB
);
GRANT TRIGGER, INSERT, REFERENCES, DELETE, SELECT, UPDATE ON TEST TO DBA;
Next, I am trying to insert a file into the DATA field using the following prepared statement:
MERGE INTO test USING (VALUES ?, ?, ?) I (name, metadata, data) ON (test.name=I.name) WHEN MATCHED THEN UPDATE SET test.data = I.data, test.metadata = I.metadata WHEN NOT MATCHED THEN INSERT (name, metadata, data) VALUES (I.name, I.metadata, I.data)
Here is the code:
String name = ...;
String metadata = ...;
InputStream data = ...;
JDBCDataSource ds = new JDBCDataSource();
ds.setDatabase("jdbc:hsqldb:file:c:/tmp/file.db");
ds.setUser("sa");
ds.setPassword("");
PreparedStatement set = ds.getConnection().prepareStatement(m_setSql);
set.setString(1, name);
set.setString(2, metadata);
set.setBinaryStream(3, data);
set.executeUpdate();
The setBinaryStream fails, because the parameter type is deemed to be VARCHAR, rather than BLOB. Indeed, the function org.hsqldb.jdbc.JDBCPreparedStatement.setBinStream has the following statement:
if (parameterTypes[parameterIndex - 1].typeCode == Types.SQL_BLOB) {
setBlobParameter(parameterIndex, x, length);
return;
}
For the parameterIndex 3 it should enter the if-statement and invoke the setBlobParameter. But, for some reason, typeCode returns 12, which corresponds to VARCHAR, the if-statement is skipped and in the end an org.hsqldb.HsqlException is raised with the message of incompatible data type in conversion.
What am I doing wrong?
The types of the parameter values in the MERGE statement are unknown and default to VARCHAR. You need to cast the BLOB parameter to BLOB.
MERGE INTO test USING (VALUES ?, ?, CAST(? AS BLOB)) I (name, metadata, data)
ON (test.name=I.name)
WHEN MATCHED THEN UPDATE SET test.data = I.data, test.metadata = I.metadata
WHEN NOT MATCHED THEN INSERT (name, metadata, data) VALUES (I.name, I.metadata, I.data)

How to insert Byte[] into the Informix database through the SQL Editor interface

I wonder if there's some way to insert Byte[] in to my database column using the INSERT statement through my SQL Editor.
For example
INSERT INTO Temp (id,name) VALUES(1,'rg_book');
I just wanna to test my data and I don't want to make a user interface (file uploader ,...etc).
How to write this statement?
The CLR Byte array type (Byte[]) maps to a VARBINARY type in Informix DB2. See typing information here.
If your name field is expecting character data, use the VARBINARY function to convert the data into a binary representation of the string. See here.
For example:
INSERT INTO Temp (id, name) VALUES (1, VARBINARY('rg_book'));
If I were you I would do the following (if I've understood your question correctly):
Create test console project
Using Foreach or For (on your Byte[] array) compose required Insert's and (for example) add them to some file on a disk.
Run this script in Management Studio to fill in a table.
FileInfo f = new FileInfo(#"d:\Inserts.txt");
Byte[] list = {0, 1, 2};
using (StreamWriter w = f.CreateText())
{
for (int i = 0; i < list.Length; i++)
{
w.WriteLine("INSERT INTO [TEMP] ([id], [Name]) VALUES ({0}, 'rg_book')", list[i]);
}
}

insert a BLOB via a sql script?

I have an H2 database (http://www.h2database.com) and I'd like to insert a file into a BLOB field via a plain simple sql script (to populate a test database for instance). I know how to do that via the code but I cannot find how to do the sql script itself.
I tried to pass the path, i.e.
INSERT INTO mytable (id,name,file) VALUES(1,'file.xml',/my/local/path/file.xml);
but this fails.
Within the code (java for instance), it's easy to create a File object and pass that in, but directly from a sql script, I'm stuck ...
Any idea?
For testing, you can insert literal hex bytes or use the RAWTOHEX(string) function, as shown below.
create table a(id integer, item blob);
insert into a values(1,'54455354');
insert into a values(2, RAWTOHEX('Test'));
select UTF8TOSTRING(item) from a;
TEST
Test
Addendum: For loading BLOB fields from a file, FILE_READ(fileNameString) may be a useful alternative.
insert into a values(3, FILE_READ('file.dat'));
Not h2database, but may help; https://blog.jerrynixon.com/2009/03/tsql-to-insert-imageblog.html
Example code from the linked blog article, should the link break again:
CREATE TABLE MyTable
(id int, image varbinary(max))
INSERT INTO MyTable
SELECT 1
,(SELECT * FROM OPENROWSET(
BULK 'C:\file.bmp', SINGLE_BLOB) as x )