0

In a phython script I read a value from a hardware module. This value I want to insert in a mariadb

I convert the hardware value to a string.

wert_float="{:.1f}".format(sensor1)
wert = str(wert_float)

mycursor =mydb.cursor()
sql = "INSERT INTO werte (feuchtigkeit) VALUES (?)", (wert)
mycursor.execute(sql)
mydb.commit()
mycursor.close()
mydb.close()

I tried the statement with (%s) too, but at all tries I got the error:

Traceback (most recent call last):
  File "./waterpi_sql.py", line 118, in <module>
    mycursor.execute(sql)
TypeError: a bytes-like object is required, not 'tuple'

Can someone help me?

0

1 Answer 1

1

You need to move your query params to actual function params

sql = "INSERT INTO werte (feuchtigkeit) VALUES (?)"
mycursor.execute(sql, (wert,))

Otherwise, you've assigned sql to a tuple, which is not executable, and the way you've written is not expressed in any docs that I've seen, so make sure you refer back to those

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

1 Comment

mycursor.execute(*sql) would also be an option, if sql were assigned the correct tuple originally (i.e., sql = "...", (wert,)).

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.