Cordova SQLite save BLOB

霸气de小男生 提交于 2019-12-05 08:56:21

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 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).

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');
}

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

You could use blob literals and the hex function to treat blob values as hex strings at the interface between SQL and JavaScript, that works fine.

how big are your images? I've build an app which saved images to app database, see this inventory-service. I've using PouchDB and a customer base64-service. You can also find the App in the official stores.

My recommendation is to simply store your pictures as flat files, with unique file names of course, and store the references in your sqlite database.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!