0

I know this has been discussed a couple of times but following the suggestions provided still did not solve my problem.
I tried to reproduce a youtube tutorial on updating a table:

Code: Part1:

cursor.execute("CREATE DATABASE testdab")
cursor.execute("CREATE TABLE users (name VARCHAR(255), email VARCHAR(255), age INTEGER(10), user_id INTEGER AUTO_INCREMENT PRIMARY KEY)")

Part2:

insert_query = "INSERT INTO users (name, email, age) VALUES (%s, %s, %s)",
record1 = ("Peter", "[email protected]", 30)
cursor.execute(insert_query, record1)

The table is successfully created, but Part2 throws error message: AttributeError: 'tuple' object has no attribute 'encode'

Anyone having an idea what's going wrong here? Many thanks, Thomas

2
  • 1
    The extras comma at the end of the line creates a tuple. Commented Oct 10, 2021 at 12:38
  • Following what suggestions? Which YouTube video? Commented Oct 10, 2021 at 13:12

1 Answer 1

0

you have a syntax problem at end of line after query change
insert_query = "INSERT INTO users (name, email, age) VALUES (%s, %s, %s)",
to
insert_query = "INSERT INTO users (name, email, age) VALUES (%s, %s, %s)"
that comma makes insert_query a tuple and it cause the problem
because you have to give cursor.execute() a string at first argument and a tuple in second argument

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

1 Comment

Hi Aazerra, many thanks for your help and the explanation. It works!

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.