0

When trying to insert a blob file into a MySQL database, I get errors. This is sample of my code:

@pyqtSlot()
    def on_click(self):
        file_name = QFileDialog.getOpenFileName(self, 'Open File', 'c:\\', 'Image Files (*.png *.jpg *gif)')
        image_path = file_name[0]

        self.converttobinary(image_path)

    def converttobinary(self, filename):
        with open(filename, 'rb') as file:
            binarydata = file.read()

        mycursor.execute("INSERT INTO imagestore (imgg) VALUES(%s)" % (binarydata))

        mydb.commit()
        print("image file inserted succesfully")

However, when I run the code I keep getting:

mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\xd9\x00\x00\x01\x11\x08\x06\x00' at line 1

1 Answer 1

1

quote the value for the column :

mycursor.execute("INSERT INTO imagestore (imgg) VALUES('%s')" % (binarydata))
Sign up to request clarification or add additional context in comments.

Comments

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.