WebReflection / dblite

sqlite for node.js without gyp problems
MIT License
209 stars 34 forks source link

nodejs buffer in blob #16

Closed erf closed 10 years ago

erf commented 10 years ago

is nodejs buffer in blob supported? example?

WebReflection commented 10 years ago

Does sqlite3-Shell support it? If yes, so does dblite, otherwise no.

erf commented 10 years ago

sqlite3 supports BLOB, and i was able to write a node buffer using sqlite3-node, but how would i go about to transform the 'bufferAsString' below back to a buffer? Thanks for any help!

var dblite = require('dblite');
var db = dblite(':memory:');
var buf = new Buffer('hello world');
console.log('before');
console.log(buf.toString());
db.query('CREATE TABLE test (data BLOB)');
db.query('INSERT INTO test VALUES (?)', [buf]);
db.query('SELECT * FROM test', function(rows){
    console.log('after');
    var bufferAsString = rows[0][0];
    console.log(bufferAsString);
    db.close();
});
erf commented 10 years ago

I've also tried this whitout success:

var dblite = require('dblite');
dblite.bin = "C:\\Users\\Erlend\\bin\\sqlite3.exe";
var db = dblite(':memory:');
var buf = new Buffer('hello world');
console.log('before');
console.log(buf.toString());
db.query('CREATE TABLE test (data BLOB)');
db.query('INSERT INTO test VALUES (?)', [buf]);
db.query('SELECT * FROM test', {data: Buffer}, function(rows){
    console.log('after');
    var bufOut = rows[0].data;
    console.log(bufOut.toString());
    db.close();
});
WebReflection commented 10 years ago

You are over a spawned bridge, you need strings there, that's all you need ;-)

update, next message you see you can have Buffers too


var
  dblite = require('dblite'),
  db = dblite(':memory:'),
  buffer = new Buffer('hello world')
;

db
  .query('CREATE TABLE test (data BLOB)')
  .query('INSERT INTO test VALUES (?)', [buffer.toString()])
  .query('SELECT * FROM test',
  {
    data: function reBuffer(str){
      return new Buffer(str);
    }
  },
  function (rows) {
    console.log(rows[0].data.toString());
    this.close();
  })
;
WebReflection commented 10 years ago

actually, if you pass the Buffer directly JSON.stringify(obj) will do the magic for you so that you can simply use that info to have the buffer back without forcint the toString.


// no toString call
db.query('INSERT INTO test VALUES (?)', [buffer.toString()]);

// parse and recreate
db.query('SELECT * FROM test',
  {
    data: function reBuffer(str){
      return new Buffer(JSON.parse(str).data);
    }
  },
  function (rows) {
    console.log(rows[0].data);
    this.close();
  });
erf commented 10 years ago

oh yeah, silly me, i just had to JSON.parse it, that works=)

had to alter a few things

db.query('INSERT INTO test VALUES (?)', [buf]);
db.query('SELECT * FROM test',
    {
        data: function reBuffer(str){
            return new Buffer(JSON.parse(str));
        }
    },
    function (rows) {
        console.log(rows[0].data.toString());
        this.close();
    });

or just

db.query('INSERT INTO test VALUES (?)', [buf]);
db.query('SELECT * FROM test',
    function (rows) {
        var arr = JSON.parse(rows[0][0]);
        console.log(new Buffer(arr).toString());
        this.close();
    });

Thanks alot for your help and awsome library. So much trouble building sqlite3-node, especially on windows..