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:

    base64.encodebytes(filename)


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

    base(filename,'ascii')


    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:

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


    Here's the insert statement:

    self.cursor.execute(
    "INSERT INTO pixx1(photop, photopath)"
    "VALUES(?,?)",
    #the parameter placholder for pyodbc is the question mark sign (?), whereas that of MySql is %s
    (self.pixL.text(),
    self.pixTx.text())

    Continue reading...

Compartilhe esta Página