【问题标题】:Insert BLOB data in mariaDB using Node.js使用 Node.js 在 mariaDB 中插入 BLOB 数据
【发布时间】:2020-07-08 20:04:14
【问题描述】:

我想使用节点 js 在 mariaDB 中插入 blob 数据,我正在使用 HeidiSQL 与 mariaDB 交互。 'users' 表有两个属性 'user_email' 和 'profile_photo'。

类似问题

我发现了以下类似的问题,但它不适用于我的情况

NodeJS mySQL Insert Blob

这是我的代码:

const inputfile = "C:\\Users\\Hammad Ali\\Desktop\\bloob\\routes\\CNN.jpg";
var email = "xyz@gmail.com",
   photo = readImageFile(inputfile); 


   var query = "INSERT INTO `users` SET ?",
   values = {
     user_email: email,
     profile_photo: photo
   };
   pool.query(query, values,  function(err, res) {
  if (err) throw err;
  console.log("BLOB data inserted!"+res);
});
});

function readImageFile(file) {
  // read binary data from a file:
  const bitmap = fs.readFileSync(file);
  const buf = new Buffer.from(bitmap);
  return buf;
}

错误

【问题讨论】:

  • 如果你包含更多代码,并格式化这个更清洁,那会有所帮助。 pool 是什么?
  • 池是连接对象

标签: mysql node.js express mariadb blob


【解决方案1】:

试试这个:

var query = "INSERT INTO `users` (user_email, profile_photo) VALUES (?, ?)",

values = [
  email,
  photo,
];

pool.query(query, values, function(err, res) {
  if (err) throw err;
  console.log("BLOB data inserted!"+res);
});

MariaDB 文档中的示例都使用数组,所以我将values 更改为数组以匹配我看到的herehere

【讨论】:

  • 谢谢它的工作,我把它贴在它下面显示一些方框。
  • 你能帮我从数据库中检索这些数据,解码并将响应作为多部分发送到http请求
  • @HammadAli 很高兴为您提供帮助!我很乐意为数据库检索和响应周期提供帮助。看看你能走多远,然后当你遇到困难时再问一个问题,并在 cmets 中标记我,如果没有其他人先到达那里,我会看看。
  • 我需要以下问题的帮助。
猜你喜欢
  • 2017-01-12
  • 2012-03-14
  • 2013-08-15
  • 2011-10-26
  • 2017-05-01
  • 2015-09-17
  • 2011-06-21
  • 2016-02-10
  • 1970-01-01
相关资源
最近更新 更多