SQLite – insert the data into the blob

I am trying to use SQLite3 shell to insert binary data into blobs, which means regular SQL statements. This is my table:

CREATE TABLE MYTABLE
(ID INTEGER,
BINDATA BLOB NOT NULL,
SOMEFK INTEGER REFERENCES OTHERTABLE(ID) NOT NULL,
PRIMARY KEY(ID)
);

This is the insert statement I am trying:

INSERT INTO MYTABLE (BINDATA, SOMEFK)
VALUES ( __READBINDATA('/tmp/somefile'), 1);

__READBINDATA(file) is the function I am looking for. Is that possible?

There is no built-in function or shell function to read files into blobs.

< p>However, with the help of the tool hexdump, the content of the file can be converted into a blob literal:

echo "insert into mytable(bindata, somefk) "\
"values(x'"$(hexdump -v -e '1/1 "%02x"' /tmp/somefile)"', 1);"

Then This command can be piped to the sqlite3 shell.

I am trying to use the SQLite3 shell to insert binary data into a blob, which means regular SQL statements. This Is my table:

CREATE TABLE MYTABLE
(ID INTEGER,
BINDATA BLOB NOT NULL,
SOMEFK INTEGER REFERENCES OTHERTABLE (ID) NOT NULL,
PRIMARY KEY(ID)
);

This is the insert statement I am trying:

INSERT INTO MYTABLE (BINDATA, SOMEFK)
VALUES (__READBINDATA('/tmp/somefile'), 1);

__READBINDATA(file) is the function I am looking for. Is that possible?

There is no built-in function or shell function to read the file into the blob.

However, in the hexdumpWith the help of the tool, you can convert the content of the file into a blob literal:

echo "insert into mytable(bindata, somefk) "\
"values(x '"$(hexdump -v -e '1/1 "%02x"' /tmp/somefile)"', 1);"

Then this command can be piped to the sqlite3 shell.< /p>

Leave a Comment

Your email address will not be published.