我想在 bash 脚本中将二进制数据(png、jpg、gif 等)插入到 sqlite3 数据库中。
我使用独立的二进制文件 sqlite3
。如何编写SQL语句?
感谢您的帮助。
最佳答案
正如我在对@sixfeetsix 的回答的评论中提到的,插入数据只是问题的一半。一旦进入,您需要将其取回。我们可以为此使用 xxd。
#A nice hubble image to work with.
echo 'http://asd.gsfc.nasa.gov/archive/hubble/Hubble_20th.jpg' > imageurl.txt
image=imageurl.txt
curl $image > image.jpg
#Insert the image, using hexdump to read the data into SQLite's BLOB literal syntax.
echo "create table images (image blob);" | sqlite3 images.db
echo "insert into images (image) values(x'$(hexdump -ve '1/1 "%0.2X"' image.jpg)');" | sqlite3 images.db 2>&1
#Select just the one image, then use xxd to convert from hex back to binary.
echo "select quote(image) from images limit 1 offset 0;" | sqlite3 images.db | tr -d "X'" | xxd -r -p > newimage.jpg
eog newimage.jpg
关于sql - 如何在 bash 中将二进制数据插入到 sqlite3 数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10353467/