简体   繁体   English

在HTML5 sqlite数据库中存储字节数组

[英]Storing byte array in HTML5 sqlite database

I am working on an offline capable mobile web app and need to store large amounts of raw byte data in a HTML5 database. 我正在开发具有脱机功能的移动Web应用程序,并且需要在HTML5数据库中存储大量原始字节数据。 I want to store them as compact as possible, so a joined string is not an option. 我希望将它们存储得尽可能紧凑,因此不能选择连接字符串。 Here is a sample code that creates a db, table, inserts data and retrieves it again. 这是一个示例代码,它创建一个db表,插入数据并再次检索它。

Example: 例:

var bytes=[97, 0, 6, 244, 98, 66, 76, 65, 131, 5, 7, 142, 81, 184, 112, 33];

openDatabase('_test_', 1.0, '_test-', 5000).transaction(function(tx) {
tx.executeSql("DROP TABLE IF EXISTS MYTABLE", [], function(){
tx.executeSql("CREATE TABLE IF NOT EXISTS MYTABLE(content BLOB);",[],function(){
tx.executeSql("INSERT INTO MYTABLE values(?)", [bytes], 
    function()
    {
        tx.executeSql("SELECT * FROM MYTABLE ", [], function(transaction, results)
        {
            console.log(results.rows.item(0))
        });
},function(transaction, error){console.log(error)})
},function(transaction, error){console.log(error)})
})
})

I am trying to store the array as is, which actually saves as a joined string :"97, 0, 6, 244, 98, 66, 76, 65, 131, 5, 7, 142, 81, 184, 112, 33". 我试图按原样存储数组,实际上将其另存为连接的字符串:“ 97、0、6、244、98、66、76、65、131、5、7、142、81、184、112、33 ”。 Not what I need since it will be way too large. 不是我所需要的,因为它太大了。

I am converting the array into a string now: 我现在将数组转换为字符串:

openDatabase('_test_', 1.0, '_test-', 5000).transaction(function(tx) {
tx.executeSql("DROP TABLE IF EXISTS MYTABLE", [], function(){
tx.executeSql("CREATE TABLE IF NOT EXISTS MYTABLE(content BLOB); ", [], function(){
tx.executeSql("INSERT INTO MYTABLE values(?)", [s], 
    function()
    {
        tx.executeSql("SELECT * FROM MYTABLE ", [], function(transaction, results)
        {
            console.log(results.rows.item(0))
        });
},function(transaction, error){console.log(error)})
},function(transaction, error){console.log(error)})
})
})

What the DB now returns is simply "a". DB现在返回的只是“ a”。

So my question is how do I serialize a javascript byte array in HTML5 database without resorting to a joined string? 所以我的问题是如何在不诉诸连接字符串的情况下序列化HTML5数据库中的javascript字节数组?

Convert data to hexadecimal values and insert like this: 将数据转换为十六进制值并按以下方式插入:

INSERT INTO t2 VALUES(X'53514C697465');

BLOB literals are string literals containing hexadecimal data and preceded by a single "x" or "X" character BLOB文字是包含十六进制数据并以单个“ x”或“ X”字符开头的字符串文字

Literal Values 文字价值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM