如何使用 blob 从 sqlite 数据库获取 sql 转储?

发布于 2024-10-09 05:15:30 字数 200 浏览 0 评论 0原文

我需要创建 sql 转储来修补现有的数据库文件。 我知道如何使用 sqlite3 命令行客户端创建 sql 转储,但它不能与 blob 正常工作。

如何使用适当的 blob 创建 sql 转储? sqlite3 .dump 命令是否有任何选项可以将 blob 编码为字符串?

我需要一个可编写脚本的解决方案才能将其集成到构建脚本中。

干杯

I need to create sql dumps in order to patch existing database files.
I know how to create sql dumps with sqlite3 command line client but it does not work properly with blobs.

how to create sql dumps with proper blobs ?
is there any option to sqlite3 .dump command to encode blobs as strings ?

I'd need a scriptable solution in order to integrate it into build scripts.

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

禾厶谷欠 2024-10-16 05:15:30

如果您的数据以某种方式插入,SQLite 认为它是字符串(TEXT)而不是 BLOB,您可以使用临时表并强制类型转换来转换它。假设您有下表:

CREATE TABLE images ( id, name, data )

示例解决方案是:

CREATE TEMP TABLE images2 ( id INT, name TEXT, data BLOB );
INSERT INTO images2
    SELECT id, name, CAST ( data AS BLOB ) FROM images;
DROP TABLE images;
CREATE TABLE images ( id INT, name TEXT, data BLOB );
INSERT INTO images
    SELECT * FROM images;

为我工作...

If your data was inserted in a way, that SQLite thinks it's string (TEXT) instead of BLOB, you can convert it using temporary table and forcing type conversion. Lets say you have the following table:

CREATE TABLE images ( id, name, data )

The example solution is:

CREATE TEMP TABLE images2 ( id INT, name TEXT, data BLOB );
INSERT INTO images2
    SELECT id, name, CAST ( data AS BLOB ) FROM images;
DROP TABLE images;
CREATE TABLE images ( id INT, name TEXT, data BLOB );
INSERT INTO images
    SELECT * FROM images;

Worked for me...

怎樣才叫好 2024-10-16 05:15:30

它应该开箱即用。

我这样创建了表:

sqlite> CREATE TABLE images( id INTEGER PRIMARY KEY, name TEXT, content BLOB );

插入一行:

sqlite> INSERT INTO images VALUES ( 1, 'test 1', X'00ff00ffaabbcc' );

创建一个转储:

sqlite> .dump

就可以了:

PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE images( id INTEGER PRIMARY KEY, name TEXT, content BLOB );
INSERT INTO "images" VALUES(1,'test 1',X'00FF00FFAABBCC');
COMMIT;

It should work out of the box.

I created the table this way:

sqlite> CREATE TABLE images( id INTEGER PRIMARY KEY, name TEXT, content BLOB );

Inserted one row:

sqlite> INSERT INTO images VALUES ( 1, 'test 1', X'00ff00ffaabbcc' );

Created a dump:

sqlite> .dump

And it's OK:

PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE images( id INTEGER PRIMARY KEY, name TEXT, content BLOB );
INSERT INTO "images" VALUES(1,'test 1',X'00FF00FFAABBCC');
COMMIT;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文