gpt4 book ai didi

javascript - 将二进制数据作为字符串存储在 Web Sql 数据库中

转载 作者:行者123 更新时间:2023-12-02 22:35:48 26 4
gpt4 key购买 nike

好的,我正在通过 xhr 下载大量数据,并且想将其存储在本地以供 Chrome 扩展使用。

起初我能够将它存储在localStorage中,但后来我需要转向 localStorage 无法处理的更大块。所以我决定将其存储在 Web Sql Database 中。但是我能够将数据存储在 localStorage 中与下载时完全相同,但是当我尝试将其存储在数据库中时,数据会丢失或出现错误。

像这样:

//this way the data gets lost because it is filtered to prevent SQL injection attacks
tx.executeSql('INSERT INTO files (id, content) VALUES (?,?)', [fileID,file]);

//this method gives an error (unrecognized token) because the data contains byte values in the string
tx.executeSql('INSERT INTO files (id, content) VALUES (?, "' + file + '")', [id]);

我能想到的将数据导入数据库的唯一其他方法是使用 btoa(file) ,并且它有效,但我认为如果必须执行 btoa ,性能将受到相当大的打击。/atob每次存储/使用数据时。

所以我的问题是,如何在不使用 btoa 的情况下将数据存储在数据库中,或者如果不可能的话,性能会如何btoa使用?

我也相当确定localStorage使用 SQLite 以及 Web Sql 数据库,那么为什么我不能像使用 localStorage 那样轻松地将数据放入后者中? ?

最佳答案

看看这样的东西,您需要对二进制数据进行 Base64 编码,因为它需要是可打印的字符。

http://www.webtoolkit.info/javascript-base64.html

但这将导致二进制数据的大小增加约四倍。

我认为,您可能需要将其编码为十六进制,以便使用 SQLite 表示二进制数据

tx.executeSql('INSERT INTO files (id, content) VALUES (2, "0xBADF00D12840F32");');

例如,后一种 SQLite 方法可能会更有效。

关于javascript - 将二进制数据作为字符串存储在 Web Sql 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3376653/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com