SQLite: insert binary data from command line

MobileCushion picture MobileCushion · Jul 2, 2012 · Viewed 8.2k times · Source

I have this SQLite table:

create table mytable (
aid INTEGER NOT NULL PRIMARY KEY, 
bid INTEGER NOT NULL, 
image BLOB
);

And I want to insert a binary file into the image field in this table. Is it possible to do it from the sqlite3 command line interface? If so, how? I'm using Ubuntu.

Thank you!

Answer

tzot picture tzot · Mar 12, 2016

The sqlite3 command line interface adds the following two “application-defined” functions:

  • readfile
    which typically is used as: INSERT INTO table(blob) VALUES (readfile('myimage.jpg'))
  • writefile
    which writes a file with the contents of a database blob and returns the bytes written.