Storing binary data as a string in a Web Sql database

So, I load chunks of data via xhr, and I want to save it locally for the chrome extension.

At first I managed to save it to localStorage, but then I needed to switch to larger chunks that localStorage could not handle. So I decided to save it in Web Sql Database . However, I was able to store the data in localStorageexactly the same way as it was loaded, but when I try to save it in the database, the data is lost or I get an error message.

Same:

//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]);

The only other way I can get the data in the database is to use btoa(file)it and it works, but I think the performance will be pretty significant if you need to do btoa/ atobevery time the data is stored / used.

So my question is how do you store data in a database without use btoa, or if this is not possible, how much performance is btoaused?

I'm also sure that it localStorageuses SQLite as well as Web Sql database, so why can't I just put the data in the latter, as simple as possible, with localStorage?

+3
source share
1

- , base64, .

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

.

, , , SQLite

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

, SQLite, , .

0

Source: https://habr.com/ru/post/1757436/


All Articles