0

I'm having this error AttributeError: 'tuple' object has no attribute 'encode' and here is the part of my code. can you please help me

cur = conn.cursor()
sql = "SELECT * FROM farm_registration \
       WHERE barangay = %s", (e_barangay);
cur.execute(sql)
results = cur.rowcount
2
  • can you post the whole code and the error stacktrace? Commented Mar 31, 2018 at 4:28
  • Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python3.5/tkinter/__init__.py", line 1562, in call return self.func(*args) File "/home/pi/Desktop/pow.py", line 415, in put cur.execute(sql) File "/usr/lib/python3/dist-packages/mysql/connector/cursor.py", line 536, in execute stmt = operation.encode(self._connection.python_charset) AttributeError: 'tuple' object has no attribute 'encode' Commented Mar 31, 2018 at 4:30

1 Answer 1

1

This statement returns a tuple

sql = "SELECT * FROM farm_registration \
       WHERE barangay = %s", (e_barangay);

which you are expecting string instead, so just changed the above into

sql = "SELECT * FROM farm_registration \
       WHERE barangay = %s" % e_barangay;
Sign up to request clarification or add additional context in comments.

3 Comments

we did that but now we have this error :mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''Saguing'' at line 1, my input at baranggay is Saguing
how about using something like this: sql = "SELECT * FROM farm_registration WHERE barangay = %s" cur.execute(sql, (e_barangay))
thank you @johnIl but the problem is it should be sql = "SELECT * FROM farm_registration WHERE barangay = '%s'" % e_barangay the '' on %s is missing. but thanks anyway

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.