0

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

2
  • If you included more code, and formatted this cleaner, that would help. What is pool? Commented Mar 28, 2020 at 6:44
  • pool is conncection object Commented Mar 28, 2020 at 8:37

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank You its working,I post it below its show some square boxes.
can you please help me to retrieve this data from database,decode and send response as multipart to http request
@HammadAli Glad to help! I'd be happy to help out with the DB retrieval and response cycle. See how far you can get, then ask another question when you get stuck and tag me in the comments and I'll have a look if no one else gets there first.
I need help in the following question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.