Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite - Insert data into blob

Tags:

shell

sql

sqlite

I'm trying to insert binary data into a blob using SQLite3's shell, which means regular SQL statements. Here's my table:

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

And this is the kind of insert statement I'm trying:

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

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

like image 273
MechMK1 Avatar asked Oct 12 '12 19:10

MechMK1


1 Answers

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

However, with the help of the hexdump tool, it's possible to transform a file's contents into a blob literal:

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

This command can then be piped into the sqlite3 shell.

like image 60
CL. Avatar answered Oct 15 '22 02:10

CL.