sql - 如何在 bash 中将二进制数据插入到 sqlite3 数据库中?

标签 sql bash sqlite binary

我想在 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/

相关文章:

mysql - 我在哪里可以找到我的 mySQL 数据库(使用 locate 没有帮助)?

c# - 'Distinct' 操作无法应用于指定参数的集合ResultType

Mysql更新列替换部分文本

c# - 使用 Npgsql 的多个游标?

regex - 使用删除

bash - 我可以使用批处理文件调用 git bash 然后向其传递命令吗?

javascript - 对 javascript 对象的复杂 SQL 查询

linux - 如何使用 mailx 将脚本文件发送给 Linux 主机上的另一个用户

iphone - SQLite "SQLITE MISUSE"错误

java - 在具有许多模型对象的 Android 应用程序中,CRUD 操作应该放在哪里?