3

Running this piece of code returns a type error of 'can't concat tuple to bytes', but in my codes I don't see where the bytes are coming from, hence unsure how to fix my codes. Any pointers on which line of codes is in the form of bytes?

def array2python():

     mfcc = "US"
     number = 8 #could be any number from 0 to 9
     t = (number, mfcc)
     conn = pymysql.connect( host=hostname, port = port, user=username, passwd=password, db=database, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor )
     cur = conn.cursor()
     query = ('INSERT INTO audioFiles VALUES (?,?)', t)
     cur.execute(query)
     conn.close()

1 Answer 1

7

The problem is on this line:

query = ('INSERT INTO audioFiles VALUES (?,?)', t)

You are defining the query as a tuple which then execute() fails to understand.

You mean to do:

query = 'INSERT INTO audioFiles VALUES (?,?)'
cur.execute(query, t)

And, I think you need to use %s as a placeholder:

query = 'INSERT INTO audioFiles VALUES (%s,%s)'
cur.execute(query, t)
Sign up to request clarification or add additional context in comments.

2 Comments

I suspected that I need to do %s but would this invite SQL injections?
@jc76 if you use these %s placeholders to do string formatting - then, ys. But, in our case we are using them as query placeholders and passing parameters in a second argument to execute(). Thanks.

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.