Cordova SQLite保存BLOB [英] Cordova SQLite save BLOB

查看:272
本文介绍了Cordova SQLite保存BLOB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Cordova SQLite插件时遇到问题。

I have a problem with Cordova SQLite plugin.

如何将BLOB图像保存到SQLite?

How can I save BLOB image to SQLite?

我在JS中有BLOB对象:

I have BLOB object in JS:

Blob:{
size: 96874
type: "image/jpeg"
__proto__: Blob
length: 1}

我尝试保存它

INSERT INTO images (img) VALUES (?);

当我试图得到这张图片时:

And when i trying to get this image:

SELECT img FROM images

我得到这个字符串:

img: "{"type":"image\/jpeg","size":96874}"

如何将其再次转换为BLOB?

How can i convert it to BLOB again?

另外我试图在base64中保存图像,但我无法将大图像保存到SQLite,因为base64字符串非常庞大。 (这适用于小图片)

Also i trying to save images in base64, but i can't save big images to SQLite, because base64 string is very huge. (It's works with small images)

请帮帮我,抱歉我的英文。

Please, help me and sorry for my English.

推荐答案

您无法将JavaScript Blob对象保存到SQLite插件。它不支持Blob格式。是的我知道它有一个 BLOB 类型,但令人困惑的是它不是一回事。 :)

You cannot save JavaScript Blob objects to the SQLite Plugin. It doesn't support the Blob format. Yes I know it has a BLOB type, but confusingly it's not the same thing. :)

WebSQL / SQLite插件确实支持二进制字符串作为Blob的替代品。但是,您可能遇到问题,因为iOS和Android上的实现中存在大量错误(一些细节在这里

WebSQL/SQLite Plugin does support binary strings as an alternative to Blobs. However, you may run into issues because there are numerous bugs in the implementation on both iOS and Android (some details are here).

在PouchDB中,我们解决了这些问题,因此附件API会为您提取所有内容。按照 PouchDB附件指南,它将为您转换Blob并将其存储在数据库中。要创建与SQLite插件对话的PouchDB,您需要执行以下操作:

In PouchDB we worked around these issues, so the attachment API abstracts everything away for you. Follow the PouchDB attachment guide and it will convert Blobs for you and store them in the database. To create a PouchDB that talks to the SQLite Plugin, you will need to do the following:

/* prefer websql, which will actually use the SQLite Plugin if available */
var db = new PouchDB('mydbname', {adapter: 'websql'});
if (!db.adapter) {
  /* fall back to IndexedDB for FirefoxOS, Windows Phone, etc. */
  db = new PouchDB('mydbname');
}

如果你想在Blob和二进制字符串或各种其他格式之间进行转换,请查看 blob-util

If you want to convert between Blobs and binary strings or a variety of other formats, check out blob-util.

这篇关于Cordova SQLite保存BLOB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆