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

如何用nodejs写入mysql 的blob格式的数据的更多相关文章

  1. MYSQL导入CSV格式文件数据执行提示错误(ERROR 1290): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement.

    MYSQL导入CSV格式文件数据执行提示错误(ERROR 1290): The MySQL server is running with the --secure-file-priv option s ...

  2. Nodejs操作MySQL数据库

    https://github.com/mysqljs/mysql   如何用nodejs操作MySql数据呢,其实写法还是简单的, 1.开始在你的node项目中 npm install mysql - ...

  3. Flink 1.9 实战:使用 SQL 读取 Kafka 并写入 MySQL

    上周六在深圳分享了<Flink SQL 1.9.0 技术内幕和最佳实践>,会后许多小伙伴对最后演示环节的 Demo 代码非常感兴趣,迫不及待地想尝试下,所以写了这篇文章分享下这份代码.希望 ...

  4. scrapy爬虫成长日记之将抓取内容写入mysql数据库

    前面小试了一下scrapy抓取博客园的博客(您可在此查看scrapy爬虫成长日记之创建工程-抽取数据-保存为json格式的数据),但是前面抓取的数据时保存为json格式的文本文件中的.这很显然不满足我 ...

  5. nodejs应用mysql(纯属翻译)

    原文点击这里 目录 Install Introduction Contributors Sponsors Community Establishing connections Connection o ...

  6. saltstack:使用教程之一安装及客户端返回写入MySQL

    saltstack使用教程: 1.安装: 需要epel的yum源,没有的话把下面的复制并新建个文件 /etc/yum.repos.d/epel.repo 粘贴即可: [epel] name=Extra ...

  7. Spark:将DataFrame写入Mysql

    Spark将DataFrame进行一些列处理后,需要将之写入mysql,下面是实现过程 1.mysql的信息 mysql的信息我保存在了外部的配置文件,这样方便后续的配置添加. //配置文件示例: [ ...

  8. nodejs中mysql断线重连

    之前写了个小程序Node News,用到了MySQL数据库,在本地测试均没神马问题.放上服务器运行一段时间后,偶然发现打开页面的时候页面一直处于等待状态,直到Nginx返回超时错误.于是上服务器检查了 ...

  9. MySQL的blob类型

    MySQL中的Blob类型 MySQL中存放大对象的时候,使用的是Blob类型.所谓的大对象指的就是图片,比如jpg.png.gif等格式的图片,文档,比如pdf.doc等,以及其他的文件.为了在数据 ...

随机推荐

  1. 学习练习 java 输入输出流练习2

     编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt p ...

  2. iosiOS 地图 自定义以及添加锚点

    - (void)clickLongPress:(UILongPressGestureRecognizer *)longPress { CGPoint point = [longPress locati ...

  3. Exceeded maximum number of retries. Exceeded max scheduling attempts 3 for instance

    Exceeded maximum number of retries. Exceeded max scheduling attempts 3 for instance

  4. Ov

  5. vsftp配置参数

    转载:http://blog.chinaunix.net/uid-134240-id-172158.html listen_address=ip address 指定侦听IP listen_port= ...

  6. Java druid

    1.ConnectionFactory (添加引用:druid-1.0.1.jar) package nankang.test; import java.sql.Connection; import ...

  7. C++利用注册表添加桌面右键新建菜单

    对于程序员来说,新建一个cpp文件是再频繁不过的事情了. 为了方便,我们习惯在桌面右键新建文件,而不是新建一个文本文档,然后修改后缀名. 百度谷歌查询了一下,终于知道如何添加注册表. 手痒,抽出时间用 ...

  8. ASP.NET验证控件二

    RequiredFieldValidator 验证控件 页面布局: <div> <h1>RequiredFieldValidator 验证控件</h1>   用户名 ...

  9. MySQL查询昨天、今天、7天、近30天、本月、上一月数据

    文章同步发表在博主网站朗度云,传输门:http://www.wolfbe.com/detail/201608/291.html   在开发或者统计时,我们可能需要统计某个表的数据.比如:查看今天新增的 ...

  10. [原]hdu2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 (这个只是题目名字) (多重背包)

    本文出自:http://blog.csdn.net/svitter 原题:http://acm.hdu.edu.cn/showproblem.php?pid=2191 题意:多重背包问题.转换成为01 ...