node.js mysql bolb_如何用nodejs写入mysql 的blob格式的数据

在Node.js中,当尝试将大于128的数字存储为MySQL的BLOB数据时,发现每个数值会占用2字节。常规方法如使用String.fromCharCode()无法解决此问题。尝试使用Node.js的Buffer类进行转换,但结果相同。使用node-mysql库连接并插入数据时遇到困扰。
摘要由CSDN通过智能技术生成

So, if the array length equals 4, then the size of blob data in mysql DB must be 4 bytes. And it works fine with numbers less than 128.

var res = "";

for(var i = 0; i < arr.length; i++) {

res += String.fromCharCode(arr[i]);

}

But numbers from 128 to 256 takes 2 bytes.

I tried to use nodejs buffer

var Buffer = require('buffer').Buffer,

buf = new Buffer(arr.length);

for(var i = 0; i < arr.length; i++) {

buf[i] = arr[i];

}

buf.toString('binary');

but the same result. I have no idea how to make it work.

to store data in mysql database I use node-mysql

var Client = require('mysql').Client,

client = new Client();

client.user = DB_USER;

client.password = DB_PASS;

client.host = DB_HOST;

client.connect(function(error, results) {

if(error) {

client.end();

return;

}

client.query('USE ' + DB_SCHEME, function(error, results) {

if(error) {

client.end();

return;

}

var sql = "INSERT INTO b SET `data` = ?";

var values = [buf];

client.query(sql, values,

function(error, results) {

if(error) {

return;

}

return;

}

);

});

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值