0

I would like help in just one thing

@app.route('/getdata', methods =['GET', 'POST'])
#@requires_auth
def get_data():

    config = {
        'user': 'user',
        'password': 'password.',
        'host': '127.0.0.1',
        'database': 'database',
        'raise_on_warnings': True,
        'use_pure': False,
    }

    cnx = mysql.connector.connect(**config)

    cur = cnx.cursor()

    cur.execute("SELECT hash FROM hash_files")


     rows = cur.fetchall()
     response = [row[0] for row in rows]

    return jsonify(response)


    cnx.close()

I get this error "tuple indices must be integers, not str" can someone help me please?

I got that error before, but now I have this one

      Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\flask\app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python27\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Python27\lib\site-packages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Python27\webapi\venv\webapi.py", line 59, in get_data
    cnx = mysql.connector.connect(**config)
  File "C:\Python27\lib\site-packages\mysql\connector\__init__.py", line 179, in connect
    return MySQLConnection(*args, **kwargs)
  File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 98, in __init__
    self.close()
  File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 252, in close
    self.cmd_quit()
  File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 581, in cmd_quit
    self._socket.send(packet, 0, 0)
  File "C:\Python27\lib\site-packages\mysql\connector\network.py", line 143, in send_plain
    errno=2055, values=(self.get_address(), _strioerror(err)))
  File "C:\Python27\lib\site-packages\mysql\connector\errors.py", line 185, in __init__
    self.msg = self.msg % values
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 46: ordinal not in range(128)

Sorry for the long error, I didin't knew if you need it all so I send like this

I had de program working with sqlite3 but I needed to use mysql so to get all the data on the web api and the other program getting the data to an array and it's not working now

7
  • Please edit your post to include the error printout Commented Oct 18, 2017 at 20:44
  • @0TTT0 I add it now, thanks Commented Oct 18, 2017 at 20:54
  • @ShwSvn you're trying to call the api using a different script and you're posting the trace back of that script. Please post the trace back from your original program. Commented Oct 18, 2017 at 21:31
  • The traceback you've posted says that you're trying to json decode the response you're getting from the API. When your API isn't returning you a json object at all. Commented Oct 18, 2017 at 21:32
  • 1
    The error seems to happen during the mysql.connector.connect() call. Do you perhaps have any unicode characters in the hostname? This seems to be an encoding error that happens while processing a failed connection attempt. Commented Oct 18, 2017 at 21:59

1 Answer 1

1

.fetchall() function returns a list of tuple. So row is a tuple and you can't do row['hash']. That is why you're getting the error tuple indices must be integers, not str.

To access the values in the tuple, you need to use index: row[0]

Edit:

For fixing the decode error, you need to read this. Since it is a long answer to post here and someone else has answered it very well, I'm not typing it all here.

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

4 Comments

Like that? I changed the post But know I'm getting the other error that I posted UnicodeDecodeError UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 46: ordinal not in range(128)
@ShwSvn Please read this to have a better understanding of your current problem. stackoverflow.com/a/35444608/1666143
No, the stack trace shows that the error doesn't happen when getting the results but during connect()
I don't know, I restarted the pycharm and it was fixed with this change of row[0] Thanks a lot to everyone

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.