How to dump a file stored in a sqlite database as a blob?

前端 未结 4 689
慢半拍i
慢半拍i 2020-12-13 06:32

I have a sqlite3 database. One column has the TEXT type, and contains blobs which I would like to save as file. Those are gzipped files.

The output of the command

4条回答
  •  死守一世寂寞
    2020-12-13 06:57

    sqlite3 cannot output binary data directly, so you have to convert the data to a hexdump, use cut to extract the hex digits from the blob literal, and use xxd (part of the vim package) to convert the hexdump back into binary:

    sqlite3 my.db "SELECT quote(MyBlob) FROM MyTable WHERE id = 1;"  \
    | cut -d\' -f2                                                   \
    | xxd -r -p                                                      \
    > object0.gz
    

    With SQLite 3.8.6 or later, the command-line shell includes the fileio extension, which implements the writefile function:

    sqlite3 my.db "SELECT writefile('object0.gz', MyBlob) FROM MyTable WHERE id = 1"
    

提交回复
热议问题