使用Bash将大图像作为BLOB存储在SQLite DB中 [英] Storing large images as BLOB inside a SQLite DB using Bash
问题描述
我创建了一个小脚本来导入SQLite数据库中的大图片目录
我完全知道SQLite不是存储大blob的完美的地方,但这是我必须做的。
其实,我正在使用hexdump:
sqlite3 name.dbINSERT INTO table(image)values(x'$(hexdump -v -e'1/1%02x'./filename.jpg)');
但是偶尔会在相当大的图片上显示错误:
sqlite3:参数列表太长
当SQL命令作为参数提供给 sqlite3
,必须在之前生成 hexdump
的整个输出。
管道没有大小限制:
(echo -nINSERT INTO表(image)值(x'
hexdump -v -e'1/1%02x'./filename.jpg
echo');)| sqlite3 name.db
或者,您可以将命令写入文件, code> sqlite3 到 .read
it。
SQLite shell版本3.8.6或更高版本具有用于直接读取文件的 readfile()函数。 / p>
I'm creating a little script to import a big directory of images inside a SQLite database, I'm perfectly aware that SQLite is not the perfect place to store big blobs but this is how i must do it.
Actually, I'm trying using hexdump:
sqlite3 name.db "INSERT INTO table (image) values(x'"$(hexdump -v -e '1/1 "%02x"' ./filename.jpg)"');"
But occasionally, on rather big images, it returns an error:
sqlite3: Argument list too long
How would you work around this problem?
When the SQL command is given as a parameter to sqlite3
, the entire output of hexdump
must be generated before the parameter can be actually used.
A pipe does not have size limits:
(echo -n "INSERT INTO table (image) values(x'"
hexdump -v -e '1/1 "%02x"' ./filename.jpg
echo "');") | sqlite3 name.db
Alternatively, you could write the command(s) into a file, and tell sqlite3
to .read
it.
The SQLite shell in version 3.8.6 or later has the readfile() function for reading a file directly.
这篇关于使用Bash将大图像作为BLOB存储在SQLite DB中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!