Hammad Ali
Hammad Ali

Reputation: 331

Insert BLOB data in mariaDB using Node.js

I want to insert blob data in mariaDB using node js,I am using HeidiSQL to interact with mariaDB. 'users' table have two attributes 'user_email' and 'profile_photo'.

Similar Question

I found the following similar question but its not working in my case

NodeJS mySQL Insert Blob

Here is my code:

const inputfile = "C:\\Users\\Hammad Ali\\Desktop\\bloob\\routes\\CNN.jpg";
var email = "[email protected]",
   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;
}

Error

enter image description here

Upvotes: 0

Views: 2151

Answers (1)

djs
djs

Reputation: 4065

Try this:

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

The examples in the MariaDB docs all use arrays, so I changed values to an array to match up with what I saw here and here.

Upvotes: 1

Related Questions