在HTML5 sqlite数据库中存储字节数组 [英] Storing byte array in HTML5 sqlite database

查看:132
本文介绍了在HTML5 sqlite数据库中存储字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发具有线程功能的移动网络应用,需要在HTML5数据库中存储大量原始字节数据。我想将它们尽可能紧凑地存储,因此连接字符串不是一个选项。下面是一个示例代码,用于创建数据库,表,插入数据并再次检索它。

I am working on an offline capable mobile web app and need to store large amounts of raw byte data in a HTML5 database. 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.

示例:

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

我正在尝试按原样存储数组,它实际上保存为连接字符串:97,0,6,244,98,66,76,65,131,5,7,142,81,184, 112,33。不是我需要的,因为它太大了。

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

DB现在返回的只是a。

What the DB now returns is simply "a".

所以我的问题是如何在HTML5数据库中序列化javascript字节数组而不依赖于加入的字符串?

So my question is how do I serialize a javascript byte array in HTML5 database without resorting to a joined string?

推荐答案

将数据转换为十六进制值并插入如下:

Convert data to hexadecimal values and insert like this:

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




BLOB文字是包含十六进制数据的字符串文字,前面是单个 x或X字符

BLOB literals are string literals containing hexadecimal data and preceded by a single "x" or "X" character

字面值

这篇关于在HTML5 sqlite数据库中存储字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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