使用 Node.js 在 mariaDB 中插入 BLOB 数据

Insert BLOB data in mariaDB using Node.js

我想使用 node js 在 mariaDB 中插入 blob 数据,我正在使用 HeidiSQL 与 mariaDB 交互。 'users' table 有两个属性 '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;
}

错误

试试这个:

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改成数组来配合我看到的here and here.