1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[SQL] unable to convert string to bytes from python to SQL server

Discussão em 'Outras Linguagens' iniciado por Stack, Setembro 4, 2021.

  1. Stack

    Stack Membro Participativo

    For hours I have been trying to save my python file image to my database table but to no avail due to conversion issue with an error notification of:

    Implicit conversion from data type nvarchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.

    The table column for photo has a data type of varbinary(max). I have made a research here and found similar posts and comments from members but none could help resolve mine.

    How do I convert a string value to bytes in python to enable me save my records in the database? I have tried to use bytes(filename), it didn't work. I also tried:


    I did that after importing base64, it still didn't work. Then I tried:


    it still didn't work.

    I want a situation when I upload the photo from the dialogbox and it gets appended to the file, the uploaded photo can be saved in the column reserved for it in the database table and the column has a data type of varbinary(max). The below is my code:

    fname, _ = QFileDialog.getOpenFileName(self, 'Open', 'c:\\', "image Files (*.jpg *.png)")
    strfn = bytes(fname,'ascii')
    self.pixL.setPixmap(QPixmap(fname).scaled(250, 250))
    except Exception as err:

    Here's the insert statement:

    "INSERT INTO pixx1(photop, photopath)"
    #the parameter placholder for pyodbc is the question mark sign (?), whereas that of MySql is %s

    Continue reading...

Compartilhe esta Página