开发者

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

Example:

开发者_如何学Pythonvar 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". 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".

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 literals are string literals containing hexadecimal data and preceded by a single "x" or "X" character

Literal Values

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜